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.
Error is coming from line belowList<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. ThanksSudhirpublic 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');
}
}
}
}

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