You need to sign in to do that
Don't have an account?

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;
}
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;
}
Replace the if else with below code and add the field in account query . Let me know any if it helps!!
Thanks
Manoj
All Answers
try with below code ,I think in your trigger event is after insert ,You can change to before insert it will work .
If you think you need in after insert then implement above logic in your code .
Let me know if it helps !!
Thanks
Manoj
Thanks!
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!
Replace the if else with below code and add the field in account query . Let me know any if it helps!!
Thanks
Manoj
I see the following error. Sorry to bother you.
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!