You need to sign in to do that
Don't have an account?
Zachary 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;
}
}
}
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;
}
}
}
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
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