0

I need to update the 'Is Cloud Customer' check box in the account object when at least one of the Child's as the Installation type field containing the word 'Cloud'.

If none of the Child as 'Cloud' in the installation type field then the 'Is Cloud Customer' checkbox in the account needs to be unchecked.

if(Trigger.isUpdate) {
   Set<Id> accounts = new Set<Id>();
   Set<Product_Delivered__c> prodsDeliveredAcc = new Set<Product_Delivered__c>();

    for (SObject so : Trigger.new) {
            Product_Delivered__c prodDelivered = (Product_Delivered__c) so;
            SObject pdOldObj = System.Trigger.oldMap.get(prodDelivered.Id);
            Product_Delivered__c oldProdDel = (Product_Delivered__c) pdOldObj;
            if(oldProdDel.Installation_Type__c != prodDelivered.Installation_Type__c && (oldProdDel.Installation_Type__c == isCloud || prodDelivered.Installation_Type__c == isCloud)){
                accounts.add(prodDelivered.Account__c);
             }

             system.debug('Accounts:: ' + accounts);
             for(Product_Delivered__c allProdsOfAccount: [SELECT Name,Account__c,Account__r.Is_Cloud_Customer__c, Installation_Type__c FROM Product_Delivered__c WHERE Account__c IN :accounts AND Installation_Type__c LIKE '%Cloud%' ]){
                 system.debug('cloud products:: ' + allProdsOfAccount.Installation_Type__c);
                 prodsDeliveredAcc.add(allProdsOfAccount);
             }

             List<Account> updateAcc = new List<Account>();
             If(prodsDeliveredAcc.size() > 0){
                 system.debug('Products delivered:: ' + prodsDeliveredAcc);
                 for(Product_Delivered__c proDel: prodsDeliveredAcc){
                     proDel.Account__r.Is_Cloud_Customer__c = true;
                     updateAcc.add(proDel.Account__r);
                 }
              update updateAcc;
             }

            }

What i am doing in my code is to query all Products Delivered(Child) for the Account and if there are any with Installation type containing 'cloud' i just add it to the prodsDeliveredAcc list and if the list has something then the Account will have the 'IS CLoud Customer' field checked.

What i need also to do is to uncheck the 'Is Cloud Customer' checkbox in the case there aren't any Cloud Products. So i need to find a way to capture the AccountId(Account__c in Product_Deliverede__c) when there are any Cloud Child. In order to update the Account.IS_Cloud_Customer__c field.

BR

3
  • Is Products Delivered object in master-detail or lookup to Account? Feb 16 '20 at 0:11
  • @DavidCheng, is in a Mater-Detail Relationship to Account obj
    – Leo
    Feb 17 '20 at 9:06
  • Have you tried using a Roll-up summary field instead and making your checkbox field a formula field that references the roll-up summary? This will work not work if Installation type field is a long textarea, though. Feb 18 '20 at 15:17

Your Answer

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

Browse other questions tagged or ask your own question.