Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am new to creating triggers and would love some assistance in creating a trigger that can mirror a picklist values.

I have two identical picklists called "Test_Status__c" on both the Account object and on the Contact object that contain the following values:

Prospect Member Probation Emeritus Terminated

I am looking to create a trigger where if say "Prospect" is selected as a value on the Account, that its Contacts' picklist fields would display "Prospect" as well. I am looking for this information to roll down from the Account and not up from the Contact.

I would create a formula field on the Contact object that would draw in the Account's picklist value, but I need the field on the Contact page to stay editable.

I have tried to pick up some hints online of how to create this trigger and have come up with the following code thus far:

trigger FieldUpdate on Contact (before insert, before update){
    Map<String, String> myPickListMap = new Map<String, String> {'Prospect'=>'Prospect', 'Member'=> 'Member','Probation'=>'Probation','Emeritus'=>'Emeritus','Terminated'=>'Terminated'};

    for (Account a : Trigger.new) || for (Contact c : Trigger.new) {
        if(a.Test_Status__c != null) {
            c.Test_Status__c = myPickListMap.get(a.Test_Status__c);
         }
    }
}
share|improve this question

2 Answers 2

Here is an implementation. The trigger needs to be on the Account because it is a change there that needs pushing to the Contacts. You could keep the map if the picklist values are not 1:1. There are a few more comments inline...

I'd like to draw particular attention to the code that checks if the picklist value on Account has changed or not. Without this, any change to an Account will cause the Last Modified Date of all child Contacts to change even though no change has taken place (which is confusing for the user). It will also consume governor limited DML and elapsed time for no reason.

// Insert not relevant as no child contacts then
trigger FieldUpdate on Account (before update) {

    // Avoid doing any SOQL unless the picklist changes
    Set<Id> accountIds = new Set<Id>();
    for (Account na : Trigger.new) {
        Account oa = Trigger.oldMap.get(na.Id);
        if (na.Test_Status__c != oa.Test_Status__c) {
            accountIds.add(na.Id);
        }
    }
    if (accountIds.size() > 0) {
        // Find the child contacts
        Contact[] contacts = [
                select Id, AccountId
                from Contact
                where AccountId in :accountIds
                ]);
        for (Contact c : contact) {
            // Copy new value including null (or add null guard)
            c.Test_Status__c = Trigger.newMap.get(c.AccountId).Test_Status__c;
        }
        update contacts;
    }
}
share|improve this answer

Some basics on triggers:

1) A trigger.new is very specific to an object that you are writing the trigger on.

In your case "account", for(contact a: trigger.new) on an account trigger would result in error.

My version of solving this problem :

2) If you want to map the values of Test_Status__c on contact to account, you simply equate both the fields

select all the contacts related to the account. This type of querying is called Relationship queries: http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_//relationships.htm

trigger test_trii on Account (after update) {
List<contact> contact_List = new List<contact>();
Map<Id,string> account_Contact_Map = new Map<Id,string>();
    for(account a: [select id,Test_Status__c ,(select id,accountId from contacts) from account where ID in: trigger.new]){
        if(a.contacts != null){
            for(contact c: a.contacts){
               c.Test_Status__c = a.Test_Status__c ;
               contact_List.add(c);
            }       
        }
    }    
    update contact_list;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.