1

I'm facing an issue understanding a trigger provided by Trailhead (link here),

There is a List of Ids declared and filled in a loop, and in that loop only a custom object is taken back (not its Id !) ;

I thought 'well, it's not really accurate, let's add the .id' (of course, the custom object does have an .id attribute) ;

But when modifying the code as told, I got an error !

Can you explain me why ? Is my logical point of view false ?

Thanks for your help, Alexis

Nb : here is the entier trigger

trigger RejectDoubleBooking on Session_Speaker__c (before insert, before update) {
    //collect ID's to reduce data calls
    List<Id> speakerIds = new List<Id>();
    Map<Id,DateTime> requested_bookings = new Map<Id,DateTime>();

    //get all speakers related to the trigger
    //set booking map with ids to fill later
    for(Session_Speaker__c newItem : trigger.new) {
        requested_bookings.put(newItem.Session__c,null);
        speakerIds.add(newItem.Speaker__c);
    }
    //fill out the start date/time for the related sessions
    List<Session__c> related_sessions = [SELECT ID, Session_Date__c from Session__c WHERE ID IN :requested_bookings.keySet()];
    for(Session__c related_session : related_sessions) {
        requested_bookings.put(related_session.Id,related_session.Session_Date__c);
    }
    //get related speaker sessions to check against
    List<Session_Speaker__c> related_speakers = [SELECT ID, Speaker__c, Session__c, Session__r.Session_Date__c from Session_Speaker__c WHERE Speaker__c IN :speakerIds];
    //check one list against the other
    for(Session_Speaker__c requested_session_speaker : trigger.new) {
        DateTime booking_time = requested_bookings.get(requested_session_speaker.Session__c);
        for(Session_Speaker__c related_speaker : related_speakers) {
            if(related_speaker.Speaker__c == requested_session_speaker.Speaker__c &&
               related_speaker.Session__r.Session_Date__c == booking_time) {
                   requested_session_speaker.addError('The speaker is already booked at that time');
               }
        }
    }
}

And my specific logical issue is there :

enter image description here


Thanks Santanu,

It's more master-detailed relationship (*) ;

In the below line, newItem.Session__c is actually referring to the object based on the Id

requested_bookings.put(newItem.Session__c,null);

I do not understand how it's a reference to an Id ?

I'm not really at ease with the way SOQL works with relationship queries, I think I've a problem visualising how SOQL works, to travel foreign key relationship ...

In the second part of your answer, you said :

Now, if you need to use .Id then you have use relationship just like as follows

requested_bookings.put(newItem.Session__r.Id,null);

I see here, you're requesting the __R --> relation, Does the trigger work the same way if I do this ?

How can I learn about manipulating the relations, then ?

Thanks again

(*) : enter image description here

2

As per my understanding you have lookup fields like Session__c and Speaker__c in the Session_Speaker__c object.

In the below line, newItem.Session__c is actually referring to the object based on the Id.

requested_bookings.put(newItem.Session__c,null);

Now, if you need to use .Id then you have use relationship just like as follows

requested_bookings.put(newItem.Session__r.Id,null);

Update based on your answer

If you try to fetch only the recordId from SOQL just use

SELECT Session__c FROM Session_Speaker__c WHERE ...

If you try to fetch other attributes like Name etc then use

SELECT Session__r.Name FROM Session_Speaker__c WHERE ...

Here Id is implicit and no need to use explicitly as Session__r.Id rather use the field api name as Session__c.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.