+ Start a Discussion
Chitral ChaddaChitral 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
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
Best Answer chosen by Chitral Chadda
@anilbathula@@anilbathula@
Hi Chitral,

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.
trigger UpdatesOnLead on Lead (before Update,before insert) 
{ list<lead> addhere = new list<lead>();
 for( lead ld :trigger.new)
  {
    lead oldld=trigger.isUpdate?Trigger.oldmap.get(ld.id):new lead(); 
	
   if(ld.Do_Not_Market__c=='Blacklisted' && oldld.Do_Not_Market__c!= ld.Do_Not_Market__c)
   {
    lead lds=new lead();
    lds.firstname='test';
    lds.lastname='lastname';
    lds.company='some company';
    lds.Do_Not_Mail__c=True;
    lds.DoNotCall=True;
    lds.Email_Opt_Out__c=True;
    lds.Status='Dead';
    addhere.add(lds);
    }
   }
   insert addhere;
 }

Thanks
Anil.B

 

All Answers

@anilbathula@@anilbathula@
Hi Chitral,

Try this trigger:-
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')
   {
    lead lds=new lead();
    lds.firstname='test';
    lds.lastname='lastname';
    lds.company='some company';
    lds.Do_Not_Mail__c=True;
    lds.DoNotCall=True;
    lds.Email_Opt_Out__c=True;
    lds.Status='Dead';
    addhere.add(lds);
    }
   }
   insert addhere;
 }

Thanks
Anil.B
Chitral ChaddaChitral Chadda
yes i got the point
Chitral ChaddaChitral Chadda

I think it shud be only on before update and not  before insert

cz we are using insert dml statement 

 

@anilbathula@@anilbathula@
Hi Chitral,

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.
trigger UpdatesOnLead on Lead (before Update,before insert) 
{ list<lead> addhere = new list<lead>();
 for( lead ld :trigger.new)
  {
    lead oldld=trigger.isUpdate?Trigger.oldmap.get(ld.id):new lead(); 
	
   if(ld.Do_Not_Market__c=='Blacklisted' && oldld.Do_Not_Market__c!= ld.Do_Not_Market__c)
   {
    lead lds=new lead();
    lds.firstname='test';
    lds.lastname='lastname';
    lds.company='some company';
    lds.Do_Not_Mail__c=True;
    lds.DoNotCall=True;
    lds.Email_Opt_Out__c=True;
    lds.Status='Dead';
    addhere.add(lds);
    }
   }
   insert addhere;
 }

Thanks
Anil.B

 
This was selected as the best answer
Chitral ChaddaChitral Chadda
for( lead ld :trigger.new) { lead oldld=trigger.isUpdate?Trigger.oldmap.get(ld.id):new lead(); if(ld.Do_Not_Market__c=='Blacklisted' && oldld.Do_Not_Market__c!= ld.Do_Not_Market__c) In this part of code lead oldld=trigger.isUpdate?Trigger.oldmap.get(ld.id):new lead(); If trigger is on update it will fetch the old values Lead oldId = trigger.oldmap. Get(ld.id); But if trigger is on before insert it wil call the else part i.e Lead oldld= new lead(); Later, if(ld.Do_Not_Market__c=='Blacklisted' && oldld.Do_Not_Market__c!= ld.Do_Not_Market__c) If trigger is on update it will simply compare the values n prrform action Bt if it is on before insert , what would this stmnt do oldld.Do_Not_Market__c!= ld.Do_Not_Market__c) Beacause oldId is also trigger.new and ld is also trigger.new ( for loop )
@anilbathula@@anilbathula@
Hi Chitral,


(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
 
Chitral ChaddaChitral Chadda
(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); Hi anil Thnkyou 1 thing In this oldId will be null only in case of before insert But in case of before update it will ( oldId) compare old value
@anilbathula@@anilbathula@
Hi Chitral

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