+ Start a Discussion
CloudNerdCloudNerd 

Error in FOR Loop APEX Code

Hello Dev Community,

We are getting the following error for the APEX Trigger code below:

"Error: Compile Error: unexpected token: '{' at line 35 column 64"

We are trying to iterate over a custom object "Project" and add child objects records "Milestones" based on the value of certain fields in the "Project" related object "Opportunity". I think there may be something wrong with the FOR loop syntax? Any help would be appreciated!
 
Trigger newMilestone on MPM4_BASE__Milestone1_Project__c (before insert){

    List<MPM4_BASE__Milestone1_Project__c> mileProj = [SELECT Id FROM MPM4_BASE__Milestone1_Project__c WHERE MPM4_BASE__Status__c='Set'];
    List<MPM4_BASE__Milestone1_Milestone__c> miles = new List<MPM4_BASE__Milestone1_Milestone__c>();
    
    for (MPM4_BASE__Milestone1_Project__c p : mileProj){
        if (p.Opportunity__r.OrderHardware__c == TRUE) {
            MPM4_BASE__Milestone1_Milestone__c morder = new MPM4_BASE__Milestone1_Milestone__c();
            morder.MPM4_BASE__Project__c = p.Id;
            morder.Name = 'Order Hardware';
            morder.MPM4_BASE__Kickoff__c = date.today();
            morder.OwnerId = '0051a000000STEt';
            miles.add(morder);
        }    else if (p.Opportunity__r.SchedulePSG__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c porder = new MPM4_BASE__Milestone1_Milestone__c();
                porder.MPM4_BASE__Project__c = p.Id;
                porder.Name = 'Schedule PSG';
                porder.MPM4_BASE__Kickoff__c = date.today();
                porder.OwnerId = '0051a000000R80f';
                miles.add(porder);
        }    else if (p.Opportunity__r.ScheduleRental__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c rorder = new MPM4_BASE__Milestone1_Milestone__c();
                rorder.MPM4_BASE__Project__c = p.Id;
                rorder.Name = 'Schedule Rental';
                rorder.MPM4_BASE__Kickoff__c = date.today();
                rorder.OwnerId = '0051a000000STEt';
                miles.add(rorder);
        }    else if (p.Opportunity__r.SendWelcome__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c worder = new MPM4_BASE__Milestone1_Milestone__c();
                worder.MPM4_BASE__Project__c = p.Id;
                worder.Name = 'Schedule Welcome';
                worder.MPM4_BASE__Kickoff__c = date.today();
                worder.OwnerId = '0051a000000R80f';
                miles.add(worder);
        }    else (p.Opportunity__r.ScheduleOffsite__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c oorder = new MPM4_BASE__Milestone1_Milestone__c();
                oorder.MPM4_BASE__Project__c = p.Id;
                oorder.Name = 'Schedule Offsite';
                oorder.MPM4_BASE__Kickoff__c = date.today();
                oorder.OwnerId = '0051a000000STEt';
                miles.add(oorder);
    }
   }
   
   insert miles;
}

 
Best Answer chosen by CloudNerd
KaranrajKaranraj
Use "else if" instead of else in the line number 35. Here is the updated code below.
I strongly recommend not to use hardcoded record id in the apex code, try getting the value dynamically 
Trigger newMilestone on MPM4_BASE__Milestone1_Project__c (before insert){

    List<MPM4_BASE__Milestone1_Project__c> mileProj = [SELECT Id FROM MPM4_BASE__Milestone1_Project__c WHERE MPM4_BASE__Status__c='Set'];
    List<MPM4_BASE__Milestone1_Milestone__c> miles = new List<MPM4_BASE__Milestone1_Milestone__c>();
    
    for (MPM4_BASE__Milestone1_Project__c p : mileProj){
        if (p.Opportunity__r.OrderHardware__c == TRUE) {
            MPM4_BASE__Milestone1_Milestone__c morder = new MPM4_BASE__Milestone1_Milestone__c();
            morder.MPM4_BASE__Project__c = p.Id;
            morder.Name = 'Order Hardware';
            morder.MPM4_BASE__Kickoff__c = date.today();
            morder.OwnerId = '0051a000000STEt';
            miles.add(morder);
        }    else if (p.Opportunity__r.SchedulePSG__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c porder = new MPM4_BASE__Milestone1_Milestone__c();
                porder.MPM4_BASE__Project__c = p.Id;
                porder.Name = 'Schedule PSG';
                porder.MPM4_BASE__Kickoff__c = date.today();
                porder.OwnerId = '0051a000000R80f';
                miles.add(porder);
        }    else if (p.Opportunity__r.ScheduleRental__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c rorder = new MPM4_BASE__Milestone1_Milestone__c();
                rorder.MPM4_BASE__Project__c = p.Id;
                rorder.Name = 'Schedule Rental';
                rorder.MPM4_BASE__Kickoff__c = date.today();
                rorder.OwnerId = '0051a000000STEt';
                miles.add(rorder);
        }    else if (p.Opportunity__r.SendWelcome__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c worder = new MPM4_BASE__Milestone1_Milestone__c();
                worder.MPM4_BASE__Project__c = p.Id;
                worder.Name = 'Schedule Welcome';
                worder.MPM4_BASE__Kickoff__c = date.today();
                worder.OwnerId = '0051a000000R80f';
                miles.add(worder);
        }    else if (p.Opportunity__r.ScheduleOffsite__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c oorder = new MPM4_BASE__Milestone1_Milestone__c();
                oorder.MPM4_BASE__Project__c = p.Id;
                oorder.Name = 'Schedule Offsite';
                oorder.MPM4_BASE__Kickoff__c = date.today();
                oorder.OwnerId = '0051a000000STEt';
                miles.add(oorder);
    }
   }
   
   insert miles;
}

 

