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
UrvikUrvik 

Apex Controller Help

Could someone please help why the following code is not working??

________

public class THUpdateLRO extends AbcTriggerHandler {
    private void updateLRO(List<License_Registration__c> newLROs) {
     

Set<id> AccIds = new Set<id>();

    for (License_Registration__c c : newLROs){
        AccIds.add(c.Current_Application__c);
    }

    Map<id, Application__c> AccIDmap = new Map<id, Application__c>([Select Id, Application_Outcome__c from Application__c
    Where Id in :AccIds]);
  
    for (License_Registration__c LicReg : newLROs){
       if(LicReg.Current_Application__c != null && LicReg.Current_Application__r.Application_Status__c == 'Withdrawn'){

       LicReg.Status__c = AccIDmap.get(LicReg.Current_Application__c).Application_Outcome__c; 
       }      
}
}

public override void OnBeforeInsert(SObject[] newObjects){
        updateLRO((List<License_Registration__c>)newObjects);
    }
public override void OnBeforeUpdate(SObject[]       newObjects,
                                       Map<Id,SObject> newObjectMap,
                                       SObject[]       oldObjects,
                                       Map<Id,SObject> oldObjectMap){
         updateLRO((List<License_Registration__c>)newObjects);
    }
}
Best Answer chosen by Urvik
Adnubis LLCAdnubis LLC
With just a quick glance I would say your issue might be that you don't have access to this field.

LicReg.Current_Application__r.Application_Status__c

When a trigger fires I believe it only gives you access to the objects direct fields. Since you are already building a map and grabbing those objects try something like this. 

for (License_Registration__c LicReg : newLROs){
Application__c application = AccIDmap.get(LicReg.Current_Application__c);
if(application != null && application.Application_Status__c == 'Withdrawn'){
  LicReg.Status__c = application.Application_Outcome__c;
}   
}

Don't forget to add that field to your query on the application__c object.

Map<id, Application__c> AccIDmap = new Map<id, Application__c>([Select Id, Application_Outcome__c,Application_Status__c from Application__c
    Where Id in :AccIds]);