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
Dipti DDipti D 

Need help with trigger

Hi All, I need a help with the following trigger.
I have the below trigger to update Case's adrress fields with the account's address fields as soon as a new Case is created.
I have to add the following requirement as well to it and do not know how to do it. Can some one help me? 
What need to be added: On Account object - Sales agent will collect 3 phone numbers - Home Phone, Work Phone & Mobile Phone - and we have another picklist field as "Best Number to Call" - with picklist values - Home, Work, Mobile.
If Agent select - Home - for "Best Number to Call" , Home Phone need to be carried over to Case Phone.
If Agent Selects - Work - for "Best Number to Call" , Work Phone need to be carried over to Case Phone.
If Agent select - Mobile - for "Best Number to Call" , Mobile Phone need to be carried over to Case Phone.
Case has only one Phone field. This is the tricky part for this trigger and I need help on this.
Thank you!

    public static void updateCasesWithAccountData(List<Case> newCases){
        Set<Id> accIds = new Set<Id>();
        List<Case> lstCases = new List<Case>();
        for(Case objCase : newCases){
            accIds.add(objCase.AccountId);
        }
        Map<Id, User> MapUserIDToUser =  new Map<Id, User>([Select Id, Profile.Name from User]);
        Map<ID, Account> MapAccIdtoAccount = new Map<ID, Account>([Select Id, Street__c, City__c, State__c, Zip__c from Account where Id IN:accIds]);
        for(Case objCase : newCases){
            if(MapUserIDToUser.get(objCase.ownerId).Profile.Name == 'Oncor Sales Agent' && MapAccIdtoAccount.containsKey(objCase.AccountId)){
                Case oCase = new Case(Id = objCase.Id);
                    oCase.Address_Line1__c = MapAccIdtoAccount.get(objCase.AccountId).Street__c ;
                    oCase.City__c = MapAccIdtoAccount.get(objCase.AccountId).City__c ;
                    oCase.Zip__c = MapAccIdtoAccount.get(objCase.AccountId).Zip__c ;
                lstCases.add(oCase);
            }
        }
        if(lstCases.size()>0){
            update lstCases;
        }
Best Answer chosen by Dipti D
ManojjenaManojjena
Hi Deepthi,
Replace  the if else with below code and add the field in account query .
If(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c != null){
    if(oMapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c =='Home Phone'){
		objCase.Phone__c=MapAccIdtoAccount.get(objCase.AccountId).Home_Phone__c; 
    }else if(oMapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c =='Work Phone'){
		objCase.Phone__c=MapAccIdtoAccount.get(objCase.AccountId).Work_Phone__c; 
    }else if(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c  =='Mobile Phone'){
	   objCase.Phone__c=MapAccIdtoAccount.get(objCase.AccountId).Mobile _Phone__c;
    }
}
Let me know any if it helps!!
Thanks
Manoj


 

All Answers

ManojjenaManojjena
Hi Deepthi,
try with below code ,I think in your trigger event is after insert ,You can change to before insert it will work .
 
public static void updateCasesWithAccountData(List<Case> newCases){
        Set<Id> accIds = new Set<Id>();
        List<Case> lstCases = new List<Case>();
        for(Case objCase : newCases){
		    if(objCase.AccountId != null){
			   accIds.add(objCase.AccountId);
			}
        }
        Map<Id, User> MapUserIDToUser =  new Map<Id, User>([Select Id, Profile.Name from User]);
        Map<ID, Account> MapAccIdtoAccount = new Map<ID, Account>([SELECT Id, Street__c, City__c, State__c, Zip__c,Home_Phone__c,Work_Phone__c,Mobile _Phone__c  FROM Account WHERE Id IN:accIds]);
        for(Case objCase : newCases){
            if(MapUserIDToUser.get(objCase.ownerId).Profile.Name == 'Oncor Sales Agent' && MapAccIdtoAccount.containsKey(objCase.AccountId)){
				objCase.Address_Line1__c = MapAccIdtoAccount.get(objCase.AccountId).Street__c ;
				objCase.City__c = MapAccIdtoAccount.get(objCase.AccountId).City__c ;
				objCase.Zip__c = MapAccIdtoAccount.get(objCase.AccountId).Zip__c ;
				if(objCase.	Best_Number_to_Call__c =='Home Phone'){
				  objCase.Phone__c=MapAccIdtoAccount.get(objCase.AccountId).Home_Phone__c; 
				}else if(objCase.Best_Number_to_Call__c =='Work Phone'){
				objCase.Phone__c=MapAccIdtoAccount.get(objCase.AccountId).Work_Phone__c; 
				}else if(objCase.Best_Number_to_Call__c =='Mobile Phone'){
				   objCase.Phone__c=MapAccIdtoAccount.get(objCase.AccountId).Mobile _Phone__c;
				}
            }
        }
        Try to check the API name of below fields.
		Phone__c
		Best_Number_to_Call__c
		Home_Phone__c
		Work_Phone__c
		Mobile _Phone__c



If you think you need in after insert then implement above logic in your code .

Let me know if it helps !!
Thanks
Manoj
 
Dipti DDipti D
Hi Manoj. Thanks for your prompt response. I will work on this and update you shortly. 
Thanks!
Dipti DDipti D
Hi Manoj, 
In Line 16. 18 & 20 - we are referencing the Best_Number_to_Call__c for sObject case. But that field is on Account. i.e. When Sales agent fills these 3 numbers and selects this picklist field to one value, that phone number need to be carried to Case. How do I correct this? Thanks!
ManojjenaManojjena
Hi Deepthi,
Replace  the if else with below code and add the field in account query .
If(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c != null){
    if(oMapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c =='Home Phone'){
		objCase.Phone__c=MapAccIdtoAccount.get(objCase.AccountId).Home_Phone__c; 
    }else if(oMapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c =='Work Phone'){
		objCase.Phone__c=MapAccIdtoAccount.get(objCase.AccountId).Work_Phone__c; 
    }else if(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c  =='Mobile Phone'){
	   objCase.Phone__c=MapAccIdtoAccount.get(objCase.AccountId).Mobile _Phone__c;
    }
}
Let me know any if it helps!!
Thanks
Manoj


 
This was selected as the best answer
Dipti DDipti D
Hi Manoj, 
I see the following error. Sorry to bother you.
 User-added image
Dipti DDipti D
Hi Manoj, 
I figured out the reason for the error. It is because I was not calling the phone fields in SOQL.
Now the issue is resolved and working great. 
Thank you!