0

Hi I am trying to validate a few fields in salesforce when a trigger is fired in the accounts.

My Code

trigger AccountCallout on Account (before update) {
// make the asynchronous web service callout   
for (Account a : Trigger.new) {     
    if(a.Name != null && a.Email__c !=null )
    {
      //Some Operations
    }
    else
    {
  a.Email__c.adderror('There was a problem updating the accounts. Email cannot be null');
    }
  }
}

Error message is not being displayed on the page and the fields are getting updated

1
  • Are you just mixing and/or? And there is no readily apparent reason to avoid Validation Rule here.
    – Adrian Larson
    Sep 15 '16 at 12:44
1

I tried this,

trigger accountTrigger1 on Account (Before Update) {

    for (Account a : Trigger.new) {     
    if(a.Number_of_Contacts__c != null  )
    {
      //Some Operations
    }
    else
    {
        a.Number_of_Contacts__c.adderror('There was a problem updating the accounts. Email cannot be null');
    }
  }

}

This worked for custom field.check this

3
2

First, rather than do a trigger, I would use a validation rule to perform the null checks or make the field required on the page layout depending on circumstances.

But if you must do it in trigger ( no idea why) make it easy on yourself and simplify trigger by putting the if no error condition code in the else instead of the if

trigger AccountCallout on Account (before update) {
// make the asynchronous web service callout   
  for (Account a : Trigger.new) {     
    if(a.Email__c == null ){
      a.Email__c.adderror('There was a problem updating the accounts. Email cannot be null');
    }else{
       //Some Operations
    }
  }
}

If that does not work for you then your email__c is not blank. Also note that you will need to bulkify some operations if you are making a call to your async method here note that if one record has an error the rest of the records being updated will be sent to the other method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.