0

i'm getting an error from updating or saving a record on the following trigger:

trigger ActiveSave on CreditInfo_Eval_Rules__c (before insert, before update) {

    for(CreditInfo_Eval_Rules__c credRules : [SELECT Active__c FROM CreditInfo_Eval_Rules__c 
                                              WHERE CreditInfo_Fields__r.Used__c = false 
                                              AND Id IN :Trigger.new]){
        if(credRules.Active__c == true){
            credRules.addError('The CreditInfo Field is not beeing used!');
        }
    }
}
0

You can you use the addError method for only those records that are available in Trigger Context.

trigger ActiveSave on CreditInfo_Eval_Rules__c (before insert, before update) {

    for(CreditInfo_Eval_Rules__c credRules : [SELECT Active__c FROM CreditInfo_Eval_Rules__c 
                                              WHERE CreditInfo_Fields__r.Used__c = false 
                                              AND Id IN :Trigger.new]){
        if(credRules.Active__c == true){
            trigger.newmap.get(credRules.id).addError('The CreditInfo Field is not beeing used!');
        }
    }
}

You need to change just one line i.e instead of credRules.addError use trigger.newmap.get(credRules.id).addError.

Triggers can be used to prevent DML operations from occurring by calling the addError() method on a record or field. When used on Trigger.new records in insert and update triggers, and on Trigger.old records in delete triggers, the custom error message is displayed in the application interface and logged.

reference:-

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.