Skip to main content
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
3 answers
  1. Jul 31, 2019, 9:24 AM
    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
0/9000