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
lovetolearnlovetolearn 

ERROR: Initial term of field expression must be a concrete SObject

Hi, 

 

I am trying to create a junction object between two custom objects. I keep getting this error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Holiday__c> at line 7 column 32. Here is my code:

 

trigger createHoliday on Leave_Request__c (after insert) {
    List<LR_and_Holiday__c> lrholidaylist = new List<LR_and_Holiday__c>();
    List<Holiday__c> allHoliday=[SELECT ID FROM Holiday__c Where Holiday__c.Holiday_Start_Date__c > Today];
    for(Leave_Request__c insertLR : Trigger.new){
        LR_and_Holiday__c lrholiday = new LR_and_Holiday__c();
        lrholiday.Leave_Request__c = insertLR.ID;
        lrholiday.Holiday__c = allholiday.ID;
        lrholidaylist.add(lrholiday);
    }
    Database.insert(lrholidaylist);
}

 Please help. 

 

Thank you. 

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force
trigger createHoliday on Leave_Request__c (after insert) {
    List<LR_and_Holiday__c> lrholidaylist = new List<LR_and_Holiday__c>();
    List<Holiday__c> allHoliday=[SELECT ID FROM Holiday__c Where Holiday__c.Holiday_Start_Date__c > Today];
    for(Leave_Request__c insertLR : Trigger.new){
        LR_and_Holiday__c lrholiday = new LR_and_Holiday__c();
        lrholiday.Leave_Request__c = insertLR.ID;
        lrholiday.Holiday__c = allholiday[0].ID;
        lrholidaylist.add(lrholiday);
    }
    Database.insert(lrholidaylist);
}

 

At line number 7, you have accessed Id from list directly, For time being I have accessed first element of that list

 

you can access different records based upon index., first you need to clear which record you want to access

 

 

 

Thanks,

bForce

All Answers

b-Forceb-Force
trigger createHoliday on Leave_Request__c (after insert) {
    List<LR_and_Holiday__c> lrholidaylist = new List<LR_and_Holiday__c>();
    List<Holiday__c> allHoliday=[SELECT ID FROM Holiday__c Where Holiday__c.Holiday_Start_Date__c > Today];
    for(Leave_Request__c insertLR : Trigger.new){
        LR_and_Holiday__c lrholiday = new LR_and_Holiday__c();
        lrholiday.Leave_Request__c = insertLR.ID;
        lrholiday.Holiday__c = allholiday[0].ID;
        lrholidaylist.add(lrholiday);
    }
    Database.insert(lrholidaylist);
}

 

At line number 7, you have accessed Id from list directly, For time being I have accessed first element of that list

 

you can access different records based upon index., first you need to clear which record you want to access

 

 

 

Thanks,

bForce

This was selected as the best answer
lovetolearnlovetolearn

I would want to access all holiday records that are greater than the start date of the Leave Request Record, but less than the end date of the leave request record. I edited my SOQL statement to reflect this, but I get this error: Compile Error: unexpected token: 'startdate' at line 3 column 100. Here is my code: 


trigger createHoliday on Leave_Request__c (after insert) {
    List<LR_and_Holiday__c> lrholidaylist = new List<LR_and_Holiday__c>();
    List<Holiday__c> allHoliday=[SELECT ID FROM Holiday__c Where Holiday__c.Holiday_Start_Date__c > startdate];
    Leave_Request__c lrequest = new Leave_Request__c();
    Date startdate = lrequest.Request_Start_Date__c;
    for(Leave_Request__c insertLR : Trigger.new){
        LR_and_Holiday__c lrholiday = new LR_and_Holiday__c();
        lrholiday.Leave_Request__c = insertLR.ID;
        lrholiday.Holiday__c = allholiday[0].ID;
        lrholidaylist.add(lrholiday);
    }
    Database.insert(lrholidaylist);
}

 

Would I have to change my for loop to define a finite number of junction records that I want to create. For example, if there are three Holiday records that are between the start and end date of the Leave Request, I would want the loop to break after three loops.

 

Thanks for all your help.

b-Forceb-Force

what is API name for holiday end date on Holiday object ?

 

 

Thanks,

bForce

lovetolearnlovetolearn

Holiday__c for the holiday object. Start date custom field is Start_Date__c

For Leave Request, the object API name is Leave_Request__c and Start date is Request_Start_date__c and end date is Request_End_date__c.

 

Thank you.