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
Chris Collier 6Chris Collier 6 

Trouble comparing old and new values of field

Hi everyone, I'm teaching myself Apex so please bear with me.  This is part 1 of a two-part trigger I'm trying to find (this is the easy part or so I thought).

I have a custom object, Listing__c, with a Date field, Active_Date__c.  The first part of the trigger is supposed to restrict the field Active_Date__c from being cleared.  Here is my code:
 
trigger ListingActiveDateUpdate on Listing__c (before update) {
    
    //Pull all listings being acted on and their associated tasks
    List<Listing__c> listingsWithTasks = [SELECT Id, Active_Date__c,(Select Subject, ActivityDate
                                                                    FROM Tasks)
                                          FROM Listing__c WHERE Id IN :Trigger.new];
    
    
    //Loop over all records being acted on
    for (Listing__c listing : listingsWithTasks){
        //RESTRICT DELETING ACTIVE DATE
        //If old Active Date was not null and new Active Date is null
        if (listing.Active_Date__c == null && Trigger.oldMap.get(listing.Id).Active_Date__c != null){
            //Add error to new listing record
            listing.addError('Cannot remove an active date from a listing');
        }
        
        
      
}
Ignore the fact that I'm querying tasks for now (that's for part 2).  I am trying to grab the old value for Active_Date__c, grab the new value for Active_Date__c, and compare the two.  I've tested this and it's not working.  I even threw in some dummy code to output these values:
        System.debug(listing.Active_Date__c); <- I would think it would show the new value of Active_Date__c because the SOQL query was written from Trigger.new?
        System.debug(Trigger.oldMap.get(listing.Id).Active_Date__c);
And the result showed that these two values were the same, even though I was going through the UI and deleting one.   Can someone please take a look and let me know where I went wrong?

Your help is much appreciated.

Chris
Best Answer chosen by Chris Collier 6
Nayana KNayana K
trigger ListingActiveDateUpdate on Listing__c (before update) 
{
    
    //Pull all listings being acted on and their associated tasks
    List<Listing__c> listingsWithTasks = [SELECT Id, Active_Date__c,(Select Subject, ActivityDate
                                                                    FROM Tasks)
                                          FROM Listing__c WHERE Id IN :Trigger.new];
    
    
    /*Loop over all records being acted on i.e Trigger.New (not SOQL result).
	If you use queried result here, then you will get old values because right now the context is
	before update (data is not committed into the database). 
	So you will end up with comparing old value with old value which will always be same and it won't 
	work as expected*/
    for (Listing__c listing : Trigger.New)
	{
        //RESTRICT DELETING ACTIVE DATE
        //If old Active Date was not null and new Active Date is null
        if (listing.Active_Date__c == null && Trigger.oldMap.get(listing.Id).Active_Date__c != null)
		{
            //Add error to new listing record
            listing.addError('Cannot remove an active date from a listing');
		}
    }    
      
}

 

All Answers

Nayana KNayana K
trigger ListingActiveDateUpdate on Listing__c (before update) 
{
    
    //Pull all listings being acted on and their associated tasks
    List<Listing__c> listingsWithTasks = [SELECT Id, Active_Date__c,(Select Subject, ActivityDate
                                                                    FROM Tasks)
                                          FROM Listing__c WHERE Id IN :Trigger.new];
    
    
    /*Loop over all records being acted on i.e Trigger.New (not SOQL result).
	If you use queried result here, then you will get old values because right now the context is
	before update (data is not committed into the database). 
	So you will end up with comparing old value with old value which will always be same and it won't 
	work as expected*/
    for (Listing__c listing : Trigger.New)
	{
        //RESTRICT DELETING ACTIVE DATE
        //If old Active Date was not null and new Active Date is null
        if (listing.Active_Date__c == null && Trigger.oldMap.get(listing.Id).Active_Date__c != null)
		{
            //Add error to new listing record
            listing.addError('Cannot remove an active date from a listing');
		}
    }    
      
}

 
This was selected as the best answer
Chris Collier 6Chris Collier 6
Thank you so so much!