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
Will Jones 18Will Jones 18 

Trigger.oldmap not working anymore on trigger

Users realized that a field was not being updated over the past few weeks. When debugging I realized that in my code the "Trigger.oldmap.get" is not working as it once was. The Trigger.oldmap.get returns the the latest commitment to the database, not the previous value as expected. Any clue why this code would stop working?
 
trigger UpdateSigningManger on Account (after insert, after update) {

//Updates Signing Manager field based on user profile and vendor stage


//prevents an infinite loop
if(Recursion2.objectRecursion)
    return;
Recursion2.objectRecursion = TRUE;

//get list of accounts
List<account> acct_list = [SELECT Id, Vendor_Stage__c, market__c, Signing_Manager_lookup__c, lastmodifiedbyid, lastmodifiedby.General_Manager__c,
createdbyid, createdby.General_Manager__c, lastmodifiedby.profile.name, createdby.profile.name, RecordTypeId FROM Account WHERE Id IN: Trigger.newmap.keyset()];
 
    
//get list of Venue markets and map them out
List <market__c> mkt_list = [SELECT Name, Venue_General_Manager__c FROM Market__c];
    
Map<string, string> mkt_map = new map<string, string>(); 
    
    for(Market__c mkt : mkt_list){
      mkt_map.put(mkt.Name, mkt.Venue_General_Manager__c);  
    }

for(Account acct : acct_list){

    
    if(Trigger.isUpdate && acct.RecordTypeId == '012C0000000M8xd'){
    
        // Initialize Boolean variables
        Boolean newAcctIsAgreed = FALSE;
        Boolean oldAcctIsAgreed = FALSE;
        
        //Access the old record by its ID in Trigger.oldmap
        Account oldAcct = Trigger.oldmap.get(acct.Id);
    
        //Trigger.new and Trigger.old values are assigned to boolean variables for comparison
        
        if(oldAcct.RecordTypeId == '012C0000000M8xd'){
            if(oldAcct.Vendor_Stage__c == null){
                oldAcctIsAgreed = FALSE;}
            else{
                oldAcctIsAgreed = oldAcct.Vendor_Stage__c.equals('Agreement of Commercials') || oldAcct.Vendor_Stage__c.equals('MSA Signed') || oldAcct.Vendor_Stage__c.equals('Covered by MSA');
            }
        }    
            if(acct.Vendor_Stage__c != null){
                newAcctIsAgreed = acct.Vendor_Stage__c.equals('Agreement of Commercials') || acct.Vendor_Stage__c.equals('MSA Signed') || Acct.Vendor_Stage__c.equals('Covered by MSA');
            }
        // Check that the field was just changed to the new value. 
         
        SYSTEM.DEBUG('Vendor Stage is ' + acct.Vendor_Stage__c);
        SYSTEM.DEBUG('oldAcctIsAgreed is ' + oldAcctIsAgreed);
        SYSTEM.DEBUG('newAcctIsAgreed is ' + newAcctIsAgreed); 
        
            if(!oldAcctIsAgreed && newAcctIsAgreed){
                if(acct.lastmodifiedby.profile.name.equals('Venue Manager') || acct.lastmodifiedby.profile.name.equals('Vendor Manager 2') ||acct.lastmodifiedby.profile.name.equals('C-Level') || acct.createdby.profile.name.equals('Coordinator') || acct.createdby.profile.name.equals('CSD') || acct.createdby.profile.name.equals('GM'))
                    {acct.Signing_Manager_lookup__c = acct.lastmodifiedbyid;
                    }
                else if(mkt_map.containsKey(acct.market__c))
                    {acct.Signing_Manager_lookup__c = mkt_map.get(acct.market__c);
                    }
                else if(acct.lastmodifiedby.General_Manager__c != null)
                    {acct.Signing_Manager_lookup__c = acct.lastmodifiedby.General_Manager__c;
                    }
                else{acct.Signing_Manager_lookup__c = acct.lastmodifiedbyid;
                    } 
            }
            
     }
            
     else {
         if(acct.RecordTypeId == '012C0000000M8xd'){
             if(acct.Vendor_Stage__c.equals('Agreement of Commercials') || acct.Vendor_Stage__c.equals('MSA Signed')|| acct.Vendor_Stage__c.equals('Covered by MSA'))
                 if(acct.createdby.profile.name.equals('Venue Manager') || acct.createdby.profile.name.equals('Vendor Manager 2') ||acct.createdby.profile.name.equals('C-Level') || acct.createdby.profile.name.equals('Coordinator') || acct.createdby.profile.name.equals('CSD') || acct.createdby.profile.name.equals('GM'))
                     {acct.Signing_Manager_lookup__c = acct.createdbyid;
                     }
                 else if(mkt_map.containsKey(acct.market__c))
                     {acct.Signing_Manager_lookup__c = mkt_map.get(acct.market__c);
                     } 
                 else if(acct.createdby.General_Manager__c != null)
                     {acct.Signing_Manager_lookup__c = acct.createdby.General_Manager__c;
                     }       
                 else {acct.Signing_Manager_lookup__c = acct.createdbyid;
                       }
         }
     }
    
  
}

if(acct_list.size()>0)
{
update acct_list;
}

}

 
Himanshu ParasharHimanshu Parashar
Hi Will,

I think problem is in your comparison at line 28 where you are comparing acc.recordtypeid with hardcoded value. I think you knbow that recordtypeid is always 18 chars and you are comapring it 15 chars id which escaping your if logic. you need to replace 15 chars recordtype id with 18 chars.

Does it makes sense ?

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Will Jones 18Will Jones 18
Hi Himanshu, this is not the issue. First off the recordtypeid was working before so it does not make sense why it would not work now. Also as stated in my initial post, the "Trigger.oldmap.get" is pulling the current values, not the old values, hence my original question.
Will Jones 18Will Jones 18
//prevents an infinite loop

if(Recursion2.objectRecursion)

    return;

Recursion2.objectRecursion = TRUE;
I think it has something to do with this piece of code. I use this in another trigger and I don't know if it is disrupting this trigger.
Himanshu ParasharHimanshu Parashar
Hi Will,

yes if you are doing that in another trigger then it is an issue because I think you know that trigger never execute in an order.
Will Jones 18Will Jones 18
Yes Himanshu, you are correct. I am now trying to figure out how to make sure that the list is updated. I made an adjustment in the code. I removed:
 
//prevents an infinite loop

if(Recursion2.objectRecursion)

    return;

Recursion2.objectRecursion = TRUE;

And I added at the end:
 
if(stoprecurssion.runonce()){
        SYSTEM.DEBUG('List size is ' + acct_list.size());
		if(acct_list.size()>0)
		{
			update acct_list;
		}
     }

The problem is that on the first run I do not have the correct value (for reasons I have not been able to figure out). Therefore by the time is reaches the 4th iteration where I have the correct values, BUT the update does not occur since the recursion only allows for it to run once. Any suggestions. Below is the debug log showing that by the time it reaches the 4th iteration (the area with the red box around it) it has the correct information, but the update does not occur.

User-added image
Himanshu ParasharHimanshu Parashar
Hi Will,

If both events firing at same event i.e. Before Update then you should write a helper class which will call once on Trigger event then you can control execution.

Thanks,
Himanshu
Will Jones 18Will Jones 18
I'm not too sure where to start with the helper class. Not a developer by trade. Just figured out about debug statements!
Himanshu ParasharHimanshu Parashar
Hi Will,

Wrinting a helper class is bit time consuming but I will try to send it to you so that you can understand the concept. meanwhile you can do one thing.

You can create another constant variable for this trigger to avoid recursion.


Thanks,
Himanshu