1

I have this batchclass with which I want to update some checkboxes on account with the values of related assets checkbox values. When running the class I get an System.NullPointerException: Attempt to de-reference a null object error.

I did some research and this could be because of a null value. I made a check on all of my fields but still get this error at the following line:

asst.account.Actief_onderhoud_Design_Solutions__c = asst.design_Solutions_Product__c;

Class

    global class BatchUpdateAccountByAsset implements
Database.Batchable<sObject>, Database.Stateful {        
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator (
            [SELECT account.Name, design_Solutions_Product__c, layertec_Product__c, simuleon_Product__c, account.Actief_onderhoud_Design_Solutions__c,
             account.Actief_onderhoud_Layertec__c, account.Actief_onderhoud_Simuleon__c
             FROM Asset 
             WHERE (design_Solutions_Product__c = true AND account.Actief_onderhoud_Design_Solutions__c = false) 
                    OR (layertec_Product__c = true AND account.Actief_onderhoud_Layertec__c  = false)
                    OR (simuleon_Product__c = true AND account.Actief_onderhoud_Simuleon__c = false)
             ]);        
    }
    global void execute(Database.BatchableContext bc, List<Asset> scope){        
        List<Account> acc = new List<Account>();
        if(scope.size()!=Null){
            for (Asset asst : Scope) {
                if(asst.design_Solutions_Product__c !=null && asst.account.Actief_onderhoud_Design_Solutions__c !=null) {
                    asst.account.Actief_onderhoud_Design_Solutions__c = asst.design_Solutions_Product__c;
                }
                if(asst.layertec_Product__c !=null && asst.account.Actief_onderhoud_Layertec__c !=null) {
                    asst.account.Actief_onderhoud_Layertec__c = asst.layertec_Product__c;
                    }
                if(asst.layertec_Product__c !=null && asst.account.Actief_onderhoud_Simuleon__c !=null) {
                    asst.account.Actief_onderhoud_Simuleon__c = asst.simuleon_Product__c;
                    }
                acc.add(asst.account);
            } 
            update acc;
        }
    }
4

You can't directly set these parent fields from the Asset record.

You have to query for the related Account and set it's Actief_onderhoud_Design_Solutions__c directly.

So you'll prob need to do another query on all the Account ids, put that result in a map and get the related Account that way.

Also, there is no need to perform a null check here:

if(scope.size()!=Null){

You sometimes might need to do something like:

if(scope!= null && scope.size() > 0){

But the loop won't start if there is nothing in scope

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.