You need to sign in to do that
Don't have an account?
Cloud-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
ooh i see! thank you for the response kyle.t but i've done new for loop. and it worked.
All Answers
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.
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
ooh i see! thank you for the response kyle.t but i've done new for loop. and it worked.
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?
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.
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
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.
**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
thanks!
{
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');
}
}
}
Tisk Tisk. Never put soql in a loop code block ;-)