2

Can somebody please guide me through creation of a trigger that would update a text field on account from picklist on a child custom object or objects?

The issue is that there can be two or even more custom objects under the account with different values which I would all like to see (separated by "," or ";" in my text field) on account.

I could use roll up summary field to summarize values from custom objects on account and run a report but I really would like to have it all in one text box.

EDIT

This is what I have tried so far

trigger UpdateMultiListAccountClassification on Press__c (after insert, after update) {
     Map<Id, String> accIds = new Map<Id, String>(); 
     for(Press__c p : trigger.new) { 
          accIds.put(p.Account__c,String.valueOf(p.Format__c)); 
     }
     List<Account> acctsToUpdate = [Select Id,Account_Press_Classification__c from Account where id in:accIds.keySet()]; 
     for(Account a : acctsToUpdate) { 
          a.Account_Press_Classification__c += ';'+accIds.get(a.Id); 
     }
     update acctsToUpdate; 
}

EDIT 2

This trigger works fine for inserts and triggers, I am hoping to modify it so it updates the Account picklist accordingly if the Press__c object is deleted.

  • 4
    Hi Pawel, welcome to SFSE. Could you tell us what you have tried so far. If you provide some code of your attempt, people will be much more likely to help. As it is now, it appears as though you are just asking people to write a trigger for you, which is likely to be skipped over. Give it a shot, and post your code, even if it's not working, people can help guide you to get it working. – Chris Duncombe Oct 8 '13 at 13:44
  • This is the code: trigger UpdateMultiListAccountClassification on Press__c (after insert, after update) { Map<Id, String> accIds = new Map<Id, String>(); for(Press__c p : trigger.new) { accIds.put(p.Account__c,String.valueOf(p.Format__c)); } List<Account> acctsToUpdate = [Select Id,Account_Press_Classification__c from Account where id in:accIds.keySet()]; for(Account a : acctsToUpdate) { a.Account_Press_Classification__c += ';'+accIds.get(a.Id); } update acctsToUpdate; } – Pawel Jan 30 '14 at 14:19
  • but it does not update the record then child object gets deleted or does not meet criteria – Pawel Jan 30 '14 at 14:20
  • I have added the code to the question – Chris Duncombe Jan 30 '14 at 14:24
  • Could you explain a bit more of what is happening now when the trigger runs? – Chris Duncombe Jan 30 '14 at 14:26
1

remember you can access the child objects by using their list name, one simple way to construct your query is using the schema browser from eclipse, for example, if I wanted to access all the contacts that belong to one account this would be my query:

SELECT Id, (SELECT Id, Name FROM Contacts) FROM Account

and you only need to assign that query result to a list of accounts

Hope it make sense to you!

|improve this answer|||||
0

Did this is a text editor, so it hasn't been compiled, but something like this should work.

trigger UpdateMultiListAccountClassification on Press__c (after insert, after update, after delete) {
     Map<Id, String> accIds = new Map<Id, String>(); 

     list<Press__c> listToUse = trigger.isDelete() ? trigger.old : trigger.new;
     for(Press__c p : listToUse) { 
          accIds.put(p.Account__c,String.valueOf(p.Format__c)); 
     }

     List<Account> acctsToUpdate = [Select Id,Account_Press_Classification__c from Account where id in:accIds.keySet()]; 

     if(trigger.isInsert || trigger.isUpdate){
         for(Account a : acctsToUpdate) { 
            a.Account_Press_Classification__c += ';'+accIds.get(a.Id); 
         }
     }

     if(trigger.isDelete){
        string target;
        for(Account a : acctsToUpdate){
            target = accIds.get(a.Id) + ';';
            a.Account_Press_Classification__c.replace(target, '');
        }
     }
     update acctsToUpdate; 
}

I used the String Class Replace() method here. You can see more about this method and other String methods here

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_String_replace.htm

|improve this answer|||||

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.