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
Zachary SpringerZachary Springer 

How to compare two values in apex?

I've got a trigger and I want to make sure that once a field has a value, it does not get updated. This seems really close but apex throws an error and won't allow the if check to go through. It says that the == must be for two booleans. I simply want to check if the two string values are equal.

trigger SourceUpdate on Contact (before update) {
    for(Contact con: Trigger.new)
    {
      Contact oldCon = Trigger.oldMap.get(con.Id);
        system.debug(con.Source__c == oldCon.Source__c);
        if(oldCon.Source__c && con.Source__c != oldCon.Source__c) {
            con.Source__c = oldCon.Source__c;
        }
    }
}
Best Answer chosen by Zachary Springer
Maharajan CMaharajan C
Hi Zachary,

I hope Source__c  is Picklist or Text Field not a checkbox. If it is a Pickist or Text then change your condtion like below:

        if(oldCon.Source__c!=null && con.Source__c != oldCon.Source__c) {
                          con.Source__c = oldCon.Source__c;
         }

Thanks,
Maharajan.C

 

All Answers

Maharajan CMaharajan C
Hi Zachary,

I hope Source__c  is Picklist or Text Field not a checkbox. If it is a Pickist or Text then change your condtion like below:

        if(oldCon.Source__c!=null && con.Source__c != oldCon.Source__c) {
                          con.Source__c = oldCon.Source__c;
         }

Thanks,
Maharajan.C

 
This was selected as the best answer
Zachary SpringerZachary Springer
That was exactly the problem. Thank you Maharajan!