Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

associate contact with account using salesforce api

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

like image 488
jpshayes Avatar asked Feb 09 '12 21:02

jpshayes


People also ask

How do I associate an account with a contact in Salesforce?

From Setup, enter Account Settings in the Quick Find box, then select Account Settings. Select Allow users to relate a contact to multiple accounts. You can use custom fields to capture unique information about relationships—for example, the best time to call a contact. Now is a good time to set that up.

Can Salesforce make API calls?

To call third-party APIs from your component's JavaScript code, add the API endpoint as a CSP Trusted Site. To call Salesforce APIs, make the API calls from your component's Apex controller. Use a named credential to authenticate to Salesforce.

Can Salesforce Call external API?

Salesforce REST API supports JSON and XML. Rest APIs that are available in Salesforce (or those that are custom written by developers) can be used by external applications to connect to salesforce to get data (records, field values) or to put (update) data.


2 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.

like image 62
Jeremy Ross Avatar answered Oct 19 '22 23:10

Jeremy Ross


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.

like image 34
jpshayes Avatar answered Oct 20 '22 01:10

jpshayes