function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kMans2kkMans2k 

addError error

hey all,
 
excuse the pun.
 
I have a custom object FormA__c, which has a status field, Status__c.
 
What I would like to do is for the system to throw an error when the user first creates this object and chooses the status as something other thans "Status1".
 
In my trigger (trigger on before insert) I have the following code:
 
FormA__c[] Current = Trigger.new;
if( Trigger.isInsert ) {
 if( Current[0].Status__c != 'Open' ) {
   trigger.new.Status__c.addError('Status error');
 }
  }
 
Apex editor is complaining about the "trigger.new.Status__c.addError('Status error');" line:
Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST:SOBJECT:FormA__c at line 8 column 7
 
if I change it to:
   trigger.new.addError('Status error');
 
It reports the following error:
Error: Compile Error: Method does not exist or incorrect signature: [LIST:SOBJECT:FormA__c].addError(String) at line 8 column 7
 
your help is much appreciated.
SuperfellSuperfell
Trigger.new is an array, addError is method on an SObject, not an array. so you need trigger.new[0].addError or Current[0].addError