Reply
KMO
Contributor
KMO
Posts: 5
0
Accepted Solution

Updating a Contact's Account from a form or trigger (or anything but manually in SalesForce)?

Hello,

I have a form that allows our users to enter a new Contact, and they need to be able to add the associated Account somehow.

At first I tried using PHP to do an upsert as below:

 

$sObject1 = new stdClass();

$sObject1->FirstName = 'MyName';
$sObject1->LastName = 'MyLastName';
$sObject1->Email = 'test@test.com';
$sObject1->AccountName = 'MyContactsAccount';

$response = $mySforceConnection->upsert('Email', array($sObject1), 'Contact');

 

I understand now, though, that I canot update (or upsert or whatever) a field that is of the type Lookup as the Account is for a Contact. First question, is that correct?

 

So I spent several hours researching alternatives. I came across infomation that suggests I should use a trigger to do so instead. The following trigger works great.  (Please note that I just simplified my code to isolate the problem...I don't really want to set the contact's last name ot the account name!)

 

trigger SetAccountContactField on Contact (before insert, before update) {
for (Contact a : Trigger.new) {
if(a.FirstName=='MyName'){
a.LastName = 'MyContactsAccount'; }
}
}

 

But when I try to actually populate the AccountName I get stuck. (I have tried a.Account.Name, a.AccountID, a.Account, a.AccountName...Everything I can think of.) I am starting to fear that there is a similar issue with updating Lookup types here? PLEASE tell me I am just having syntax problems and there is a way to update the Account for a Contact automatically?


trigger SetAccountContactField on Contact (before insert, before update) {
for (Contact a : Trigger.new) {
if(a.FirstName=='MyName'){
a.Account.Name = 'MyContactsAccount'; }
}
}

 

I really need to find a way to update the Account for a Contact automatically. Thank you in advance for any thoughts or ideas!

Regular Contributor
Saurabh Dhoble
Posts: 138
0

Re: Updating a Contact's Account from a form or trigger (or anything but manually in SalesForce)?

Look at this example - this is getting all the contacts for an account in a trigger, using SOQL queries. you need to reverse the query :-

 

trigger SetAccountContactField on Contact (before insert, before update) 
{
    Account [] accs = [select id,name from account where id IN :Trigger.New];

    for (Contact a : Trigger.new) {
      //Loop through the array above to find your Account ID - this is the AccountId field
    } 

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_qs_HelloWorld.htm|StartTopic=Content%2Fapex_qs_HelloWorld.htm|SkinName=webhelp

KMO
Contributor
KMO
Posts: 5
0

Re: Updating a Contact's Account from a form or trigger (or anything but manually in SalesForce)?

Thanks so much for the suggestion.  I am not having problems querying the contact to get the account, though, just a problem setting it to be something different.  Maybe I am just misunderstanding your suggestion?

 

If I have just a single contact in my whole SalesForce database, and I would like to set that contact's account to be "MyOnlyAccount" but do not want to do so manually in SalesForce, is there any way to do it?  A trigger, SOQL, anything?

 

Thanks so much!

KMO
Contributor
KMO
Posts: 5
0

Re: Updating a Contact's Account from a form or trigger (or anything but manually in SalesForce)?

Finally found the answer!  I can simply do as I had hoped by upserting the AccountID instead of the AccountName.

 

So back to my original approach...

 

$sObject1 = new stdClass();

$sObject1->FirstName = 'MyName';
$sObject1->LastName = 'MyLastName';
$sObject1->Email = 'test@test.com'; 

(write the code to find the account ID based on what account was entered)
$sObject1->AccountID = 'MyContactsAccountID';

$response = $mySforceConnection->upsert('Email', array($sObject1), 'Contact');

 

Thanks!