You need to sign in to do that
Don't have an account?
Abhishek 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); } }
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); } }
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.
Modify the class like below.
If this helps, Please mark it as best answer.
Thanks!!