I hope someone can help...?

I have this test code that's working on all record types. I would like to modify it in a way that only works on specific Account Record Type and updates fields on all Contacts associated with the Account.

I.E. If I change one of the picklists on Account with Record type ID: 01220000000JWFn, all corresponding picklists will update on all Contacts under the Account.

trigger SFDC_TEST on Account (before update) {

List <Account> acct=[select id,(select id,firstname from contacts) from Account where id=:trigger.new[0].id];

for(Account a:acct)
        for(Contact c:a.contacts)





update c;



Ok. First of all, you need to bulkify your trigger or it'll fail on mass updates.

you should write something like this:

trigger SFDC_TEST on Account(before update){

    Id recTypeId = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('Name of RecorType').getRecordTypeId(); //using it like this you won't have problems to deploy (and it'll save you a query)
    List<Account> accountsToVerify = new List<Account>();

   for(account a : trigger.new){
       if(a.RecordTypeId == recTypeId  //checks if the account has the correct RecordType
          && (a.PURE_Importance__c != trigger.oldMap.get(a.Id).PURE_Importance__c
          || a.WAA_Importance__c != trigger.oldMap.get(a.Id).WAA_Importance__c
          || a.LE_Miami_Importance__c != trigger.oldMap.get(a.Id).LE_Miami_Importance__c
          || a.PURE_Agent__c != trigger.oldMap.get(a.Id).PURE_Agent__c
          || a.LE_Miami_agent__c != trigger.oldMap.get(a.Id).LE_Miami_agent__c
          || a.WAA_agent__c != trigger.oldMap.get(a.Id).WAA_agent__c)){ //checks if the picklist is changed

   if(!accountsToVerify.isEmpty()){ //will only process if there's a need to.
      List<Contact> contactsToUpdate = [SELECT Id, AccountId, FirstName FROM Contact WHERE AccountId IN :accountsToVerify];
      for(Contact c : contactsToUpdate){
          Account a = trigger.newMap.get(c.AccountId);

       update contactsToUpdate;

| improve this answer | |
  • Thanks Fernando, the code is throwing a problem at line 14 (List<Contact> contactsToUpdate = [SELECT Id, AccountId, FirstName FROM Contacts WHERE AccountId IN :accountsToVerify]; sObject type 'Contacts' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. – Lakis Juzwa Dec 3 '15 at 13:02
  • Oops. I added and extra 's'. I just edited the answer. It should be correct. – Fernando Gavinho Dec 3 '15 at 13:09
  • OK. Now in line 8 <<&& a.PicklistField != trigger.oldMap.get(a.Id).PicklistField){ //checks if the picklist is changed>> I have a problem: Invalid field PicklistField for SObject Account. Also, is there any space where I need to put the Account record type name/ID? I'm not good with the code and learning to understand the basics. Thanks a lot. L – Lakis Juzwa Dec 3 '15 at 13:23
  • You have to change 'Name of the RecorType' with the name of the RecordType you need. Also, you have to change 'picklistfield' with the name of the picklist field that'll trigger the change. If you there's more than one, let me know the names and I'll edit the example so you can copy – Fernando Gavinho Dec 3 '15 at 13:47
  • Yes please. I have the following picklists: PURE_Importance__c, LE_Miami_Importance__c, WAA_Importance__c, PURE_Agent__c, LE_Miami_agent__c, WAA_agent__c. Many thanks. – Lakis Juzwa Dec 3 '15 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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