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 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;
        }
    }
Best Answer chosen by Dipti D
JeffreyStevensJeffreyStevens
So your line where you're getting the Accounts - you want to only get the account if the Source__c is null - correct?

If that's the case - change this line....
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]);

to this...
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 AND Source__c == null]);


 

All Answers

JeffreyStevensJeffreyStevens
So your line where you're getting the Accounts - you want to only get the account if the Source__c is null - correct?

If that's the case - change this line....
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]);

to this...
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 AND Source__c == null]);


 
This was selected as the best answer
Dipti DDipti D

Thanks Jeffrey, I was able to figure that and do, but I very much appreciate your response. Thank you again!!