+ Reply to Thread
Results 1 to 5 of 5

Thread: Create Record on Custom Object using PHP

  1. #1
    mike's Avatar
    mike is offline Administrator
    Join Date
    May 2007
    Location
    Wylie, Texas
    Posts
    557
    Blog Entries
    15

    Create Record on Custom Object using PHP

    Over at the Salesforce.com developer forums Paul asked for some help on his custom object to create records in a custom object he had created for his Org, called PFC_API and could not get the script to work.

    Here is an working script with some of the fields from his script that I have tested against my developers org. I created the same fields in an object that I created called PFC_API:

    PHP Code:
    <?php
    ini_set
    ("soap.wsdl_cache_enabled""0");


    require_once (
    './includes/soapclient/SforcePartnerClient.php');
    require_once (
    './includes/soapclient/SforceHeaderOptions.php');

    $campaign_read "My Campaign";
    $document_read "My Document";
    $emailid "contact@email.com";
    $sfid "00550000000wI2LAAU";
    $ownerid $sfid;
    $name "Samsung";




    $mystuff = array('Name' => $name,
                     
    'OwnerId' => $ownerid,
                     
    'PFC_Campaign__c' => $campaign_read,
                     
    'PFC_Document_Name__c' => $document_read,
                     
    'PFC_Contact_Email__c' => $emailid,
                     
    'Contact__c' => $sfid,
                     
    'PFC_Action__c' => 'Read', );

    //clean contacts to allow changing of characters to XML
    $mystuff array_map('htmlspecialchars'$mystuff);

    //try to add contact from form
    try 
    {
        
    //salesforce login and wsdl
        
    $wsdl './includes/soapclient/partner.wsdl.xml';
        
    $userName "changeme";
        
    $password "changeme";

        
    //connect to salesforce
        
    $client = new SforcePartnerClient();
        
    $client->createConnection($wsdl);
        
    $loginResult $client->login($userName$password);


        
    $sObject = new sObject();
        
    $sObject->type 'PFC_API__c';
        
    $sObject->fields $mystuff;


        
    //this part adds the contact to salesforce
        
    $result $client->create(array($sObject));

        if (
    $result->success
        {
            echo 
    "New Record has been added to your custom Object with and id of " $result->id "<br />";
            echo 
    "**************** ALL DONE ****************<br />";
            exit;
        } 
        else 
        {
            
    $errMessage $result->errors->message;
            echo 
    $errMessage;
        }


    }
    catch (
    exception $e
    {
        
    // This is reached if there is a major problem in the data or with
        // the salesforce.com connection. Normal data errors are caught by
        // salesforce.com
        
    echo '<pre>' print_r($etrue) . '</pre>';
        return 
    false;
        exit;
    }


    ?>
    This works and should work for anyone that needs to modify it for their custom object


    ~Mike

  2. #2
    ncee is offline Junior Member
    Join Date
    Oct 2007
    Posts
    14

    Exclamation help Creating contacts

    @Mike

    I used your code to try to create a record. I slightly modified the array and the object type.

    PHP Code:
    <?php
    ini_set
    ("soap.wsdl_cache_enabled""0");


    require_once (
    './includes/soapclient/SforcePartnerClient.php');
    require_once (
    './includes/soapclient/SforceHeaderOptions.php');

    $mystuff = array('Username'         => 'user@email.com'// change this to a valid email
                     
    'LastName'         => 'Wiley',
                     
    'FirstName'        => 'Mike',
                     
    'Email'            => 'user@email.com'// change this to a valid email
                     
    'Alias'            => 'MWile',
                     
    'IsActive'         => 'true',
                     
    'TimeZoneSidKey'   => 'America/New_York',
                     
    'LocaleSidKey'     => 'en_US',
                     
    'EmailEncodingKey' => 'ISO-8859-1',
                     
    'ProfileId'        => '00550000000wI2LAAU',
                     
    'LanguageLocaleKey'=> 'en_US');
                     
    //clean User to allow changing of characters to XML
    $mystuff array_map('htmlspecialchars'$mystuff);

    //try to add User from from array
    try 
    {
        
    //salesforce login and wsdl
        
    $wsdl './includes/soapclient/partner.wsdl.xml';
        
    $userName "email@gmail.com";
        
    $password "somepassword";

        
    //connect to salesforce
        
    $client = new SforcePartnerClient();
        
    $client->createConnection($wsdl);
        
    $loginResult $client->login($userName$password);

        
    $sObject = new sObject();
        
    $sObject->type 'User';
        
    $sObject->fields $mystuff;


        
    //this part adds the contact to salesforce
        
    $result $client->create(array($sObject));

        if (
    $result->success
        {
            echo 
    "New User has been added to your instance of Salesforce with an Id of " $result->id "<br />";
            echo 
    "**************** ALL DONE ****************<br />";
            exit;
        } 
        else 
        {
            
    //print "Unsucessful Create<br>";
            
    $errMessage $result->errors->message;
            echo 
    $errMessage;
        }

    }
    catch (
    exception $e
    {
        
    // This is reached if there is a major problem in the data or with
        // the salesforce.com connection. Normal data errors are caught by
        // salesforce.com
        
    echo '<pre>' print_r($etrue) . '</pre>';
        return 
    false;
        exit;
    }


    ?>
    I get this error:

    invalid cross reference id

    have any ideas how to fix this error?

  3. #3
    mike's Avatar
    mike is offline Administrator
    Join Date
    May 2007
    Location
    Wylie, Texas
    Posts
    557
    Blog Entries
    15
    Ncee

    Can you post just the lines that you changed? and did you use this example or the one from this URL > Create User On Salesforce with PHP


    ~Mike

    PS > maybe you could email me the script if you do not want to post it here

  4. #4
    ncee is offline Junior Member
    Join Date
    Oct 2007
    Posts
    14

    Exclamation RE:

    Mike,

    I used the code at Create User On Salesforce with PHP and modified the username and password to my cridentials.

    PHP Code:

    $userName 
    "email@gmail.com";
    $password "somepassword"
    Do you think it might have something to do with the profileID in the array?

    ~NCEE

  5. #5
    mike's Avatar
    mike is offline Administrator
    Join Date
    May 2007
    Location
    Wylie, Texas
    Posts
    557
    Blog Entries
    15
    yes that is what the error is telling me!

    I got that profile ID from my dev instance, try and switch yours and rerun the script

+ Reply to Thread

LinkBacks (?)

  1. Hits: 1
    03-15-2010, 02:52 PM
  2. Hits: 1
    03-07-2010, 08:20 PM
  3. Hits: 1
    02-25-2009, 01:51 AM
  4. Hits: 1
    12-16-2008, 09:03 PM
  5. Hits: 2
    10-01-2008, 01:06 PM
  6. Hits: 1
    08-20-2008, 12:01 PM
  7. Hits: 2
    08-15-2008, 02:25 PM
  8. Hits: 1
    06-30-2008, 01:07 AM
  9. Hits: 1
    06-09-2008, 10:53 PM
  10. Hits: 1
    04-25-2008, 02:38 AM
  11. Hits: 1
    04-02-2008, 07:27 PM
  12. Hits: 1
    10-30-2007, 09:55 PM
  13. Hits: 1
    09-24-2007, 08:17 AM
  14. Hits: 1
    09-17-2007, 04:45 PM
  15. Hits: 2
    09-12-2007, 03:43 PM
  16. Hits: 1
    09-12-2007, 03:35 PM
  17. Hits: 1
    09-12-2007, 12:41 PM
  18. Hits: 1
    09-12-2007, 08:25 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

SEO by vBSEO 3.5.1