All Answers

KaranrajKaranraj
Use "else if" instead of else in the line number 35. Here is the updated code below.
I strongly recommend not to use hardcoded record id in the apex code, try getting the value dynamically 
Trigger newMilestone on MPM4_BASE__Milestone1_Project__c (before insert){

    List<MPM4_BASE__Milestone1_Project__c> mileProj = [SELECT Id FROM MPM4_BASE__Milestone1_Project__c WHERE MPM4_BASE__Status__c='Set'];
    List<MPM4_BASE__Milestone1_Milestone__c> miles = new List<MPM4_BASE__Milestone1_Milestone__c>();
    
    for (MPM4_BASE__Milestone1_Project__c p : mileProj){
        if (p.Opportunity__r.OrderHardware__c == TRUE) {
            MPM4_BASE__Milestone1_Milestone__c morder = new MPM4_BASE__Milestone1_Milestone__c();
            morder.MPM4_BASE__Project__c = p.Id;
            morder.Name = 'Order Hardware';
            morder.MPM4_BASE__Kickoff__c = date.today();
            morder.OwnerId = '0051a000000STEt';
            miles.add(morder);
        }    else if (p.Opportunity__r.SchedulePSG__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c porder = new MPM4_BASE__Milestone1_Milestone__c();
                porder.MPM4_BASE__Project__c = p.Id;
                porder.Name = 'Schedule PSG';
                porder.MPM4_BASE__Kickoff__c = date.today();
                porder.OwnerId = '0051a000000R80f';
                miles.add(porder);
        }    else if (p.Opportunity__r.ScheduleRental__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c rorder = new MPM4_BASE__Milestone1_Milestone__c();
                rorder.MPM4_BASE__Project__c = p.Id;
                rorder.Name = 'Schedule Rental';
                rorder.MPM4_BASE__Kickoff__c = date.today();
                rorder.OwnerId = '0051a000000STEt';
                miles.add(rorder);
        }    else if (p.Opportunity__r.SendWelcome__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c worder = new MPM4_BASE__Milestone1_Milestone__c();
                worder.MPM4_BASE__Project__c = p.Id;
                worder.Name = 'Schedule Welcome';
                worder.MPM4_BASE__Kickoff__c = date.today();
                worder.OwnerId = '0051a000000R80f';
                miles.add(worder);
        }    else if (p.Opportunity__r.ScheduleOffsite__c == TRUE) {
                MPM4_BASE__Milestone1_Milestone__c oorder = new MPM4_BASE__Milestone1_Milestone__c();
                oorder.MPM4_BASE__Project__c = p.Id;
                oorder.Name = 'Schedule Offsite';
                oorder.MPM4_BASE__Kickoff__c = date.today();
                oorder.OwnerId = '0051a000000STEt';
                miles.add(oorder);
    }
   }
   
   insert miles;
}

 
This was selected as the best answer
CloudNerdCloudNerd
Thank you Karanraj! That worked. I agree with the static ID's not being used, but how would you then query user ID's based on matching values for the new "Milestones" being created?

EX:

Milestone A = User A
Milestone B = User A
Milestone C = User B
etc....
Neha Raizada!Neha Raizada!
The initial query should also be updated to include opportunity related fields otherwise it will give a System.SobjectException for not querying the respective fields i.e.
List<MPM4_BASE__Milestone1_Project__c> mileProj = [SELECT Id,Opportunity__r.OrderHardware__c,Opportunity__r.ScheduleOffsite__c,Opportunity__r.SendWelcome__c,Opportunity__r.ScheduleRental__c,Opportunity__r.SchedulePSG__c FROM MPM4_BASE__Milestone1_Project__c WHERE MPM4_BASE__Status__c='Set'];

One option to not hardcode would be to use custom setting to store the user id and milestone mapping. As the user ids change from environment to environment, custom setting could be easily updated with the user ids from the environment and could be dynamically accessed in the code.
To do that Create a List custom setting e.g.UserMilestoneMapping(api name UserMilestoneMapping__c), create a field on the setting e.g. UserId(api name UserId__c). Using the Manage button populate the custom setting with Milestone name in the name field(e.g. Order Hardware) and ownerid in the userId field(e.g. 0051a000000STEt), similarly populate other mappings as well-
Schedule PSG,0051a000000R80f
Schedule Rental, 0051a000000STEt
Schedule Welcome, 0051a000000R80f
Schedule Offsite, 0051a000000STEt

Access the custom setting in the Apex code -
morder.ownerId = UserMilestoneMapping__c.getValues('Order Hardware').UserId__c;
porder.OwnerId = UserMilestoneMapping__c.getValues('Schedule PSG').UserId__c;
rorder.OwnerId = UserMilestoneMapping__c.getValues('Schedule Rental').UserId__c;
worder.OwnerId = UserMilestoneMapping__c.getValues('Schedule Welcome').UserId__c;
oorder.OwnerId = UserMilestoneMapping__c.getValues('Schedule Offsite').UserId__c;
Change the user ids when migrating to pre-prod and prod environments. Let me know if that worked.