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
Michael Davis 15Michael Davis 15 

trigger.oldmap returning same values as trigger.newmap

Here is the code snippit:
trigger WorkOrderJobRollUp on Work_Order__c (before update, after insert, after delete, after undelete) {
    
    // Check to make sure work order is not being deleted
    IF (trigger.isdelete == FALSE)
    {
        // Run trigger for all situations with the exception of the AFE Approval process
        For(Work_Order__c wo : Trigger.New)
        {
            try
            {
                Work_Order__c beforeUpdate = Trigger.oldMap.get(wo.ID);
                system.debug('Work Order ID New: ' + wo.ID);
                system.debug('Work Order ID Old: ' + beforeUpdate.ID);
                
                string oldstatus = beforeUpdate.Work_Order_Status__c;
                
                system.debug('New WO Status: ' + wo.Work_Order_Status__c);
                system.debug('Old WO Status: ' + beforeUpdate.Work_Order_Status__c);
                system.debug('Old WO Status: ' + trigger.oldmap.get(wo.ID).Work_Order_Status__c);
                system.debug('Old WO Status: ' + oldstatus);
                   
                   IF (wo.Work_Order_Status__c == 'Approved' && beforeUpdate.Work_Order_Status__c == 'Pending Approval') return;
            }
            catch(exception e){}
        }
    }

Here is a snippit of the debug log:
14:09:14.853 (9285875790)|USER_DEBUG|[12]|DEBUG|Work Order ID New: a270j0000009ASKAA2 14:09:14.853 (9285880579)|STATEMENT_EXECUTE|[13] 14:09:14.853 (9285889523)|HEAP_ALLOCATE|[13]|Bytes:18 14:09:14.853 (9285894991)|HEAP_ALLOCATE|[13]|Bytes:37 14:09:14.853 (9285898897)|USER_DEBUG|[13]|DEBUG|Work Order ID Old: a270j0000009ASKAA2 14:09:14.853 (9285901596)|STATEMENT_EXECUTE|[15] 14:09:14.853 (9285907986)|VARIABLE_SCOPE_BEGIN|[15]|oldstatus|String|false|false 14:09:14.853 (9285914795)|VARIABLE_ASSIGNMENT|[15]|oldstatus|"Approved" 14:09:14.853 (9285916837)|STATEMENT_EXECUTE|[17] 14:09:14.853 (9285920997)|HEAP_ALLOCATE|[17]|Bytes:23 14:09:14.853 (9285925680)|USER_DEBUG|[17]|DEBUG|New WO Status: Approved 14:09:14.853 (9285928468)|STATEMENT_EXECUTE|[18] 14:09:14.853 (9285932040)|HEAP_ALLOCATE|[18]|Bytes:23 14:09:14.853 (9285935598)|USER_DEBUG|[18]|DEBUG|Old WO Status: Approved 14:09:14.853 (9285938221)|STATEMENT_EXECUTE|[19] 14:09:14.853 (9285964009)|HEAP_ALLOCATE|[19]|Bytes:23 14:09:14.853 (9285968749)|USER_DEBUG|[19]|DEBUG|Old WO Status: Approved 14:09:14.853 (9285971863)|STATEMENT_EXECUTE|[20] 14:09:14.853 (9285974149)|HEAP_ALLOCATE|[20]|Bytes:23 14:09:14.853 (9285978158)|USER_DEBUG|[20]|DEBUG|Old WO Status: Approved

The value of the Work Order Status field is changing from Pending Approval to Approved and I don't want the body of the trigger to fire off when this happens.  How does the Trigger.newmap and trigger.oldmap values equal each other?
Best Answer chosen by Michael Davis 15
Shivdeep KumarShivdeep Kumar
Hi Michael,
I apologize for my late response. !!
As per your requirement, I write a trigger in which you ll easily get the logic of the trigger i.e., how to match the value of old and new trigger.
trigger WorkOrderJobRollUp on Work_Order__c (before insert, before update) {
	Set<String> WoStatus = New Set<String>();
	If(Trigger.IsUpdate){
		for(Work_Order__c wo : Trigger.New){
			// you can set your own condition and add to the string of work order status like :
			If(wo.Work_Order_Status__c <> null || wo.Work_Order_Status__c <> ''){
				WoStatus.add(wo.Work_Order_Status__c);
			}
		}
		for(Work_Order__c wo : Trigger.old){
			// your condition
			If(wo.Work_Order_Status__c <> null || wo.Work_Order_Status__c <> ''){
				WoStatus.add(wo.Work_Order_Status__c);
			}
		}
	}else if(Trigger.IsInsert){
		for(Work_Order__c wo : Trigger.New){
			// your condition
			If(wo.Work_Order_Status__c <> null || wo.Work_Order_Status__c <> ''){
				WoStatus.add(wo.Work_Order_Status__c);
			}
		}
	}
	
	Map<Id,Work_Order__c> ExistingWOrdersMap = New Map<Id, Work_Order__c>([Select Id,Work_Order_Status__c from Work_Order_Status__c where Work_Order_Status__c IN : WoStatus ]);
	// trigger new values
	for(Work_Order__c w : Trigger.New){
		// work order exixting values with all status or you can set your own 
		for(Work_Order__c WoMap : ExistingWOrdersMap.Values()){
			If(w.Work_Order_Status__c == 'Pending Approval' && WoMap.Work_Order_Status__c == 'Approved'){
				// your logic
			}
		}
	
	}
}

for more information take a look on http://salesforce.stackexchange.com/questions/46790/how-to-avoid-recursive-trigger-other-than-the-classic-class-w-static-variable

Please let me know if this help. !

Thanks
Shivdeep

All Answers

Shivdeep KumarShivdeep Kumar
Hi Michael,
Can you please explain the requirement for this trigger.

thanks
Shivdeep
Michael Davis 15Michael Davis 15
This is only the top half of the tigger.  The bottom half just rolls up values from the Work Order object to another object.  The reason for the check is there is an approval process that kicks off the trigger.  I hopped to prevent the trigger from executing when the approval process from kicking off.
Shivdeep KumarShivdeep Kumar
Hi Michael,
I apologize for my late response. !!
As per your requirement, I write a trigger in which you ll easily get the logic of the trigger i.e., how to match the value of old and new trigger.
trigger WorkOrderJobRollUp on Work_Order__c (before insert, before update) {
	Set<String> WoStatus = New Set<String>();
	If(Trigger.IsUpdate){
		for(Work_Order__c wo : Trigger.New){
			// you can set your own condition and add to the string of work order status like :
			If(wo.Work_Order_Status__c <> null || wo.Work_Order_Status__c <> ''){
				WoStatus.add(wo.Work_Order_Status__c);
			}
		}
		for(Work_Order__c wo : Trigger.old){
			// your condition
			If(wo.Work_Order_Status__c <> null || wo.Work_Order_Status__c <> ''){
				WoStatus.add(wo.Work_Order_Status__c);
			}
		}
	}else if(Trigger.IsInsert){
		for(Work_Order__c wo : Trigger.New){
			// your condition
			If(wo.Work_Order_Status__c <> null || wo.Work_Order_Status__c <> ''){
				WoStatus.add(wo.Work_Order_Status__c);
			}
		}
	}
	
	Map<Id,Work_Order__c> ExistingWOrdersMap = New Map<Id, Work_Order__c>([Select Id,Work_Order_Status__c from Work_Order_Status__c where Work_Order_Status__c IN : WoStatus ]);
	// trigger new values
	for(Work_Order__c w : Trigger.New){
		// work order exixting values with all status or you can set your own 
		for(Work_Order__c WoMap : ExistingWOrdersMap.Values()){
			If(w.Work_Order_Status__c == 'Pending Approval' && WoMap.Work_Order_Status__c == 'Approved'){
				// your logic
			}
		}
	
	}
}

for more information take a look on http://salesforce.stackexchange.com/questions/46790/how-to-avoid-recursive-trigger-other-than-the-classic-class-w-static-variable

Please let me know if this help. !

Thanks
Shivdeep
This was selected as the best answer