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
Cloud-LingCloud-Ling 

addError Method

Heya,

My code has no error on compilation but when it came to addError, it didn't work when I gave it a try.. here's my code, tell me what's jiffy in it.

 

trigger ErrorDuplicateOutput on Account (before insert, before update)
{
  List<Account> accList =new List<Account>();
  Set<String> b = new Set<String>{'Express Logistics and Transport','Edge Communications'};
  accList = [Select Id, Name FROM Account Where Name in: b];
  
  for (Account acc : accList)
  {
   if (b.contains(acc.Name))
   {
    acc.Name.addError('Account name already exist!');
   }
  }
  update accList;
}

 Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ErrorDuplicateOutput caused an unexpected exception, contact your administrator: ErrorDuplicateOutput: execution of BeforeInsert caused by: System.FinalException: SObject row does not allow errors: Trigger.ErrorDuplicateOutput: line 11, column 5

 

Best Answer chosen by Admin (Salesforce Developers) 
Cloud-LingCloud-Ling

ooh i see! thank you for the response kyle.t but i've done new for loop. and it worked.

 

for(integer x = 0; x < Trigger.new.size(); x++)
  {
      //Trigger.new[x].Name = 'test';
      if(b.contains(Trigger.new[x].name))
      {
          Trigger.new[x].addError('Error:Account name already exist!');
          
      }
  }

 

All Answers

kyle.tkyle.t

It looks like you are trying to add the error to the items you are querying and not the items in the trigger.  you need to add the error to the items in the trigger.

 

 

it appears you are trying to prevent any account from being created which has the name Express Logistcs and Transport or Edge Communications.  This should do the trick.

 

 

trigger ErrorDuplicateOutput on Account (before insert, before update)
{
  for (Account acc : trigger.new)
  {
   if (acc.Name == 'Express Logistics and Transport' || acc.Name == 'Edge Communications')
   {
    acc.Name.addError('Account name already exist!');
   }
  }
}

 

The idea is to loop through all of the accounts in the trigger and add an error if they match the condition.  the addError will prevent any DML Operation from occurring

 

 

 

Cloud-LingCloud-Ling

ooh i see! thank you for the response kyle.t but i've done new for loop. and it worked.

 

for(integer x = 0; x < Trigger.new.size(); x++)
  {
      //Trigger.new[x].Name = 'test';
      if(b.contains(Trigger.new[x].name))
      {
          Trigger.new[x].addError('Error:Account name already exist!');
          
      }
  }

 

This was selected as the best answer
Cloud-LingCloud-Ling

anyways, my new problem now is to create outbound email from this trigger using Apex class (without using the template and workflow). any idea how?

kyle.tkyle.t

The visualforce documentation can be found here: http://www.salesforce.com/us/developer/docs/apexcode/index.htm

 

Search for "outbound email" and use the example.

Cloud-LingCloud-Ling

yes, i reviewed the documentation but there's no example for the syntax on how i can call the Email Class from my trigger.

 

thanks,

C-L

kyle.tkyle.t

On that page, you see that to send a single mail message you have to instantiate:

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

 

 

So you can search the documentation for "SingleEmailMessage" to see the properties.

 

from those properties you should be able to figure out what you need to set to send the email.

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses = 'abc@mail.com';
mail.setSubject = 'This is the subject';
mail.setPlainTextBody = 'This the body of the email';

 **Note, the above code has not been tested, it is just to give you the Idea.

 

Finally,  back on the documentation for Messaging we can see how to send the email

 

Messaging.sendEmail(new Messaging.Email[] { mail } );
Cloud-LingCloud-Ling

thanks!

Nabeel khan 30Nabeel khan 30
trigger DuplicateAccount on Account (before insert ,before update) 
{
    for(Account acc: trigger.new)
    {
       list<Account> acclist=[select Name ,Id from Account where Name=:acc.Name];   
    
    if(acclist.size()>0)
    {
   acc.Name.addError('duplicate Account Name');
   system.debug('you can not add account with the duplicate name');
    }
    
    }
}
Sebastian DavisSebastian Davis
@Nabeel Kahn 30
Tisk Tisk. Never put soql in a loop code block ;-)