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

Need a small help with the following trigger
Need help with the following trigger helper class.
This is the trigger helper class where it would update certain fields from one object (Customer) to its child object (Case). I need to add a condition here. The fields like Address, City, Zip etc should be carried over to Customer onmy when Source__ is NULL.
Where exactly do I add this condition, so that when Source on the Customer has a value, the values are not carried over to the Case.
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, Product__c> MapProductIdToProduct = new map<Id, Product__c>([Select Id, Name from Product__c]);
Map<ID, Account> MapAccIdtoAccount = new Map<ID, Account>([Select Id, Street__c, City__c, State__c, Zip__c, Best_Number_to_Call__c, PersonHomePhone, Phone, Source__c, PersonMobilePhone, (Select Id from Contacts) from Account where Id IN:accIds]);
for(Case objCase : newCases){
if (MapAccIdtoAccount.containsKey(objCase.AccountId) && MapProductIdToProduct.containsKey(Objcase.Product__c) && ){ // if(MapUserIDToUser.get(objCase.ownerId).Profile.Name == 'Oncor Sales Agent' && ){
Case oCase = new Case(Id = objCase.Id);
oCase.ContactId = MapAccIdtoAccount.get(objCase.AccountId).Contacts[0].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 ;
If(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c != null){
if(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c =='Home'){
oCase.Phone__c = MapAccIdtoAccount.get(objCase.AccountId).PersonHomePhone;
}else if(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c =='Work'){
oCase.Phone__c = MapAccIdtoAccount.get(objCase.AccountId).Phone;
}else if(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c =='Mobile'){
oCase.Phone__c = MapAccIdtoAccount.get(objCase.AccountId).PersonMobilePhone;
}
}
lstCases.add(oCase);
}
}
if(lstCases.size()>0){
update lstCases;
}
}
This is the trigger helper class where it would update certain fields from one object (Customer) to its child object (Case). I need to add a condition here. The fields like Address, City, Zip etc should be carried over to Customer onmy when Source__ is NULL.
Where exactly do I add this condition, so that when Source on the Customer has a value, the values are not carried over to the Case.
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, Product__c> MapProductIdToProduct = new map<Id, Product__c>([Select Id, Name from Product__c]);
Map<ID, Account> MapAccIdtoAccount = new Map<ID, Account>([Select Id, Street__c, City__c, State__c, Zip__c, Best_Number_to_Call__c, PersonHomePhone, Phone, Source__c, PersonMobilePhone, (Select Id from Contacts) from Account where Id IN:accIds]);
for(Case objCase : newCases){
if (MapAccIdtoAccount.containsKey(objCase.AccountId) && MapProductIdToProduct.containsKey(Objcase.Product__c) && ){ // if(MapUserIDToUser.get(objCase.ownerId).Profile.Name == 'Oncor Sales Agent' && ){
Case oCase = new Case(Id = objCase.Id);
oCase.ContactId = MapAccIdtoAccount.get(objCase.AccountId).Contacts[0].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 ;
If(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c != null){
if(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c =='Home'){
oCase.Phone__c = MapAccIdtoAccount.get(objCase.AccountId).PersonHomePhone;
}else if(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c =='Work'){
oCase.Phone__c = MapAccIdtoAccount.get(objCase.AccountId).Phone;
}else if(MapAccIdtoAccount.get(objCase.AccountId).Best_Number_to_Call__c =='Mobile'){
oCase.Phone__c = MapAccIdtoAccount.get(objCase.AccountId).PersonMobilePhone;
}
}
lstCases.add(oCase);
}
}
if(lstCases.size()>0){
update lstCases;
}
}
If that's the case - change this line....
to this...
All Answers
If that's the case - change this line....
to this...
Thanks Jeffrey, I was able to figure that and do, but I very much appreciate your response. Thank you again!!