I have this batchclass where I update a checkbox on account based on related assets. How do I set the checkbox to true If 1 of the assets is true and to false if all assets are false.
I would have to loop trough all the related assets but then I would have a loop in an if statement so how do I bulkify this.
Here is what I have so far where the account gets updated with the last value of the Asset in the query:
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator (
[SELECT account.Name, accountId, Actief__c , account.As__c
FROM Asset
WHERE (Actief__c = true AND account.As__c = false)
OR (Actief__c = false AND account.As__c = true)
]);
}
global void execute(Database.BatchableContext bc, List<Asset> scope){
Map<Id, Account> accounts = new Map<Id, Account>();
Account tempRecord;
for (Asset record: Scope) {
if(record.AccountId == null) { continue; }
if((tempRecord = accounts.get(record.AccountId)) == null) {
tempRecord = new Account(Id=record.AccountId);
}
if(record.Actief__c !=null && record.account.As__c !=null) {
tempRecord.As__c = record.Actief__c ;
accounts.put(record.AccountId, tempRecord);
}
}
update accounts.values();
}