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
Leigh0725Leigh0725 

convert to bulk trigger - stumped!

I am at a loss on what to do.  So we created this update trigger and it worked great in the sandbox.  Pushed it over to production with no issues.  We went through some test records and everything was great.  Then, we try to do a data load.  That's when we found out about bulk triggers. Here is our trigger.  We have a master detail relationship with Account and Program (client__c housing the account id).  Then Program has a master detail relationship with Reward (rm_detail_link__c housing the program id).  Any help would be great.  I've been reading on the maps, but am not quite sure how to do the maps since we need to access 4 objects - User, Account, Program and Reward.  Thanks!

 

trigger RMrewardupdate on Reward__c (before update) {
    for(Reward__c rs: trigger.new){
        ID pID = [select RM_Detail_Link__c from Reward__c where id = :rs.id].rm_detail_link__C;
        ID aID = [select client__c from program__c where id = :pid].client__c;
        ID rmID = [select relationship_manager__c from account where id = :aid].relationship_manager__C;
        
        If (rmID  != null) {
            string RMemail = [select email from user where id = :rmid].email;
            rs.rm_user_id__c = rmID;
            rs.rm_email__c = RMemail;
            
            ID rrmID = [select managerid from user where id = :rmid].managerid;
    
            If (rrmID  != null) {
                string RRMemail = [select email from user where id = :rrmid].email;
                rs.rrm_email__c = RRMemail;
            }
        }
    }
}

ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi,

The issue you are facing during bulk upload is because, there are queries in your 'for' loop, which is hitting the maximum number of query limit.

You need to store all the id(values) in 'set' and then fire the query for each 'set' collection.

Leigh0725Leigh0725

Yes, I am aware of having to move the queries out of the for loop.  How would I incorporate the 'sets'?  I tried reading through the documentation, but am still a little confused.  With sets and lists, how do we know which ones to use?  Do I even need to try and use a map, or can be I okay with the sets outside of the for loop.  Any assistance with code modficiation would be greatly appreciated. Thanks!

ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi,

 

trigger RMrewardupdate on Reward__c (before update) {
   
    Set<Id> setRewardDetailId = new Set<Id>();
    Set<Id> setProgramId = new Set<Id>();
    Set<Id> setRelManagerId = new Set<Id>();
    Map<Id, program__c> mapProg = new Map<Id, program__c>();
    Map<Id, account> mapAccnt = new Map<Id, account>();
    Map<Id, user> mapUser = new Map<Id, user>();
    
    for(Reward__c rs: trigger.new)
    {
        setRewardDetailId.add(rs.rm_detail_link__C);
        lstRewards.add(rs);
    }
    for(program__c p : [select Id, client__c from program__c where id IN : setRewardDetailId])
    {
        mapProg.put(p.Id, p);
        setProgramId.add(p.client__c);
    }
    for(account a : [select Id, relationship_manager__c from account where id IN : setProgramId])
    {
        mapAccnt.put(a.Id, a);
        setRelManagerId.add(a.relationship_manager__c);
    }
    for(user u : [select managerid, email, Id from user where id IN : setRelManagerId])
    {
        mapUser.put(u.Id, u);
    }
    
    for(Reward__c r: trigger.new)
    {
        string strProg = mapProg.get(r.rm_detail_link__c).client__c;
        ID Accnt;
        if(strProg != null && strProg != '')
        {
            strAccnt = mapAccnt.get(strProg).relationship_manager__c;
        }
        ID RMemail = mapUser.get(strAccnt).email;
        rs.rm_user_id__c = rmID;
        rs.rm_email__c = RMemail;
        ID rrmID = mapUser.get(strAccnt).managerid;
        string RRMemail = mapUser.get(rrmID).email;
        rs.rrm_email__c = RRMemail;
    }
}

Hope this helps!