function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
zeezackzeezack 

How do you insert a record into Saleforce and retrieve last inserted id

I tried to establish this application using apex...but I am now turning over to php to do the job.
Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
If you're using the web services API, the create / update / upsert calls return a SaveResult that include the Id of the row.

All Answers

SuperfellSuperfell
If you're using the web services API, the create / update / upsert calls return a SaveResult that include the Id of the row.
This was selected as the best answer
zeezackzeezack

Thank you for your reply Simon, can you provide examples...

I've tried using create on an  sObject and it fails?

 

 
    $contact = new SObject('Account',
                           null,
                           array(
                               'Id' => "33234534534",
                               'Name' => "name"
                           )                          
                          );
    $createResult = $mySforceConnection->create($contact);

werewolfwerewolf

You can't set the ID when you're doing an insert -- ID is an autogenerated field.  In general you should wrap your stuff here in a try/catch as with this sample:

 

http://wiki.apexdevnet.com/index.php/PHP_Toolkit_13.0_Update_Sample_(Partner)

 

That way it will actually tell you what the error is.

zeezackzeezack

Ah that was good thinking werewolf...

 

ok I think its inserting stuff into the database now... how do  pull out the LAST INSERTED / UPDATED id

 

 

werewolfwerewolf
Look at what's getting stored in $createResult.  Try print_r on it perhaps.  You'll see that it contains an array of SaveResults, and each one will have an ID attribute.  In your case there will be just one, but it will contain the ID of the newly created object.
zeezackzeezack

Hey ya buddy,

just tried that

 

 

Ok I know I can run a select after the insert... and pull out what should be the last id, from an order by date DESC query...

 

but is there a function to pull out the last id.

werewolfwerewolf
Why would you want to do that?  You have the last ID -- you just created it.
zeezackzeezack

Yo, ok

 

I am making a web form, which would allow a client to add a person to the account table.

 

I then need the id of this new person to pass into a new web form. A claims form.

 

Thus I need the id of the account to attach to a new claim.

 

I was not sure how to do this so in apex and java - from what I could tell that would just create the forms based on the api.

 

 

So yeah... now I've managed to make a new insert... is there a neat way of getting the latest id like

 $last_id = mysql_insert_id();

 

 

 

SuperfellSuperfell
the Id is returned as part of the create call response, there's no need for an additional call.
werewolfwerewolf

Well, as I said, it's going to be in your SaveResult object.  Off the top of my head it would probably be something like $createResult[0]->Id.

zeezackzeezack

hey ok

so how I do retrieve it as a variable?

 

$createResult = $mySforceConnection->create($sObjects_insert);

 

is it something like

 

$lastid->id-> $createResult;

zeezackzeezack
ah let me try that... hold that thought guys
werewolfwerewolf
It's already a variable.  It's $createResult, which is probably an array so it's something like $createResult[0].Id, as I said.  There is no $lastid.
zeezackzeezack

oo

Fatal error: Cannot use object of type stdClass as array

werewolfwerewolf
Ok, well, run a debugger on it or print_r your $createResult variable to look at its contents.  That was just off the top of my head -- maybe PHP makes it not an array if it's just got one element.  I speak many languages, I tend to get them confused.
zeezackzeezack

Hey ya guys,

one more hurdle.

 

oK I've got the last id thing.

 

now I want to insert it into the contact form just as a tester

 

I get an error

 

 

 

        $sObject = new sObject();
        $sObject->type = 'Contact';

            $sObject->fields = array('Name' => $fullname, 'AccountId' => $accountid);
            array_push($sObjects_insert, $sObject);
            echo "insert<br />";
            flush();
   

        if (count($sObjects_insert) > 0)
        {
             $createResult = $mySforceConnection->create($sObjects_insert);
             echo''.$createResult.'';
             print_r($createResult);
        }

 

        

 Object id #9stdClass Object ( [errors] => stdClass Object ( [fields] => Name [message] => Unable to create/update fields: Name. Please check the security settings of this field and verify that it is read/write for your profile

SuperfellSuperfell
Name is a readonly compound field, you need to set firstName / lastName.