+ Start a Discussion
EUSEUS 

Trigger not stopping DML operation using sObject.Field.addError() Method...

Hi,

 

I have a trigger which checks whether two fields in different objects have the same value in which case it should return to the Salesforce interface with the addError message inserted. However, it does not stop the DML operation nor returns with the Error Message.  I placed the addError message statement everywhere testing, but it doesn't work. I might be missing something important! .... 

 

Here follows the trigger code:

 

trigger languageLangMessageInsertion on LANG__c (before insert) {
    List<SetUp__c> SU = [SELECT Msgs_Lang__c FROM SetUp__c LIMIT 1];
       if (SU.size()>0) {     //  Country/Region record Exists whithin sObject SetUp__c                
        for (SetUp__c usu : SU) {
            List<LANG__c> LG = [SELECT Language__c FROM LANG__c  WHERE id IN :Trigger.new];
               for (LANG__c aLG : LG) {
               if (usu.Msgs_Lang__c == aLG.Language__c) {  //  SetUp Msgs_Lang__c equals this selection .... Not Valid   
                  aLG.Language__c.addError('message language not valid.');
              }
              else {
                  aLG.Language__c.addError('B .. message language not valid.');
              }
            }    
           }   
      }   // if SU.size()>0
    else {
                //  Country/Region record does not Exist yet ...
               for (LANG__c aLG : trigger.new) {
                   aLG.Language__c.addError('First you need to create the Region record and default language.');
                  } 
             }  //  else
}

 

Any suggestions?

Thanks a lot!

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You have to add an error to a record in Trigger.old/Trigger.new. Instead, you're querying the records (line 5), then adding errors to them. Coincidentally, if SetUp__c were empty, you'd get an error (that loop is written correctly). Remove line 5, and replace LG on line 6 (the new line 5) with Trigger.new.

All Answers

sfdcfoxsfdcfox

You have to add an error to a record in Trigger.old/Trigger.new. Instead, you're querying the records (line 5), then adding errors to them. Coincidentally, if SetUp__c were empty, you'd get an error (that loop is written correctly). Remove line 5, and replace LG on line 6 (the new line 5) with Trigger.new.

This was selected as the best answer
MellowRenMellowRen

Hello EUS

 

You have had this problem before: “Before Insert Trigger question”.

 

This line will always return an empty list:

List<LANG__c> LG = [SELECT Language__c FROM LANG__c  WHERE id IN :Trigger.new];

         "You are trying to find all existing records whose unique ID is the same as the ID of the records you are about to insert." 

 

How many records are in the current database that have the same ID (a per record unique code) as the records you are about to create? Answer 0 or in Salesforce terms List<LANG__c> LG = { };

 

Hence nothing else within your for loop gets executed.

 

I think you will help yourself greatly if you went an read up a bit on what trigger.new and trigger.old actually are—google-ing can get you directly to the correct pages in the Salesforce docs.

 

As a starter though, given that this is a trigger for "LANG__c" and it is a "before insert", then trigger.new is a little bit like Salesforce has done this:

 

List<LANG__c> trigger.new = [SELECT all_fields FROM LANG__c WHERE the_record_has_been_submitted_but_not_saved];

 So, to start you off down the right track:

 

  1. Get rid of this line: List<LANG__c> LG = [SELECT Language__c FROM LANG__c  WHERE id IN :Trigger.new];
               
    It is superfluous, as you already have trigger.new.
       
  2. Change this line: for (LANG__c aLG : LG) {
               
    To: for (LANG__c aLG : trigger.new) {

 

This will at least get you down into the If statement.

 

BTW, have you come across the "System.Debug()" method and the Developer Log console? This is an invaluable debugging combination and would have picked this up very quickly.

 

Regards

MellowRen

EUSEUS

Thank sfdcfox!

EUSEUS

Hi MellowRen,

 

I guess I need some rest! .... Thanks again!

 

Regards,

EUS

MellowRenMellowRen

Ha ha ha, yes I know what THAT feels like. Good luck.