Ad

How To Set Checkbox To True With Trigger When Related Objects Are Created

enter image description here This is relation between two obj. I want to code a trigger which makes Enrolled field from false to true (unchecked to checked) when Student is created with College record.

Ad

Answer

You don't need code for this. A formula field of type Checkbox and value !ISBLANK(College__c) would be enough.

Or workflow field update / flow / process builder with condition ISNEW() || ISCHANGED(College__c) and action Enrolled = College not equal to blank

If you're sure it needs code - this is decent start (but ideally you'd move logic to helper class, don't keep it straight in trigger)

trigger StudentTrigger on Student__c (before insert, before update){
    for(Student__c s : trigger.new){
        s.Enrolled__c = s.College__c != null;
    }
}
Ad
source: stackoverflow.com
Ad