0

I am trying to get the value of formula field to check in condition via customAccMap. i am checking if formula_field__c is null then i need to throw a cpqException. But on Add Product Page on CPQ i am getting "column 1: Attempt to de-reference a null object". Can anyone please help me.

public static void cannotChooseAcc(List<SBQQ__QuoteLine__c> newLines){

    if(!newLines.isEmpty())
    {   
        Set<Id> customObjectAcc = new Set<Id>();
        for(SBQQ__QuoteLine__c newQL :newLines)
        {   
        if((newQL.OppRecordTypeId__c == Label.newOppRecordTypeId__c)
               customObjectAcc.add(newQL.custom_Account__c);
        }
        Map<Id,custom_Account__c> customAccMap = new Map<Id,custom_Account__c>([Select Id,formula_field__c from custom_Account__c where Id IN : customObjectAcc]);
        if(!customAccMap.isEmpty()){
            for(SBQQ__QuoteLine__c newQL :newLines){

                if(customAccMap.get(newQL.custom_Account__c).formula_field__c == null) {

                     throw new CPQException('You cannot select this account');
                }
            }
        }
    }        
}
  • The map may not contain the record you think it does since you populate from a query it may not be empty. Check containskey before you blindly dereference it in your get call – Eric Feb 4 at 5:21
1

A cleaner solution would be to modify the query, and then you just check for the presence of the ID value:

public static void cannotChooseAcc(List<SBQQ__QuoteLine__c> newLines) {
    // Get custom account ID values
    Set<Id> customObjectAcc = new Set<Id>();
    for(SBQQ__QuoteLine__c newQL :newLines) {
        if(newQL.OppRecordTypeId__c == Label.newOppRecordTypeId__c) {
            customObjectAcc.add(newQL.custom_Account__c);
        }
    }
    // Query for accounts with null formula field in the list
    Set<Id> unavailable = new Map<Id, Custom_Account__c>([
        SELECT Id FROM Custom_Account__c WHERE Formula_Field__c = NULL AND Id = :customObjectAcc
    ]).keySet();
    // If the ID is in the Set, the formula was null
    for(SBQQ__QuoteLine__c newQL :newLines){
        if(unavailable.contains(newQL.Custom_Account__c)) {
            throw new CPQException('You cannot select this account');
        }
    }        
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.