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
BCSScottBCSScott 

Help with an update using Apex Trigger.

I have created a trigger on  our object LGA__c that will create and insert a new Event. I also want this trigger to update the created Event whenever the LGA__c is updated. To update the correct Event, I created a field called Event_ID__c in both the LGA__c and Event objects. When an LGA is created it auto creates an Event ID and passes that information to the created event. The problem I am having is using a Query to pull up the right event using the Event_ID__c. I have highlighted the exact line of code that is not working in red. I get the following error.

 

Error: Compile Error: unexpected token: 'lga.Event_ID__c' at line 34 column 81

 

Any help on how I can write that query to pull up the correct Event based off the Event_ID__c would be great. Thanks in advance.

 

 

 

trigger CreateLGAEvent on LGA__c (after insert, after update){

Map<String, LGA__c> lgaMap = new Map<String, LGA__c>();

List<Event> lgaEvents = new List<Event>();

    if(trigger.isinsert)
   
        for (LGA__c lga : System.Trigger.new){

            Event event = new Event(
           
                StartDateTime = lga.LGA_Start_DateTime__c ,
                EndDateTime = lga.LGA_End_Date_Time__c,
                Subject = 'LGA Scheduled',
                IsRecurrence = FALSE,
                Description = lga.Event_Description__c,
                Location = lga.Event_Subjects__c,
                Event_ID__c = lga.Event_ID__c,
                IsAllDayEvent = False);
               
            lgaEvents.add(event);
           
            insert lgaEvents;
             
           }
        
    else
   
        for (LGA__c lga : System.Trigger.New) {
       
        String eventID = lga.Event_ID__c;
              
            Event event = [Select id, Event_ID__c from Event where Event_ID__c = lga.Event_ID__c];
             
                event.StartDateTime = lga.LGA_Start_DateTime__c;
                event.EndDateTime = lga.LGA_End_Date_Time__c;
                event.Subject = 'LGA Scheduled';
                event.Description = lga.Event_Description__c;
                event.Location = lga.Event_Subjects__c;

            lgaEvents.add(event);

            update lgaEvents;
           
            }
             
}

Best Answer chosen by Admin (Salesforce Developers) 
ThomasTTThomasTT

When you refere a variable in the class from your query scope ([ ]), you need to put ":"(colon) before the variable, so it will be

 

 

Event event = [Select id, Event_ID__c from Event where Event_ID__c = :lga.Event_ID__c];

 

Your post is very easy to understand and to answer. I like it. I'll try to anser your posts every time!

ThomasTT

 

All Answers

ThomasTTThomasTT

When you refere a variable in the class from your query scope ([ ]), you need to put ":"(colon) before the variable, so it will be

 

 

Event event = [Select id, Event_ID__c from Event where Event_ID__c = :lga.Event_ID__c];

 

Your post is very easy to understand and to answer. I like it. I'll try to anser your posts every time!

ThomasTT

 

This was selected as the best answer
BCSScottBCSScott

Awesome. It always seems to be the little things I am missing. Thanks a ton for the help and I will try and make my posts like this in the future. I know sometimes it can be confusing to figure out what people are trying to say. Thanks again.

 

- JVH

ThomasTTThomasTT
Look around us... some posts are like "My code doen't work! Please help! Please! (period)". To understand the problem, 2 weeks with 10 posts from both sides, to provide a solution, 3 min... You already knew where was wrong, at least from error message, and you could imagine that I couldn't know where the step number indicated (some can't imagine that). You alredy solved 99% of your problem. You just needed a fresh eye. - ThomasTT