0

Hi I have built a trigger to throw error if a field is blank upon certain condition.

Since it is for large number of country i used trigger instead of validation rule.

However I tried to test it but the trigger did not throw the error.

Please find the trigger below

trigger TR_REMA_OPP on Opportunity (before insert,before update) {

for(Opportunity opps : trigger.new){

    if((opps.AccountHQCountry__c != 'ARGENTINA' || opps.AccountHQCountry__c != 'AUSTRALIA' || opps.AccountHQCountry__c != 'AUSTRIA' || opps.AccountHQCountry__c != 'BELGIUM' || opps.AccountHQCountry__c != 'BRAZIL' || opps.AccountHQCountry__c != 'CANADA' || opps.AccountHQCountry__c != 'CHINA'
         || opps.AccountHQCountry__c != 'DENMARK' ||
            opps.AccountHQCountry__c != 'FRANCE'  ||
            opps.AccountHQCountry__c != 'GERMANY' ||
            opps.AccountHQCountry__c != 'HONG KONG' ||
            opps.AccountHQCountry__c != 'INDIA' ||
            opps.AccountHQCountry__c != 'INDONESIA' ||
            opps.AccountHQCountry__c != 'IRELAND' ||
            opps.AccountHQCountry__c != 'IRLAND' ||
            opps.AccountHQCountry__c != 'ITALIA' ||
            opps.AccountHQCountry__c != 'ITALY' ||
            opps.AccountHQCountry__c != 'JAPAN' ||
            opps.AccountHQCountry__c != 'KOREA (NORTH)' ||
            opps.AccountHQCountry__c != 'KOREA (SOUTH)' ||
            opps.AccountHQCountry__c != 'LAOS' ||
            opps.AccountHQCountry__c != 'LUXEMBOURG' ||
            opps.AccountHQCountry__c != 'MEXICO' ||
            opps.AccountHQCountry__c != 'NETHERLANDS' ||
            opps.AccountHQCountry__c != 'NEW CALEDONIA' ||
            opps.AccountHQCountry__c != 'NEW ZEALAND' ||
            opps.AccountHQCountry__c != 'NICARAGUA' ||
            opps.AccountHQCountry__c != 'NORWAY' ||
            opps.AccountHQCountry__c != 'PARAGUAY' ||
            opps.AccountHQCountry__c != 'PERU' ||
            opps.AccountHQCountry__c != 'PHILIPPINES' ||
            opps.AccountHQCountry__c != 'POLAND' ||
            opps.AccountHQCountry__c != 'PORTUGAL' ||
            opps.AccountHQCountry__c != 'REPUBLIC OF SAN MARINO' ||
            opps.AccountHQCountry__c != 'REUNION ISLAND' ||
            opps.AccountHQCountry__c != 'SINGAPORE' ||
            opps.AccountHQCountry__c != 'SPAIN' ||
            opps.AccountHQCountry__c != 'SWEDEN' ||
            opps.AccountHQCountry__c != 'SWITZERLAND' ||
            opps.AccountHQCountry__c != 'UNITED KINGDOM' ||
            opps.AccountHQCountry__c != 'UNITED STATES OF AMERICA' ||
            opps.AccountHQCountry__c != 'URUGUAY'   ||
            opps.AccountHQCountry__c != 'VENEZUELA' ||
            opps.AccountHQCountry__c !='VIETNAM' || 
            opps.AccountHQCountry__c != 'ZIMBABWE') && opps.Probability >= 75 && opps.Financed__c == ''){



                    opps.addError('Financed is mandatory to update');


    }
}

}

On the last step I also tried qpps.Financed__c.addError('this field is mandatory to update');

Stll did not worked.

opps.AccountHQCountry__c is a formula text field which will display the accounts country. Based upon the condition i set the account country to a different country to make sure the condition are met and the probability to above 75 still did not worked.

Please advise.

Thanks.

3
  • A few things: 1) Try putting in a few System.debug() statements to spit out the values of AccountHQCountry__c, Probability and Financed__c. 2) Consider changing opps.Financed__c == '' to String.isBlank(opps.Financed__c) for nulls. 3) Strongly consider storing those countries in a custom metadata instead of hardcoding those values. Commented Apr 13, 2020 at 17:56
  • @DrewKennedy Great thanks, that worked perfectly. Commented Apr 13, 2020 at 18:04
  • @DrewKennedy String.isBlank is overkill. Fields loaded from a database with no value are always null, never an empty string.
    – sfdcfox
    Commented Apr 13, 2020 at 18:06

1 Answer 1

2

Formulas are in a "before" update state in a "before" trigger; for new records, this will be a null value, and for updates, will be the calculated value before saving. Use an "after" trigger instead. They will be correctly calculated only in an after trigger.

Also, note that field != A || field != B will always be true (if the field is equal to A, it will not be equal to B, and vice versa). Use && instead.

Also, you could improve performance by simply saying:

Set<String> countries = new Set<String> {
  'ARGENTINA', 'AUSTRALIA', ...
};
if(!countries.contains(opps.AccountHQCountry__c)) {
  opps.addError('...');
}

Empty fields loaded from a database are always null, never an empty string:

opps.Financed__c == null

Probability is exposed as a decimal percentage (value / 100). You should compare against 0.75:

opps.Probability > 0.75
4
  • @ sfdcfox The opportunity is making the finaced field required to save when the record meets the condition if I put this in after trigger how will it throw an error to user before he commits the record to database. Commented Apr 13, 2020 at 18:10
  • @SamuelRobert after triggers are not fully committed, it's only a save point about half-way through the DML operation. Adding an error here will still undo any changes.
    – sfdcfox
    Commented Apr 13, 2020 at 18:12
  • @ sfdcfox Sure thanks. :) Commented Apr 13, 2020 at 18:13
  • sfdcfox I tried to implement your solution, but the system throws error even after the probability is less than 75 Commented Apr 15, 2020 at 9:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .