You need to sign in to do that
Don't have an account?
Chitral Chadda
Insert lead trigger
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UpdatesOnLead caused an unexpected exception, contact your administrator: UpdatesOnLead: execution of BeforeUpdate caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.UpdatesOnLead: line 14, column 1
i want to insert new lead whenevr blackisted is selected on update
bt i get this error
Review all error messages below to correct your data.
Apex trigger UpdatesOnLead caused an unexpected exception, contact your administrator: UpdatesOnLead: execution of BeforeUpdate caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.UpdatesOnLead: line 14, column 1
trigger UpdatesOnLead on Lead (before Update,before insert) { list<lead> addhere = new list<lead>(); for( lead ld :trigger.new) { if(ld.Do_Not_Market__c=='Blacklisted' && trigger.oldmap.get(ld.id).Do_Not_Market__c!= 'Blacklisted') { ld.Do_Not_Mail__c=True; ld.DoNotCall=True; ld.Email_Opt_Out__c=True; ld.Status='Dead'; addhere.add(ld); } } insert addhere; }
i want to insert new lead whenevr blackisted is selected on update
bt i get this error
The problem is not with insert the problem is with your if condition.
In insert you wont get trigger.old that will through you error.
If you can change ur code like this it will work on both events.
If you want ur trigger based on update then use after update or before update.
Thanks
Anil.B
All Answers
Try this trigger:-
Thanks
Anil.B
I think it shud be only on before update and not before insert
cz we are using insert dml statement
The problem is not with insert the problem is with your if condition.
In insert you wont get trigger.old that will through you error.
If you can change ur code like this it will work on both events.
If you want ur trigger based on update then use after update or before update.
Thanks
Anil.B
(ld.Do_Not_Market__c=='Blacklisted' && oldld.Do_Not_Market__c!= ld.Do_Not_Market__c)
System.debug('===>'+oldld.Do_Not_Market__c +'====>'+ld.Do_Not_Market__c);
In this line oldld will be null and ld.Do_Not_Market__c will be having Blacklisted.
For more understanding of the code put system debug under it and check in developer console you can understand everything.
Thanks
Anil.B
Yes in case of insert "oldld" will be null.
But in case of updated it will contain the oldvalue on the record and compare old value with new value.
Thanks
Anil.B