0

How can I update a checkbox from false to true based on a condition using apex trigger?

I have three custom objects: ObjectA__c, ObjectB__c, and ObjectC__c

And here are some of fields:

ObjectA__c (Id, Name)

ObjectB__c (Id, Name, ObjectA__c, Checkbox__c)

ObjectC__c (Id, ObjectA__c)

The requirement is that if a specific ObjectA__c has a related ObjectC__c record, check the Checkbox__c field from ObjectB__c related to that ObjectA__c record.

Apex Trigger:

trigger updateCheckbox on ObjectC__c(after insert) {   

    List<ObjectC__c> relatedObjC = [SELECT Id,Name,ObjectA__c FROM ObjectC__c
 WHERE ObjectA__c IN :Trigger.New];

    List<ObjectB__c> relatedobjB = [SELECT Id, Name, Checkbox__c FROM ObjectB__c WHERE ObjectA__c IN :Trigger.New];         

    List<ObjectB__c> objBToUpdate = new List<ObjectB__c>();

    for(ObjectC__c oc : relatedObjC) { 

        for(ObjectB__c ob : relatedobjB) {    

            if (oc.ObjectA__c != null && ob.ObjectA__c != null) {

                ob.Checkbox__c = true;

                objBToUpdate.add(ob);

            }
        }

    }

    update objBToUpdate;

} 
  • Welcome to SF.SE sfnewbie21. Please visit the Help center and read "How to Ask". If you show us your code from what you've tried so far and where you're stuck, I'm confident you'll find plenty of people who'll be willing to assist you. If you don't know where to start, I recommend you go through the Apex Trigger Module on Trailhead. – crmprogdev Nov 27 '16 at 18:08
  • @crmprogdev My apologies, just a beginner here :) By the way, kindly see my updated post. There, I included the apex trigger I am working at, but I am not so sure how to make that work. I tried looking for scenarios like mine but most examples only include one or two objects. – sfnewbie21 Nov 27 '16 at 18:55
1

According to what you're showing, ObjectC__c has a lookup on it to ObjectA__c, so there's no need to run a query to get the related ObjectA__c record. That information will already be contained in Trigger.new.

However, you will need to find the correct ObjectB__c record to set the checkbox on it, or else create it. It's not clear to me how that record gets created.

Your trigger should look more like the following:

trigger updateCheckbox on ObjectC__c(after insert) {   

    map<Id,Id>cIdToAId = new map<Id,Id>();    
    for(ObjectC__c c:Trigger.new){
        If(null != c.Object__A){
        // I'm making the assumption that not all Object__c records are related to Ojbect__A records.
        // If that's not correct, then the If statement shouldn't be necessary.
        // You may also need to use a try-catch to avoid nullpointerexceptions if there are null values

           cIdToAId.put(c.Id, c.Object__A);

         }
    }


    List<ObjectB__c> relatedobjB = [SELECT Id, Name, ObjectA__c, Checkbox__c FROM ObjectB__c WHERE ObjectA__c IN :cIdToAId.values()];        

    set<Id>objectAIds = new set<Id>();
    objectAIds.addAll(cIdToAId.values());

    for(ObjectB__c ob : relatedobjB) {    

        if(objectAIds.contains(ob.ObjectA__c)){    
            ob.Checkbox__c = true;

        }

    }

    update relatedobjB;

}

Note: The above code has not been debugged or tested.

|improve this answer|||||
  • Thanks for your reply. I tried to follow what you have suggested, except that I removed the if statement since all ObjectC__c records are related to ObjectA__c records. But there's this error: Method does not exist or incorrect signature: [List<Id>].contains(Id) , which pertains to this line if(cIdToAId.values().contains(ob.ObjectA__c)) – sfnewbie21 Nov 28 '16 at 3:51
  • You can't use contains() with a list, only works with a set or a map. Values() converts the map to a list. Just use map.contains(). My bad. – crmprogdev Nov 28 '16 at 6:30
  • I modified the line to this if(cIdToAId.contains(ob.ObjectA__c)), but there's this another error displaying: Method does not exist or incorrect signature: [Map<Id,Id>].contains(Id) – sfnewbie21 Nov 28 '16 at 6:59
  • Again, my bad. Maps can use containskey(), but not contains() by itself. See edited code above which should solve the issue. If you have any additional issues. Please see How do I start to debug my own Apex code?. – crmprogdev Nov 28 '16 at 13:42
  • It is finally working! I definitely have to read a lot more about triggers. Thanks for your guidance and help :) – sfnewbie21 Nov 28 '16 at 17:33

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.