You need to sign in to do that
Don't have an account?
megan.burch
Dynamic Assignment in Trigger
Hi,
I'm writing a trigger that pulls user names from a custom object and writes them to a field on another custom object. There are up to 4 fields that could be updated. That all works, the problem is I want to do it for every name that is not the owner on the object I'm writing to and if conditions are too complicated since any of the names could be the owner. My trigger is below. Any suggestions?
trigger SharedMMTrigger on RM_Requests__c (before insert, before update) { //iterate through all requests passed to the trigger RM_requests__c[]requests = Trigger.new; for (RM_requests__c RM: requests){ { List <account> accounts = [select id, name, GeoJunction__r.Market_Assignments__c, GeoJunction__r.Market_Assignments__r.Market_Manager_Shared1__c, GeoJunction__r.Market_Assignments__r.Market_Manager_Shared2__c, GeoJunction__r.Market_Assignments__r.Market_Manager_Shared_3__c, GeoJunction__r.Market_Assignments__r.Market_Manager__c from account where id = :rm.account__c]; System.debug(' market assignment = ' + rm.account__c); for (account a : accounts){ if (a.geojunction__c !=null) if(rm.shared_market_Account__c == 'yes') { if (rm.owner.id != a.GeoJunction__r.Market_Assignments__r.Market_Manager_Shared1__c){ rm.Market_Manager_Shared1__c = a.GeoJunction__r.Market_Assignments__r.Market_Manager_Shared1__c; } else rm.Market_Manager_Shared1__c = a.GeoJunction__r.Market_Assignments__r.Market_Manager_Shared2__c; if (rm.owner.id != a.GeoJunction__r.Market_Assignments__r.Market_Manager_Shared2__c){ rm.Market_Manager_Shared2__c = a.GeoJunction__r.Market_Assignments__r.Market_Manager_Shared2__c; } else rm.Market_Manager_Shared2__c = a.GeoJunction__r.Market_Assignments__r.Market_Manager_Shared_3__c; if (rm.owner.id != a.GeoJunction__r.Market_Assignments__r.Market_Manager_Shared_3__c){ rm.Market_Manager_Shared3__c = a.GeoJunction__r.Market_Assignments__r.Market_Manager_Shared_3__c; } else rm.Market_Manager_Shared3__c = a.GeoJunction__r.Market_Assignments__r.Market_Manager_Shared1__c; } } } } }
I don't see problem with the complicated if, as those 4 different fields.
But I thougth of 2 issues:
1.You have select inside the loop, which might cause issues if you will update bulk of data.
Better can be something like:
list<String> accidLst=new list<String>();
//collect all acoutn ID's
for(RM_requests__c req : Trigger.new)
accidLst.add(req.account__c);
//retreie all accounts
list<Account> account_Lst= [ select id, name, GeoJunction__r.Market_Assignments__c, GeoJunction__r.Market_Assignments__r.Market_Manager_Shared1__c, GeoJunction__r.Market_Assignments__r.Market_Manager_Shared2__c, GeoJunction__r.Market_Assignments__r.Market_Manager_Shared_3__c, GeoJunction__r.Market_Assignments__r.Market_Manager__c
from account where id IN :accidLst];
//in the loop work with the account_lst instead running select query each time.
2.Did you check if this can be done without code? means with workflow (4 workflow in this case)?
Thanks! Yes, I checked that I couldn't do it with a workflow.
The 'if' gets complicated becuase any of the 4 users could be in any of the 4 fields.
so if mm1 is not the owner put in mmshared 1 if it is the owner then move on to mmshared 2 etc....
I'll try putting the query outside the for loop.
Thanks!