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
sankumsankum 

how to analysis the trigger

hi

i have one scenario like after update

i have two objects like College and Graduates both objects have one same field status(picklist)  field values are :

Visible,Invisible and reject

if i update the college record it will also update the graduate record

i have code but i did not get step by step...

 

trigger CollegeAU on College__c (after update)
{
 List<College__c> vLstColl = trigger.new;
 List<String> vCollId = new List<String>();
 List<Student__c> vLstStd1 = new List<Student__c>();
 
 for(College__c vColl: vLstColl)   //Why to add to college id??
 {
  vCollId.add(vColl.Id);
 }
 
 List<Student__c> vLstStd = [select id, Name, Gender__c, Status__c, College__c from Student__c where College__c In :vCollId];   //explain this step also
 
 for(College__c vColl: vLstColl)
 {
     for(Student__c vStd: vLstStd)
     {
      if(vColl.Id == vStd.College__c)// explain here also
      {
       vStd.Status__c = vColl.Status__c;
       vLstStd1.add(vStd);
      }
     }
 }
 
 if(!vLstStd1.isEmpty())
 {
  update vLstStd1;

 

 


 and is there anysteps to analyse the trigger?Please send me 

Thanks in advance

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

 

1) The author wanted to get all of the record ID values for the records in the trigger, so this is how they did it. This design pattern is very common when you cannot retrieve the values you need through other means (there actually happens to be a better way, so this is unnecessary).

 

2) The author wanted to find all of the related students, so they queried a list based on the colleges that are in the trigger (from the ID values as in the first question). This is a valid way to find all children records of a list of parent records. There are other ways that the same goal can be achieved, which I outline below.

 

3) The author wanted to match the college from each student to the correct college so they could update the status of each student. Unaware of an efficient method to match the student to the college, they simply performed a brute force search method by comparing each college to each student's college, and if they matched, copied the status from one to the other. The performance of this search is O(m*n+n), where m is the number of colleges, and n is the number of students.

 

If you're trying to learn Apex Code, you should take this code as an example of how not to leverage Apex Code.

 

Here is how the code should have been written:

 

trigger CollegeAU on College__c(after update) {
	Student__c[] students = [SELECT Id,Name,Gender__c,Status__c,College__c FROM Student__c WHERE College__c IN :Trigger.new];
	for(Student__c record:students) {
		record.Status__c = Trigger.newMap.get(record.College__c).Status__c;
	}
	update students;
}

1) The system already gives us a way to get all records in the trigger through Trigger.new. You can query against it to find records without the first loop/map that was necessary. This has a reduction of n + 2 lines of execution, since the variables used no longer matter, and the loop of n records is no longer needed.

 

2) Trigger.newMap is a map that lets us retrieve the associated record for each item we are looping through without having to perform two loops. This reduces the matching complexity from O(m*n+n)  to O(n) (linear time) by eliminating one of the loops.

 

3) This isn't really vital (and it doesn't really matter), but there is no longer a need to check if a list of sobjects is empty before attempting to perform a DML on it. "Null" DML operations no longer fail with an error and do not count against governor limits; the author probably learned this syntax from an older piece of code, which is still perfectly valid, just cumbersome.