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
Abhishek chauhan 20Abhishek chauhan 20 

two Obj parent obj= Object_1__c and Child obj= Object_2__c in a parent obj there is a filed "Status__c" which is picklist and in child obj there is a field "Form_Submitted" which is checkbox i want to mark Status as Complete only when childs are "Checked"

and also both having look relation field name " Child_Of__c "
Best Answer chosen by Abhishek chauhan 20
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abhishek,

Can you try below trigger.
 
trigger UpdateParentStatus on Object_2__c (after insert, after update, after delete, after undelete) {

    // Get a set of all the parent object IDs for the child records being processed
    Set<Id> parentIds = new Set<Id>();
    
    If(Trigger.isupdate|| trigger.isinsert|| trigger.isundelete){
    for (Object_2__c childRecord : Trigger.new) {
        parentIds.add(childRecord.Child_Of__c );
    }
    }
    if(Trigger.isdelete){
    for (Object_2__c childRecord : Trigger.old) {
        parentIds.add(childRecord.Child_Of__c );
    }
    }
    
    // Query for all the parent records with the matching IDs and their related child records
    Map<Id, Object_1__c> parentMap = new Map<Id, Object_1__c>([
        SELECT Id, Name, Status__c, 
            (SELECT Id, Name, Form_Submitted__c FROM Object_2s__r )
        FROM Object_1__c WHERE Id IN :parentIds
    ]);
    
       for (Object_1__c parentRecord : parentMap.values()) {
        boolean allChildRecordsChecked = true;
        for (Object_2__c childRecord : parentRecord.Object_2s__r) {
            if (!childRecord.Form_Submitted__c) {
                allChildRecordsChecked = false;
                break;
            }
        }
        
        // If all child records are checked, update the parent record's status field to "Complete"
        if (allChildRecordsChecked) {
            parentRecord.Status__c = 'Complete';
        }
        else {
            parentRecord.Status__c = null;
        }
    }
    
    // Update the parent records
    update parentMap.values();
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,