• Mir Marayan
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies

I'm having trouble with a trigger I'm trying to write. When new accounts are loaded into my org the owner field is sometimes defaulted to a placeholder user 'x x'. There is another field on the account called market assignment which is a forumula that pulls the market assignment on the market assignment custom object. Also on the market assignement custom object is the manager which should also be the account owner. The trigger is trying to update accounts where owner = 'x x' with the manager from the corresponding market assignement. Any help would be much appreciated. Thanks!!

 

Trigger

 

Trigger marketassignmenttrigger on Account (before Insert, before Update) {
    
    Map<String, Market_Assignments__c> junctionMap = new Map<String, Market_Assignments__c>{};
    
    for (Account a : Trigger.new)
    {
        if (a.owner != null && 
        (Trigger.isInsert || (Trigger.isUpdate && Trigger.oldMap.get(a.id).Market_assignment_copy__c == a.Market_assignment_copy__c)))
        {            
            //Ignore casing in the map
            junctionMap.put(a.Market_assignment_copy__c.toLowerCase(), null);            
        }
    }
    
    if (!junctionMap.isEmpty())
    {    
        
        for (Market_Assignments__c item : [ select Id, market_manager__c, name from Market_Assignments__c where market_manager__c IN :junctionMap.keySet()] )
        {
            junctionMap.put(item.name.toLowerCase(), item);
        }
                        
        for (Account a : Trigger.new)
        {
            if (a.ownerid == '00560000001267d')
            {
               Market_Assignments__c m = junctionMap.get(a.market_assignment_copy__c.toLowerCase());
                
                
                {
                    a.ownerid = m.market_manager__c;                
                }
                
            }
        }
    }    
}