function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
momorganmomorgan 

Field update trigger - what am I missing?

Hello all.

 

This is the most simple example of something I'm trying (and failing) to achieve with triggers:

 

 

trigger AccountOriginalName on Contact (before insert) { for (Contact c : Trigger.new){ c.Account_Original_Name__c = c.Account.Name; } }

 

So, when an contact is created, a field on the contact is populated with the name of the associated account. Except it isn't - it comes out null. Why's that, then?

 

Thanks

 

 

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

Ok - this seems to work. For this example, I update a custom field called Spouse Name, but you would just change that to be the field you want to update. If you wanted to reference other Account Fields, you would need to add them to SOQL statement, and then use a separate get statement with the Account field name you want:

 

 

trigger AccountOriginalName on Contact (before insert){

 

//A set is unique, so by adding the Account id to a set, we can be sure we don't query for a parent twice.

Set<String> AccountIDs = new Set<String>();

 

//Loop through the Contacts being updated and add the Account Id to the set

for (contact c : trigger.new)

{

AccountIDs.add(c.AccountId);

}

 

/* Here's where we create the map by executing a query for all accounts in the set created above. A Map consists of a key (in this case the AccountId) and a value - in this case an Account Object. We can lookup an Account in the map by using the Contact's Accountid as the lookup key. We will then loop back through the contacts being updated and extract the account name based on the Account id of each Contact using one SOQL query

*/

Map<ID, Account> AcctMap = new Map<ID, Account>([select id, name from account where Id in : AccountIDs]);

 

//Now we loop back through the Contacts, and use the 'get' functionality of maps to lookup the Account name based on the AccountId of the Contact and here set the Spouse Name as an example..

for (Contact c : trigger.new) {

c.Spouse_Name__c = AcctMap.get(c.AccountId).Name;

}

}

 

 

 

Message Edited by BritishBoyinDC on 07-15-2009 11:42 AM

All Answers

OnkiOnki

Hi

Try this

 

trigger AccountOriginalName on Contact (before insert) {
    for (Contact c : Trigger.new)
    {
        Account A = [Select Name from Account where Id =: c.AccountId];
        c.FirstName= A.Name;
    }
}

 

 

May be 

c.FirstName= C.Account.Name; not support in trigger .

BritishBoyinDCBritishBoyinDC

While that will work, you'll quickly break the Governor limits - you'll need to put the Account Ids into a set and then build a map that enable you to execute one single query for all accounts and then update that field as necessary.

 

You can see an example of how that logic works here - it's worth understanding as it works for most situations like this.

 

However, for this example, you can just do that with workflow I believe - workflow and field updates can reference values from linked objects so write a field update to use a formula that sets the custom field = Account Name, and trigger it via a normal workflow rule.

momorganmomorgan

Yes - perhaps that's my fault for oversimplifying the example... I am trying to avoid using an SOQL statement as far as possible because of the governor limits.

 

True, the workflow approach works well in this case. When you look at the trigger approach initially it's surprising that it's not as straightforward as workflow and/or formula fields.

 

Regarding building a map - I've looked at the other posts here and in the doco too, but it's still not clear to me how this could be applied in this simple example while still keeping database interaction to a minimum. Would anyone be kind enough to sketch out a very simple example for the benefit of those of us struggling to grasp this approach at the top level?

 

Thanks again.

BritishBoyinDCBritishBoyinDC

Ok - this seems to work. For this example, I update a custom field called Spouse Name, but you would just change that to be the field you want to update. If you wanted to reference other Account Fields, you would need to add them to SOQL statement, and then use a separate get statement with the Account field name you want:

 

 

trigger AccountOriginalName on Contact (before insert){

 

//A set is unique, so by adding the Account id to a set, we can be sure we don't query for a parent twice.

Set<String> AccountIDs = new Set<String>();

 

//Loop through the Contacts being updated and add the Account Id to the set

for (contact c : trigger.new)

{

AccountIDs.add(c.AccountId);

}

 

/* Here's where we create the map by executing a query for all accounts in the set created above. A Map consists of a key (in this case the AccountId) and a value - in this case an Account Object. We can lookup an Account in the map by using the Contact's Accountid as the lookup key. We will then loop back through the contacts being updated and extract the account name based on the Account id of each Contact using one SOQL query

*/

Map<ID, Account> AcctMap = new Map<ID, Account>([select id, name from account where Id in : AccountIDs]);

 

//Now we loop back through the Contacts, and use the 'get' functionality of maps to lookup the Account name based on the AccountId of the Contact and here set the Spouse Name as an example..

for (Contact c : trigger.new) {

c.Spouse_Name__c = AcctMap.get(c.AccountId).Name;

}

}

 

 

 

Message Edited by BritishBoyinDC on 07-15-2009 11:42 AM
This was selected as the best answer
momorganmomorgan
Spot on - that works a treat. Thanks again.
JvVoorstJvVoorst

Hi,

 

I see you are very gifted in writing Apex code could you get me started? I'm totally stuck trying to create my first Apex Trigger. It should be a very basic one but I can't figure out how to write it.

 

What I want to do is:

 

On Account

When the custom field "Collectief_Code" contains a specific value (for instance AJG67FGK) and the custom field "Collectief" is empty. I want to update the custom field "Collectief" with a specific value (for instance VipTel)

 

Should be easy does it so I'm quite embarrassed to say I don't have a clue..

 

Help is much appreciated!

BritishBoyinDCBritishBoyinDC

Ah, I've just done it a lot...

 

I would first ask if you have tried to doing this via workflow? Looks like a good use case?

 

If you can't for some reason, it would look something like this:

 

 

trigger taexample on Account (before insert, before update) {

for (Account a: Trigger.New) {

if (a.Collectief_Code.equals('AJG67FGK') && a.Collectief == null) {
a.Collectief = 'VipTel';
}

}


}