使用salesforce api将联系人与帐户相关联

时间:2012-02-09 21:11:18

标签: php api salesforce

我是使用salesforce api的新手。我已经下载了saleforce / php工具包,并且能够从我服务器上的webform成功创建联系人和帐户。

要创建联系人,我正在执行以下操作:

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

要创建帐户,我正在执行以下操作

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

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

有人能举例说明我如何将联系人与帐户关联起来吗?

我在表单上有一个复选框,询问这是否是一个组织。如果用户选中此框,我想创建一个包含部分数据的组织帐户,并创建与某些数据的联系并将这两个数据关联起来。

我不是在寻找一个完整的工作示例,而是更多的只是让我指向正确的方向。

假设我有一个ID为001Z0000004XeWfIAK

的帐户

我试过了

    $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

它返回:

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] => 
        )

)

但我试图将联系人与orginization

联系起来

2 个答案:

答案 0 :(得分:6)

联系人有一个AccountId字段。因此,以下代码假定您在名为$accountId的变量中拥有帐户ID,而$resource[0]是您要关联的联系人。

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

我不太了解php,但我认为这将接近正确。

答案 1 :(得分:6)

好的,我自己也在回答这个问题,因为我不能将Superfell的评论标记为答案。但他的评论

“当您创建帐户时,需要将recordTypeId设置为不属于家庭记录类型的记录类型。 - superfell”

帮助我得到答案。

以下是我创建帐户的最终代码,然后是该帐户的联系人。

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

创建帐户后,Salesforce将使用新的AccountId

返回成功消息
Array
(
    [0] => stdClass Object
        (
            [id] => 001Z0000004XfXcIAK
            [success] => 1
        )

)

然后我可以创建联系人并将其与我的新帐户相关联

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

再次感谢Jeremey和Superfell。节省了我几个小时。