• SFDC_Newbie
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies

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;
             
                }
   
            }
   
         }
       
   
    }

}
}

When using the data loader, I cannot view the "Login IP Range" object in the "Select Data Objects" screen. I have the most recent version. Is this something that needs be made available to us? It would be extremely helpful to us when updating profiles and just being able to export our current allowed ranges. When updating profiles and just being able to export our current allowed ranges. 
When using the data loader, I cannot view the "Login IP Range" object in the "Select Data Objects" screen. I have the most recent version. Is this something that needs be made available to us? It would be extremely helpful to us when updating profiles and just being able to export our current allowed ranges. When updating profiles and just being able to export our current allowed ranges.