0

I have a trigger on a child object called Interview. I would like to update a field on the parent object using a trigger on child object. Interviewer is a look up on interview object thus can have many interviewer related to the interview. Closed Date is a field on interview object which I check in my if condition( logic is irreverent). I have field on interviewer object called points__c(number). I like to add 1 to this field whenever the if(true) condition is satisfied. However in below code I am trying to figure out how to get the interviewer__c details inside the for loop so that I can just add =+ to the points__c field. If I put a SOQL i will not be bulkyfing the logic. Any suggestions?

    if(trigger.IsAfter && trigger.IsUpdate){


    for(Interview__c i : trigger.new){
    Interviewer__c intr = new Interviewer__c();
      if((i.Closed_Date__c!=trigger.oldMap.get(i.id).Closed_Date__c && trigger.oldMap.get(i.id).Closed_Date__c!=NULL) || 
              (trigger.oldMap.get(i.id).Closed_Date__c==NULL && i.Closed_Date__c!=NULL)){

// want to add number 1 to the value in the points__c field on Interviewer__c object for this interview

        }

    }

    }
2
  • see my answer below and let me know if it solves Commented Nov 2, 2016 at 4:41
  • @SantanuBoral I have used a different approach by creating a list of interviewer inside the if statement and using that list to update the point field on each interviewer. Thanks for the below response which looks interesting for me to read. i will check this out too.
    – SfdcBat
    Commented Nov 2, 2016 at 4:44

1 Answer 1

0

I have taken an approach like this below. Please note I have not compiled the code but tried to put my thoughts.

  1. Store incremented points for each Interviewer upon looping through Interview__c record
  2. Retrieve existing points from the Interviewer record firing SOQL
  3. Loop though the existing Interviewer records and add the points upon matching the Interviewer Id collected in step1.
  4. Finally update the Interviewer record.
if(trigger.IsAfter && trigger.IsUpdate){
Map<Id, Integer> mapInterviewerIds = new Map<Id,Integer>();
Integer pointCount = 0;

//calculate how many points to be added for each Interviewer
for(Interview__c i : trigger.new){
    Interviewer__c intr = new Interviewer__c();
    if((i.Closed_Date__c!=trigger.oldMap.get(i.id).Closed_Date__c && trigger.oldMap.get(i.id).Closed_Date__c!=NULL) || 
          (trigger.oldMap.get(i.id).Closed_Date__c==NULL && i.Closed_Date__c!=NULL)){
        //check if map contains the interviewer Id 
        if(mapInterviewerIds.containsKey(trigger.oldMap.get(i.id).Interviewer__c)){
            //then retrieve the pointCount
            pointCount = mapInterviewerIds.get(trigger.oldMap.get(i.id).Interviewer__c);
        }           
        pointCount = pointCount + 1;            
        mapInterviewerIds.put(trigger.oldMap.get(i.id).Interviewer__c,pointCount);
    }
    pointCount = 0; //initialize for next one
}

List<Interviewer__c> lstUpdate = new List<Interviewer__c>();    
//now retrieve the existing points record from the SOQL
if(lstInterviewIds.size()>0){
    Map<Id, Interviewer__c> interviewerMap = new Map<Id, Interviewer__c>([SELECT Id, points__c FROM Interviewer__c
                                                    WHERE Id IN:mapInterviewerIds.keySet()]);
    for(Id recordId:interviewerMap.keySet()){
        //retrieve the pointCount from DB
        Interviewer__c objRecord = interviewerMap.get(recordId);

        //add the points which has been collected in mapInterviewerIds and add that to points__c
        objRecord.points__c = objRecord.points__c + mapInterviewerIds.get(recordId);
        lstUpdate(objRecord);       
    }
    //finally update the lstUpdate to DB
}
}

Hope it makes sense.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .