You need to sign in to do that
Don't have an account?
Chris 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:
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
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
All Answers