Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- General Development
- :
- Updating a Contact's Account from a form or trigge...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Updating a Contact's Account from a form or trigger (or anything but manually in SalesForce )?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 04:52 PM
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!
Solved! Go to Solution.
Re: Updating a Contact's Account from a form or trigger (or anything but manually in SalesForce )?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-02-2013 10:37 PM
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/apexco
Re: Updating a Contact's Account from a form or trigger (or anything but manually in SalesForce )?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-03-2013 04:49 AM
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!
Re: Updating a Contact's Account from a form or trigger (or anything but manually in SalesForce )?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
01-03-2013 09:29 AM
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!

