• BCSScott
  • NEWBIE
  • 0 Points
  • Member since 2009

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

I am new to Apex and have just finished building my first trigger. Before I set up the test code I wanted to make sure that I wasn't going to submit code that was efficient or poorly written. According to an article I read, it is considered poor coding to include a SOQL query within a For loop. I am posting my code in this message and would really appreciate any advice on how to clean up my code so that it works as efficiently as possible.

 

I have highlighted the section I think might be poorly written in red.

 

 

 

 

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) {

          

            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;

            

            }

              

}

 

 

Thanks in advance.

 

 

JVH

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

I am new to Apex and have just finished building my first trigger. Before I set up the test code I wanted to make sure that I wasn't going to submit code that was efficient or poorly written. According to an article I read, it is considered poor coding to include a SOQL query within a For loop. I am posting my code in this message and would really appreciate any advice on how to clean up my code so that it works as efficiently as possible.

 

I have highlighted the section I think might be poorly written in red.

 

 

 

 

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) {

          

            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;

            

            }

              

}

 

 

Thanks in advance.

 

 

JVH

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