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
ToyetoyetoyeToyetoyetoye 

Trigger: populate custom field with ContactRole parent Acct Name

Working on a trigger/query to populate a field on a Case with a ContactRole's parent Acct name. Anybody using something similar.

 

Thanks

mikefmikef

At this time you can not add a trigger to the Case Contact Role object so one event you can get this info from is the Case Update. 

 

Your case trigger will need to run two queries to get the info you want.

 

Set<Id> caseIds = new Set<Id>();
Map<Id,Id> caseIdAccIdMap = new Map<Id,Id>();
Map<Id, String> parentAccNameMap = new Map<Id, String>();

for(Case c : trigger.new){
   caseIds.add(c.Id);

}

for(CaseContactRole ccr : {Select c.ContactId, c.Contact.AccountId, c.CasesId From CaseContactRole c where CaseId in : caseIds]){
  caseIdAccIdMap.put(ccr.CaseId, ccr.Contact.AccountId);

}

for(Account acc : [select Parent.Name, Id from Account where Id in : caseIdAccIdMap.values()]({
   parentAccNameMap.put(acc.Id, acc.Parent.Name);
}

for(Case c : trigger.new){
   c.[your_custom_field__c] = parentAccNameMap.get(caseIdAccIdMap.get(c.Id));

}

 This will get you started but there is a lot of work to do to make this trigger production ready.

 

Also please note I didn't test this at all and just wrote it in the post so you will have work there.

 

Also please note you can have more case contact roles then one, and you might want to save this into one field, but add to a custom object linked to case.

 

good luck.

 

ToyetoyetoyeToyetoyetoye

Hi Mike...thanks for the prompt response. I planned to run the trigger on the Case object (not CaseContactRole) after insert or update. Would that approach not work?

 

Thanks again for the code. I will work on adapting it.

mikefmikef

Yes you have to run this on case update, as you don't have access to the CaseContactRole object.

 

Remember you will have the use case of many CaseContactRoles under a Case so you have to code for that.

Dumping the Parent Account value in a Case field might not be the best solution. You might want to rebuild the CaseContactRole related list in your won custom object. Think of it as a wrapper custom object, and then your new CaseContactRole__c related list will have a field for the Parent Account.

 

Hope that helps.

ToyetoyetoyeToyetoyetoye

Ah. Hadn't thought of rebuilding CaseContactRole object.

 

By the way I should add that there are actually multiple fields on the Case corresponding to the Contact Roles Account to be populated for which I was going to apply a series of "if" statements. But your rebuild idea sounds like I could just use a formula (cross object) to populate them.

 

Thanks Mike!