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 Sharma 527Abhishek Sharma 527 

clarification on Apex program

Hello there, As new to development, I referred a piece of code and trying to understand this program, some of the statements didn't understand where I put question mark
can anyone spare some time to explain once plz

//handler code - 
public class NewClass { // class name

    public static void NewMethod(List<Parent__c> newParent) { //created list using custom object parent and named it newParent

        newParent = [SELECT Id, Name, Field1__c, Field2__c WHERE Id IN: newParent]; // fetching id, name and other fields where id is matching to newParent ? but with which other object they are comparing this ? are they fetching from newParent and assigning also in newParent ? plz explain this part

        List<Child__c> childQry = new List<Child__c>(); // list created for child object
        List<Child__c> childListToUpdate = new List<Child__c>(); //another list created for child object, did not understand why 2 list created here ?

        for(Parent__c p : newParent) { // Parent object reference variable p created which will run through records saved in newParent list

            childQry = [SELECT Id, Name, Parent__c, Field1__c, Field2__c FROM Child__c WHERE Field1__c =: p.Id]; // fetching records and assigning to list

            for(Child__c c : childQry) { // loop running through childQry records

                if(p.Id != null) { // null check
                    c.Field2__c = p.Field2__c; // assigning value to field2 from parent object field2
                    childListToUpdate.add(c);   // inserting record in new list
                }

            } 

        }   
        update childListToUpdate; // updating the record, did not understand what is meaning of this, didn't we already insert record in previous line ?
    }

}

//Trigger code 
trigger NewTrigger on Child__c (after update) { if(Trigger.isAfter && Trigger.isUpdate) { NewClass.NewMethod(Trigger.New); } }
Best Answer chosen by Abhishek Sharma 527
AnkaiahAnkaiah (Salesforce Developers) 
Hi Abhishek,

You have written a trigger on Child__c object and you need to pass the  Child__c object records to the NewClass method called NewMethod instead of parent__c records.

You were doing dml operation on same object then you can use before update instead of after update.

Modify the trigger like below.
 
trigger NewTrigger on Child__c (before update) { if(Trigger.isBefore && Trigger.isUpdate) { NewClass.NewMethod(Trigger.New); } }

Modify the class like below.
public class NewClass { // class name

public static void NewMethod(List<Child__c> newchilds) {// List of child records

//initialize set to store parent ids
set<id> parentids = new set<id>();

for(Child__c c:newchilds ){
if(c.Parent__c!=Null){
parentids.add(c.Parent__c)
}
}

Map<id,string> parentmapvalues = new Map<id,string>();

for(Parent__c p: [select id,Field2__c from Parent__c where id=:parentids AND Field2__c!=Null ]){

parentmapvalues.put(p.id,p.Field2__c);
}

for(Child__c c:newchilds ){

if(parentmapvalues.containskey(c.Parent__c)){
c.Field2__c = parentmapvalues.get(c.Parent__c);
}
}

}
}

If this helps, Please mark it as best answer.

Thanks!!