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
Hermann OuréHermann Ouré 

knowledge__kav System.NullPointerException: Attempt to de-reference a null object

Hello,
I am unable to fix the error 
System.NullPointerException: Attempt to de-reference a null object on my trigger.

Could someone help?
Thanks
 
trigger Knowledge_kavKnownErrorSubscription on Knowledge__kav (after insert, after update) {
    
    List<Known_Error_Subscription__c> kesList = new List<Known_Error_Subscription__c>();
    
    for(Knowledge__kav kav : [SELECT KnowledgeArticleId, Known_Error_Status__c, VersionNumber, (SELECT Knowledge__c FROM Known_Errors_Subscriptions__r)
                              FROM Knowledge__kav WHERE Id IN :Trigger.New]) {
                                                                              
            if(kav.KnowledgeArticleId != null && (Trigger.oldMap.get(kav.Id).LastPublishedDate != Trigger.newMap.get(kav.Id).LastPublishedDate)) {
            Known_Error_Subscription__c kes = kav.Known_Errors_Subscriptions__r;
            kes.Knowledge__c = kav.KnowledgeArticleId;
            kesList.add(kes);
            
        }
                                  
    }
    if(kesList.size() > 0) {
        update kesList;
    }
    

}

 
Best Answer chosen by Hermann Ouré
Abhishek BansalAbhishek Bansal
Hi,

You can use the below updated code:
trigger Knowledge_kavKnownErrorSubscription on Knowledge__kav (after insert, after update) {
    
    List<Known_Error_Subscription__c> kesList = new List<Known_Error_Subscription__c>();
    
    for(Knowledge__kav kav : [SELECT KnowledgeArticleId, Known_Error_Status__c, VersionNumber, (SELECT Knowledge__c FROM Known_Errors_Subscriptions__r)
                              FROM Knowledge__kav WHERE Id IN :Trigger.New]) {
                                                                              
            if(kav.KnowledgeArticleId != null && (trigger.isInsert || (Trigger.oldMap.get(kav.Id).LastPublishedDate != Trigger.newMap.get(kav.Id).LastPublishedDate))) {
            Known_Error_Subscription__c kes = kav.Known_Errors_Subscriptions__r;
            kes.Knowledge__c = kav.KnowledgeArticleId;
            kesList.add(kes);
            
        }
                                  
    }
    if(kesList.size() > 0) {
        update kesList;
    }
    

}

Let me know if  there is any issue.

Thanks,
Abhishek Bansal.

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Hermann,

Greetings!

You would need to add the logic in the if block whenever you are trying to access or use the list.

Reference:https://help.salesforce.com/HTViewSolution?id=000063739

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri 
Abhishek BansalAbhishek Bansal
Hi,

There is no old map in case of after insert so you need to add additional checks in order to access the trigger.oldMap only in case if the record is updated. Let me know if you need any other help or informaiton on this.

Thanks,
Abhishek Bansal.
Hermann OuréHermann Ouré

Hi Abhishek,
I would appreciate if you could give me extra informations on how to access trigger.oldMap
Thanks,
Hermann

 

Abhishek BansalAbhishek Bansal
Hi,

You can use the below updated code:
trigger Knowledge_kavKnownErrorSubscription on Knowledge__kav (after insert, after update) {
    
    List<Known_Error_Subscription__c> kesList = new List<Known_Error_Subscription__c>();
    
    for(Knowledge__kav kav : [SELECT KnowledgeArticleId, Known_Error_Status__c, VersionNumber, (SELECT Knowledge__c FROM Known_Errors_Subscriptions__r)
                              FROM Knowledge__kav WHERE Id IN :Trigger.New]) {
                                                                              
            if(kav.KnowledgeArticleId != null && (trigger.isInsert || (Trigger.oldMap.get(kav.Id).LastPublishedDate != Trigger.newMap.get(kav.Id).LastPublishedDate))) {
            Known_Error_Subscription__c kes = kav.Known_Errors_Subscriptions__r;
            kes.Knowledge__c = kav.KnowledgeArticleId;
            kesList.add(kes);
            
        }
                                  
    }
    if(kesList.size() > 0) {
        update kesList;
    }
    

}

Let me know if  there is any issue.

Thanks,
Abhishek Bansal.
This was selected as the best answer