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
roni shoreroni shore 

check null value in oldMap

Hi Guys,
getting variable does not exist when checking the old value is null or not. please suggest

 public static Boolean HasAccess(List<AccountContactRelation> lstNew) {
               for (AccountContactRelation acr : lstNew) {
                system.debug('trigOld-->'+trigger.oldMap.get(acr.Id));
                
                    if(string.isNotBlank(acr.Entity__c) || string.isNotBlank(trigger.oldMap.get(acr.Id).External_Entity__c)  ){// getting  error on this line checking oldmap
                    
                        if(acr.External_Entity__c != 'Customer-facing')
                            throw new acrException(System.Label.Cimited);
                    }
                }
                  return bReturn;
            }
       
mirkimirki
Hi,

Modify your if statement to check if the Trigger.OldMap has the Id value.

if(string.isNotBlank(acr.Entity__c) || (Trigger.OldMap.keySet().contains(acr.Id) && string.isNotBlank(trigger.oldMap.get(acr.Id).External_Entity__c) ) )

Regards,
mirki
roni shoreroni shore
still same error
mirkimirki
What is this line producing in the debug log: system.debug('trigOld-->'+trigger.oldMap.get(acr.Id));
Raj VakatiRaj Vakati
Try this 

public static Boolean HasAccess(List<AccountContactRelation> lstNew) {
               for (AccountContactRelation acr : lstNew) {
                system.debug('trigOld-->'+trigger.oldMap.get(acr.Id));
                
                    if(acr.Entity__c!=NULL || Trigger.oldMap.get(acr.Id).External_Entity__c!=NULL  ){// getting  error on this line checking oldmap
                    
                        if(acr.External_Entity__c != 'Customer-facing')
                            throw new acrException(System.Label.Cimited);
                    }
                }
                  return bReturn;
            }
Raj VakatiRaj Vakati
Or change your method as below .. pass the map form the trigger it self
public static Boolean HasAccess(List<AccountContactRelation> lstNew , Map<Id,AccountContactRelation> oldMapVal) {
               for (AccountContactRelation acr : lstNew) {
                system.debug('trigOld-->'+trigger.oldMap.get(acr.Id));
                
                    if(acr.Entity__c!=NULL || oldMapVal.get(acr.Id).External_Entity__c!=NULL  ){// getting  error on this line checking oldmap
                    
                        if(acr.External_Entity__c != 'Customer-facing')
                            throw new acrException(System.Label.Cimited);
                    }
                }
                  return bReturn;
            }

 
NimitNimit
Make sure you are not calling this method from beforetrigger.

Also try to use "trigger.oldMap.get(acr.Id) != null" in if condition.