Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

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 contact with some of the data and associate the two.

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

Let's 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');

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

1 Answer

+1 vote
by (32.1k points)
edited by

When you create the account, you need to set the recordTypeId to a record type that's not the household record-type. 

Here is the final code for creating an account and contact of that account:

    $records[0] = new stdclass();

    $records[0]->Name = $Name;

    //Create a new orginization account

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

After an account is created, Salesforce delivers a success message with the new AccountId

Array

(

    [0] => stdClass Object

        (

            [id] => 001Z0000004XfXcIAK

            [success] => 1

        )

)

Now, I have created contact and associated it with my new account using the following code:

       $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');

 

Enroll in this Salesforce Certification to start your journey today!

 

Browse Categories

...