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

Contruct utility class to populate the SOQL queries to be used in the apex class
Hello,
How can i do the SOQL query and save the result in utility class and further call the vaules populated in utility class
Thank you for suggestion
How can i do the SOQL query and save the result in utility class and further call the vaules populated in utility class
Trigger: trigger AccountTrigger on Account(before insert, before update) { AccountTriggerHandler handler = new AccountTriggerHandler(); handler.updateUser(Trigger.new); // Call method for update account field. } Trigger Handler Class: public class AccountTriggerHandler { //TO DO: Do the SOQL Call and store in utility class public void updateUser(List<Account> accList) { Set<Id> masterIds = new Set<Id>(); for(Account accObj : accList) { if(accObj.Master__c != null) masterIds.add(accObj.Master__c); } Map<Id,Master__c> masterMap = new Map<Id,Master__c>([Select Id, test_user1__c from Master__c where Id IN : masterIds]); for(Account acct : accList) { if(masterMap.containsKey(acct.Master__c)) { Master__c masterObj = masterMap.get(acct.Master__c); if(acct.test_user1__c == null) acct.test_user1__c = masterObj.test_user1__c; } } } }
Thank you for suggestion
To use the SOQLQueryUtility class in triggerHandler. Please create a SQOLQueryUtility Class and implement the below mentioned code in trigger handler. (highlighted in Bold)
SOQLQueryUtiltiy Class -
public class SOQLUtilityMaster{
public static List<Master__c> fetchMasterRecords(Set<Id> masterId){
return [Select Id, Name from Master__c where Id IN : masterId];
}
}
Updated TriggerHandler -
public class AccountTriggerHandler
{
//TO DO: Do the SOQL Call and store in utility class
public void updateUser(List<Account> accList)
{
Set<Id> masterIds = new Set<Id>();
for(Account accObj : accList)
{
if(accObj.Master__c != null)
masterIds.add(accObj.Master__c);
}
Map<Id,Master__c> masterMap = new Map<Id,Master__c>(SOQLUtilityMaster.fetchAccountRecords(masterIds));
for(Account acct : accList)
{
if(masterMap.containsKey(acct.Master__c))
{
Master__c masterObj = masterMap.get(acct.Master__c);
if(acct.test_user1__c == null)
acct.test_user1__c = masterObj.test_user1__c;
}
}
}
}
Let me know if there are any questions/comments.
Thanks