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
Shari SchultzShari Schultz 

Apex Trigger: Picklist Value on Contact Updates Checkbox on Custom Object

Hi everyone. I am trying to create an apex trigger with which a specific picklist criteria chosen on the Contact Object updates a checkbox on a custom object.

If Contact Object = Specific Field = "Completed", then
Custom Object = Checkbox = "True"

I'm have trouble getting the code right for the specific picklist option to update the checkbox.

Any thoughts?
AbhinavAbhinav (Salesforce Developers) 
Hi Sahri,

Relationship  between contact and custom object? Please share the code which you have tried.

Thanks!
CharuDuttCharuDutt
Hii Shari
Try Below Trigger
trigger TestTrigger on Contact(after insert, before update) {
set<Id>setConId = new Set<Id>();
list<Custom_Object__c> lstCoUpdate = new list<Custom_object__c>();
if((trigger.IsAfter && trigger.IsInsert )||(trigger.IsBefore && trigger.IsIpdate)){
    for(Contact Con :trigger.new){
        if(Con.Specific_Field__c == 'Completed'){
        setConId.Add(Con.Id);
        }
    }
}

list<Custom_Object__c>lstCo = [Select Id,Contact__c,CheckBox__c from Custom_Object__c where Contact__c IN:setConId];
for(Custom_object__c Co:lstCo){
    Co.CheckBox__C = True;
    lstCoUpdate.Add(Co);
}
if(lstCoUpdate.Size()>0){
    update lstCoUpdate;
}
}
Please Mark It As Best Answer If It Helps
Thank You! 

 
Todkar AishwaryaTodkar Aishwarya
Hi Shari,

This can be achieved using process builder if the custom object is a child of Contact.

Thanks!
Suraj Tripathi 47Suraj Tripathi 47
Hi Shari Schultz,
Greeting!

First, Define the relationship between Contact Object and Custom Object.
After that use the Trigger on Contact for After Update.
Use SOQL to fetch contacts with its related Custom object (if a Relationship exists between objects).

You can get Help from this :  
trigger RelatedRecord on Contact(after update)   
{    
    List<contact> contactList = new List<contact>([select id,LastName,(select id,name from customObjects__r) from Contact where id in : trigger.new]);
    for(contact con:contactList){
        if(con.customObjects__r.size() > 0 && con.SpecifiedField == 'Completed'){
            for(customObjects__c cusObj:con.customObjects__r){
                cusObj.chechBox = true;
            }
        }
    }
}

if you have any query please comment.
---------------
If you find your Solution then mark this as the best answer to close this question. 

Thank you!
Regards,
Suraj Tripathi