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
SFDC_NewbieSFDC_Newbie 

After insert trigger giving error - PLEASE HELP- Have a deadline!!!

Hello everyone,

I'm very new to Apex and I've not done any kind of development for a few years now, so please bear with me if the query is poorly-written. I need to have this query done by tomorrow and I'm stuck. So any help would be greatly appreciated!!! I'm sure this is a cake walk for many of you.

 

So, all I'm trying to do is: Once a new lead is created, I want to update a certain field on the lead. The reason I've chose the after insert is because I need the campaign ids associated to that particular lead (from the Campaign Member table).

But when I try to save the lead, I get the following error. I suspect this has something to do with not being to change an existing record, but how else can i get the campaigns associated to that particular lead from the CampaignMember table if I didn't use after insert? :(( 

 

Error: Record is read-only: Trigger.Copy_CampaignCode_For_New_Leads: line 34, column 21

 

 

trigger Copy_CampaignCode_For_New_Leads on Lead (after insert) {
if (Trigger.new.size() == 1)
{

     Set<String> LeadString = new Set<String>();
    //Capture the Lead Id for use in the query
   
    for(Lead l : Trigger.new)
    {
     
        LeadString.add(l.Id);
        System.Debug(l.Id);
     
       
        //Query all the Campaign records that match the Lead Id
        List<CampaignMember> c = [Select CampaignId  from CampaignMember where LeadId IN :LeadString order by LastModifiedDate desc];
   
   
        //Capture the Campaign ID related to the first Campaign in the Campaign List achieved above.
        if(!c.isEmpty())
        {  
            system.debug(c[0].id);                              
            ID First_CampaignId = c[0].CampaignId;                   
   
   
            //Now capture the Campaign Name associated to this Campaign Id
            if(First_CampaignId != null)
            {
               List<Campaign> Camp = [Select Name  from Campaign where Id = :First_CampaignId];
              //Set the value for the new Campaign Name field for this newly created Lead
                if(!Camp.isEmpty())
                {
                    system.debug(Camp[0].Name);
                    l.Campaign_Name__c = Camp[0].Name;
             
                }
   
            }
   
         }
       
   
    }

}
}

Richie DRichie D

Hi,

 

Not sure if this is the anser or not but try getting the leads back out of the system instead on the trigger.new collection.

 

E.g.

 

trigger Copy_CampaignCode_For_New_Leads on Lead (after insert) { if (Trigger.new.size() == 1) { //create a lead that won't be readonly... Lead l = [select id, name,..........,..... from Lead where id=:Trigger.new[0].Id]; Set<String> LeadString = new Set<String>(); //Capture the Lead Id for use in the query //for(Lead l : Trigger.new) //{ LeadString.add(l.Id); System.Debug(l.Id); //Query all the Campaign records that match the Lead Id List<CampaignMember> c = [Select CampaignId from CampaignMember where LeadId IN :LeadString order by LastModifiedDate desc]; //Capture the Campaign ID related to the first Campaign in the Campaign List achieved above. if(!c.isEmpty()) { system.debug(c[0].id); ID First_CampaignId = c[0].CampaignId; //Now capture the Campaign Name associated to this Campaign Id if(First_CampaignId != null) { List<Campaign> Camp = [Select Name from Campaign where Id = :First_CampaignId]; //Set the value for the new Campaign Name field for this newly created Lead if(!Camp.isEmpty()) { system.debug(Camp[0].Name); l.Campaign_Name__c = Camp[0].Name; } } } //} } }

 

This may or may not work - you'll have to get all the fields you need in your SOQL statement when loading Lead l.

 

Done this quickly for you so won't be syntactically correct but may get you going again.

Good luck.

R.

Richie DRichie D

p.s. you'll also have to do an update on Lead l. e.g.

Update l;

 

R.

Cool_DevloperCool_Devloper

Hello,

 

There is another straighforward solution which you can implement.

 

For updating the field after the record is inserted, you can use the "@future" annotation to update the records again with the requisite values.

 

You need to pass the set of ID's of the Trigger.New records and put the corresponding logic into the @future method. This will allow you to update the records by creating a new context outside the trigger.

 

Cool_D