Home » Php » php – associate contact with account using salesforce api

php – associate contact with account using salesforce api

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am new to using the salesforce api. I have downloaded the saleforce/php toolkit and am able to successfully create contacts and accounts from a webform on my server.

To create a contact I am doing the following:

    $records[0] = new stdclass();
    $records[0]->FirstName = $FirstName;
    $records[0]->LastName = $LastName;
    $records[0]->Email = $Email;
    $records[0]->Phone = $Phone;
    $records[0]->MailingStreet = $MailingStreet;
    $records[0]->MailingCity = $MailingCity;
    $records[0]->MailingState = $MailingState;
    $records[0]->MailingPostalCode = $MailingPostalCode;
    $records[0]->MailingCountry = $MailingCountry;
    $records[0]->LeadSource = $LeadSource;

    $create = $mySforceConnection->create($records, 'Contact');

To create an account I am doing the following

    $records[0] = new stdclass();
    $records[0]->Name = $Name

    $create = $mySforceConnection->create($records, 'Account');

Can anyone give me a simple example of how I would associate a contact with an account?

I have a check-box on the form that asks if this is an organization. If the user checks this box I would like to create an organization account with some of the data and create a contact with some of the data and associate the two.

I am not looking for a full blown working example but more just somthing pointing me in the right direction.

Lets say I have an account with the id of 001Z0000004XeWfIAK

I have tried

    $records[0] = new stdclass();
    $records[0]->FirstName = $FirstName;
    $records[0]->LastName = $LastName;
    $records[0]->Email = $Email;
    $records[0]->Phone = $Phone;
    $records[0]->MailingStreet = $MailingStreet;
    $records[0]->MailingCity = $MailingCity;
    $records[0]->MailingState = $MailingState;
    $records[0]->MailingPostalCode = $MailingPostalCode;
    $records[0]->MailingCountry = $MailingCountry;
    $records[0]->LeadSource = $LeadSource;
    $records[0]->AccountId = '001Z0000004XeWfIAK';

    $create = $mySforceConnection->create($records, 'Contact');

@ superfell

it returns this:

Array
(
    [0] => stdClass Object
        (
            [errors] => Array
                (
                    [0] => stdClass Object
                        (
                            [message] =>  A Household Contact's account must be a household.
                            [statusCode] => FIELD_CUSTOM_VALIDATION_EXCEPTION
                        )

                )

            [id] => 
            [success] => 
        )

)

But I am trying to associate a contact with an orginization

How to&Answers:

Contact has an AccountId field. So the code below assumes you have the account id in a variable called $accountId and $resource[0] is the contact you want to associate.

$records[0]->AccountId = $accountId
$mySforceConnection->update($records)

I don’t know php very well, but I think this would be close to correct.

Answer:

Ok, I am answering this myself because I cant mark Superfell comment as the answer. But his comment

“When you create the account, you need to set the recordTypeId to a record type that’s not the household recordtype. – superfell”

helped me get the answer.

Here is my final code to create a account and then a contact of that account.

    //First I create a simple account
    //With no recordTypeId specified it defaults to the the type I want

    $records[0] = new stdclass();
    $records[0]->Name = $Name;

    //Create a new orginization account
    $org = $mySforceConnection->create($records, 'Account');

After creating the Account, Salesforce returns a success message with the new AccountId

Array
(
    [0] => stdClass Object
        (
            [id] => 001Z0000004XfXcIAK
            [success] => 1
        )

)

Then I am able to create a contact and associate it with my new account

    $contact[0] = new stdclass();
    $contact[0]->FirstName = $FirstName;
    $contact[0]->LastName = $LastName;
    $contact[0]->Email = $Email;
    $contact[0]->Phone = $Phone;
    $contact[0]->MailingStreet = $MailingStreet;
    $contact[0]->MailingCity = $MailingCity;
    $contact[0]->MailingState = $MailingState;
    $contact[0]->MailingPostalCode = $MailingPostalCode;
    $contact[0]->MailingCountry = $MailingCountry;
    $contact[0]->LeadSource = $LeadSource;

    //This is where my problem was, Thanks again superfell
    //$organization_contact = My custom Salesforce contact type ID, E.G. recordTypeId
    $contact[0]->recordTypeId = $orginization_contact;

    //The AccountId is the account I want to associate this contact with.
    //AccountId was returned by Salesforce upon the creation of the account (See above)  
    $contact[0]->AccountId = $org[0]->id;

    $contact = $mySforceConnection->create($contact, 'Contact');

Thank you again to Jeremey and Superfell. Saved me hours.