0

I am trying to create a Trigger on an object 'Education History' and the trigger is supposed to check if an 'Affiliation' record already exists based on comparing two fields and update that Affiliation or create a new Affiliation and copy few fields.

trigger EducationHistoryTrigger on EnrollmentrxRx__Education_History__c (After Insert, After Update) {
    if(trigger.isInsert || trigger.isUpdate){
        set<EnrollmentrxRx__Education_History__c> educList=new set<EnrollmentrxRx__Education_History__c>();
        for(EnrollmentrxRx__Education_History__c edu: trigger.new){
            educList.add(edu);
        }
        if(educList.size()>0){
            EducationHistoryTriggerHandler EHTH=new EducationHistoryTriggerHandler();
            EHTH.createAffiliation(educList);
        }
    }
}


public class EducationHistoryTriggerHandler{
    public void createAffiliation(set <EnrollmentrxRx__Education_History__c> educ )
    { 
      system.debug('< EHTH Inside createAffiliation');
      system.debug('* EHTH Education History set: '+educ);
        List<hed__affiliation__c> affList=new list<hed__Affiliation__c>();
        List<hed__affiliation__c> affiliationTobeInserted=new list<hed__Affiliation__c>();
        List<hed__affiliation__c> affiliationTobeUpdated=new list<hed__Affiliation__c>();
        for(EnrollmentrxRx__Education_History__c eH:educ)
        {
           system.debug('* EHTH Education History field account is: '+eH.Educational_Institution__c+ ' and contact is '+ eH.EnrollmentrxRx__Application__r.EnrollmentrxRx__Applicant__r.name);            
            affList=[select id, hed__account__c, hed__contact__c 
                     from hed__Affiliation__c 
                     where hed__account__C=:eH.Educational_Institution__c 
                     AND hed__contact__c=:eH.EnrollmentrxRx__Application__r.EnrollmentrxRx__Applicant__c limit 1];
                     system.debug('* EHTH Affiliation returned list is: '+affList);       

            if(affList.size()>0)
            {
                system.debug('* EHTH Affiliation exists and updating the Affiliation');            
                hed__Affiliation__c aff=affList[0];
                aff.Application__c=eH.EnrollmentrxRx__Application__c;
                aff.CEEB_Code__c=eh.EnrollmentrxRx__CEEB_Code__c;
                aff.End_Date__c=eH.EnrollmentrxRx__End_Date__c;
                aff.Start_Date__c=eH.EnrollmentrxRx__Start_Date__c;
                aff.Graduated_MM_YYYY__c=eH.Graduated_MM_YYYY__c;
                system.debug('* EHTH Affiliation to be updated: '+aff);
                affiliationTobeUpdated.add(aff);
            }
        }

               if(affiliationTobeUpdated.size()>0)
            {
                try{
                    update affiliationTobeUpdated;
                }

                catch(Exception e){
                    system.debug(e);
                }
            }


             if(affList.isEmpty())
            {
                for(EnrollmentrxRx__Education_History__c ed:educ)
                {
                    if(ed.Educational_Institution__c!=null)
                    {
                     system.debug('* EHTH Education History record triggering update is: '+ed);
                        hed__Affiliation__c aff=new hed__Affiliation__c(hed__Account__c=ed.Educational_Institution__c, 
                                                                        hed__contact__c=ed.EnrollmentrxRx__Application__r.EnrollmentrxRx__Applicant__c,
                                                                        Application__c=ed.EnrollmentrxRx__Application__c,
                                                                        CEEB_Code__c=ed.EnrollmentrxRx__CEEB_Code__c,
                                                                        End_Date__c=ed.EnrollmentrxRx__End_Date__c,
                                                                        Start_Date__c=ed.EnrollmentrxRx__Start_Date__c,
                                                                        Graduated_MM_YYYY__c=ed.Graduated_MM_YYYY__c
                                                                       );
                      system.debug('* EHTH Affiliation to be inserted: '+aff);
                        affiliationTobeInserted.add(aff);
                    }
                }
            }

            if(affiliationTobeInserted.size()>0)
            {
                try{
                    insert affiliationTobeInserted;
                }

                catch(Exception e){
                    system.debug(e);
                }
        }
    } 
}

EnrollmentrxRx__Application__c is a Master object and the EnrollmentrxRx__Applicant__c is a lookup on Application.

The issue I have is, I need to check if Affiliation already exists by looking up eH.EnrollmentrxRx__Application__r.EnrollmentrxRx__Applicant__r.name, and it's always coming as null, even though there is value in it. I am not sure what I am doing wrong and any suggestion would be very much appreciated.

4

The terminology you're using introduces some confusion (master-detail relationships and lookup relationships are different), but the core of your question is this

The issue I have is, I need to check if Affiliation already exists by looking up 'eH.EnrollmentrxRx__Application__r.EnrollmentrxRx__Applicant__r.name', and it's always coming as null

The underlying issue is simple. You're trying to pull data from related objects from a trigger context variable.

Trigger Context Variables (like trigger.new, and trigger.oldMap) only contain the data stored on the triggered object itself. If you need to use more than one period/dot/full-stop, as you are when you try to access eH.EnrollmentrxRx__Application__r.EnrollmentrxRx__Applicant__r.name, the result will be null if eH was taken from a trigger context variable (which is precisely what your code is currently doing).

To fix this, you'll need to write a query to explicitly pull this data, and then use the record instance from the result of that query instead of the trigger context variable.

A brief example to illustrate my point

trigger EducationHistoryTrigger on EnrollmentrxRx__Education_History__c (After Insert, After Update) {

    EducationHistoryTriggerHandler EHTH=new EducationHistoryTriggerHandler();

    // Do an explicit query for the data you need from related records
    List<EnrollmentrxRx__Education_History__c> educationHistoryWithRelatedData = [SELECT Id, EnrollmentrxRx__Application__r.EnrollmentrxRx__Applicant__r.name FROM EnrollmentrxRx__Education_History__c WHERE Id IN :trigger.newMap.keySet()];

    // ...and pass the result of that query instead of passing
    //   records from your trigger context variables
    EHTH.createAffiliation(educationHistoryWithRelatedData);
}
  • Thank you, that worked! Your explanation was very helpful – Sooraj Aug 28 '18 at 23:50

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.