3

I need help taking the SOQL out of the for loop. I know I probably need to map the data. But how I would do this and achieve the same result I am not quite sure about. I know the answer is probably fairly simple as this is not a complicated block of code but I am unfamiliar with APEX and this type of syntax so any help would be greatly appreciated. Thank you.

trigger Add_Event_ID on Hum_data__c (before insert) {

    for (Hum_data__c h : Trigger.new) {
    
        String callEventID;
        callEventID = 
            [SELECT call__Event_ID__c 
            FROM call__Call_Detail__c 
            WHERE call__Call_Detail__c.call__Callid__c = :h.call__callid__c 
            LIMIT 1].call__Event_ID__c;
        
        h.call__Event_ID__c = callEventID;
    }
2

Try this code, here I'm assuming call__Callid__c is lookup field

trigger Add_Event_ID on Hum_data__c (before insert) {
    
    List<Id> callIdList = new List<Id>();
    for (Hum_data__c h : Trigger.new) {
        callIdList.add(h.call__callid__c);
    }
    
    Map<Id,Id> IdMap = new Map<Id,Id>();
    for(call__Call_Detail__c cc : [SELECT call__Event_ID__c,call__Callid__c 
            FROM call__Call_Detail__c 
            WHERE call__Callid__c IN :callIdList] ){
                IdMap.put(cc.call__Callid__c,cc.call__Event_ID__c);
        
    }
    
    for (Hum_data__c h : Trigger.new) {
        h.call__Event_ID__c = IdMap.get(h.call__callid__c);
    }
       
    }
| improve this answer | |
2

Here's a commented example of how this code could be restructured using a map to associate the id values to one another.

As an aside, don't use String for Id values. It is safer to use the correct data type whenever you can.

trigger Add_Event_ID on Hum_data__c(before insert) {
    // a place to store our results
    Map<Id, Id> callIdToEventIdMap = new Map<Id, Id>();

    // gather all of the call ids
    for (Hum_data__c h : Trigger.new) {
        callIdToCallDetailMap.put(h.call__callid__c, null);
    }

    // make the query
    for (call__Call_Detail__c detailRecord : [
        SELECT call__Event_ID__c
        FROM call__Call_Detail__c
        WHERE call__Callid__c IN :callIdToEventIdMap.keyset()
    ]) {
        // match the call id with the event id. this will also push out any duplicates
        callIdToEventIdMap.put(detailRecord.call__Callid__c, detailRecord.call__Event_ID__c);
    }

    // put the event id value from the map into the field on the trigger record
    for (Hum_data__c h : Trigger.new) {
        h.call__Event_ID__c = callIdToEventIdMap.get(h.call__callid__c);
    }
}
| improve this answer | |
0

This answer is similar to other two answers, but with some additional performance and safety check which I think should be a good practice, ie:

  1. Only run query when necessary
  2. Only do assignment when reference data exists

Generally the pattern is:

  1. Build a map collection to filter the query and store the corresponding query result
  2. Iterate the trigger list and do the assignment based on the map's key (from step #1)
trigger Add_Event_ID on Hum_data__c (before insert) {

    // Step 1

    // build the collection
    Map<Id, Id> eventIdBycallId = new Map<Id, Id>();
    for (Hum_data__c h : Trigger.new) {
        eventIdBycallId.put(h.call__callid__c, null);
    }
    // important! Only run SOQL query when the Map is not empty
    if (!eventIdBycallId.isEmpty()) {
        for (call__Call_Detail__c ccd : [
                SELECT call__Callid__c, call__Event_ID__c 
                FROM call__Call_Detail__c 
                WHERE call__Callid__c = :eventIdBycallId.keySet()
            ]) {
            eventIdBycallId.put(ccd.call__callid__c, ccd.call__Event_ID__c);
        }
    }
    
    // Step 2 

    for (Hum_data__c h : Trigger.new) {
        // important to check if your map has the corresponding key 
        // to prevent NullPointerException
        if (eventIdBycallId.containsKey(h.call__callid__c)) {
            h.call__Event_ID__c = eventIdBycallId.get(h.call__callid__c);
        }
        // ...
        // other lines of code
    }
}
| improve this answer | |

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.