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
MaheemSamMaheemSam 

Before Delete thorws Attempt to de-reference a null object

Hi, 
    
    I am trying to implement a delete validation on opportunity object using a trigger but I keep getting Attempt to de-reference a null object error please suggest me what is the issue in this code. 
public static void processOptyDelete(List<Opportunity> newLst){

    List<SBQQ__Quote__c> cpqqt = [select id,SBQQ__Opportunity2__c from SBQQ__Quote__c where SBQQ__Opportunity2__c = :trigger.newmap.keyset()];
     for(Opportunity opp: newLst){
       for(SBQQ__Quote__c sqt : cpqqt){
          if(opp.id == sqt.SBQQ__Opportunity2__c){
             opp.addError('Opportunity have Quotes ,so you can not delete this opportunity');
           }
       }  
     } 
 }

Error is coming from line below
List<SBQQ__Quote__c> cpqqt = [select id,SBQQ__Opportunity2__c from SBQQ__Quote__c where SBQQ__Opportunity2__c = :trigger.newmap.keyset()];

Please let me know if there are any other way to fix this issue. 

Thanks
Sudhir
Ajay K DubediAjay K Dubedi
Hi MaheemSam,
Try this:
public static void processOptyDelete(List<Opportunity> newLst){
List<SBQQ__Quote__c> cpqqt = [select id,SBQQ__Opportunity2__c from SBQQ__Quote__c where SBQQ__Opportunity2__c IN:newLst];
     for(Opportunity opp: newLst){
       for(SBQQ__Quote__c sqt : cpqqt){
          if(opp.id == sqt.SBQQ__Opportunity2__c){
             opp.addError('Opportunity have Quotes ,so you can not delete this opportunity');
           }
       }  
     } 
 }
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi MaheemSam,

Try the following code it works for you:
public static void processOptyDelete(List<Opportunity> newLst){
try 
{
List<SBQQ__Quote__c> cpqqt = [select Id, SBQQ__Opportunity2__c from SBQQ__Quote__c where SBQQ__Opportunity2__c IN : trigger.newmap.keyset() AND SBQQ__Opportunity2__c != null];
if(cpqqt.size() > 0) {
for(Opportunity opp: newLst){
for(SBQQ__Quote__c sqt : cpqqt){
if(opp.id == sqt.SBQQ__Opportunity2__c){
opp.addError('Opportunity have Quotes ,so you can not delete this opportunity');
}
} 
} 
}
} catch(Exception ex)
{
System.debug('Exception on Line Number ' + ex.getLineNumber() + ' Message ---- ' + ex.getMessage());
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
MaheemSamMaheemSam
Thansk Ajay and Deepali I tried noth of your methods using below code earlier as well still getting same null pointer error. 
 
public static void processOptyDelete(List<Opportunity> newLst){
List<SBQQ__Quote__c> cpqqt = [select id,SBQQ__Opportunity2__c from SBQQ__Quote__c where SBQQ__Opportunity2__c IN:newLst];
     for(Opportunity opp: newLst){
       for(SBQQ__Quote__c sqt : cpqqt){
          if(opp.id == sqt.SBQQ__Opportunity2__c){
             opp.addError('Opportunity have Quotes ,so you can not delete this opportunity');
           }
       }  
     } 
 }


method 2
List<SBQQ__Quote__c> cpqqt = [select Id, SBQQ__Opportunity2__c from SBQQ__Quote__c where SBQQ__Opportunity2__c IN : trigger.newmap.keyset() AND SBQQ__Opportunity2__c != null]; if(cpqqt.size() > 0) { for(Opportunity opp: newLst){ for(SBQQ__Quote__c sqt : cpqqt){ if(opp.id == sqt.SBQQ__Opportunity2__c){ opp.addError('Opportunity have Quotes ,so you can not delete this opportunity'); } } 
I dont want to use try catch because if there are any other error it will be difficult to debug. 

Please let me know if there are any alternative methods

Thanks
Sudhir