0

I'm trying to populate a long text field with phones from many contacts, but I don't know how to do it. I want to populate a field called "Authorized Contacts" with all phone numbers returned from a map whenever a case is inserted or updated.

trigger getAuthorizedContacts on Case (before insert, before update) {

Set<Id> CaseIds = new Set<Id>();
for(Case getIds : Trigger.New){
    CaseIds.add(getIds.Contract_Case__c);
}    

Map<Id, Junction_contact_contract__c> aContacts = new Map<Id, Junction_contact_contract__c>([SELECT Contact__r.Phone 
                                                                                                       FROM Junction_contact_contract__c
                                                                                                       WHERE Contract__c IN: CaseIds AND 
                                                                                                       Contact__r.RecordType.Name = 'Authorized Contact']);   
if(aContacts.size() > 0 ){
    for(Case a : Trigger.New){
        a.Authorized_contacts = aContacts. ;
    }
 }  
}  

Could you guys please help me to solve this? If you guys have any other suggestions, please let me know.

0

You can loop over the "aContacts" map values and create a string containing all phone numbers, then you can use it anywhere.

String phones = '';
for(Junction_contact_contract__c junction : aContacts.values()) {
   phones = phones + ' ' + junction.Contact__r.Phone;
}

if(aContacts.size() > 0 ){
    for(Case a : Trigger.New){
        a.Authorized_contacts = phones;
    }
}  
| improve this answer | |
  • Thank you so much! I can't thank you enough! – Matheus Antonio Mar 1 at 17:56

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.