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
TibUbqTibUbq 

Mystery Trigger, I'm unsure of what it's doing

I've come across a trigger in our system but i'm not sure what it's doing? Could anyone take a look and see if they can break down what exactly it's doing?

trigger Trigger_Name on Account (after insert) {

    Class_Name CN1 = new Class_Name();
    CN1.Account_Link(Trigger.new);       
}   
 
public with sharing class Class_Name {
public void Account_Link(list<account> acs){
    list<account> marked_acs = new list<account>();
    for(Account ac : acs){
        if(ac.industry == 'Link'){
             marked_acs.add(ac);
        }
     }     
    list<Account_Link__c> Link_acs = new list<Account_Link__c>();
    for(Account ac : marked_acs){
Linked_Account__c con_ac = new Linked_Account__c(name = ac.name, account__c = ac.id);
Linked_acs.add(con_ac);
        }
        insert Linked_acs;       
    }   
}
 
Best Answer chosen by TibUbq
Maharajan CMaharajan C
Hi ,

The Main purpose of this trigger is creating the Linked Account Custom Object Record when the new Account is Link Industry.

trigger Trigger_Name on Account (after insert) {
    Class_Name CN1 = new Class_Name();    // Intializing  the Helper Class
    CN1.Account_Link(Trigger.new);       // passing the newly inserted accounts to the helper. 
}    
 
public with sharing class Class_Name {
public void Account_Link(list<account> acs){
    list<account> marked_acs = new list<account>();
    for(Account ac : acs){
        if(ac.industry == 'Link'){        //if the incoming account industry is Link then that Account data is added to marked_acs list to proceed further
             marked_acs.add(ac);
        }
     }      
    list<Account_Link__c> Link_acs = new list<Account_Link__c>();   
    for(Account ac : marked_acs){        // Iterating the above list which holds the industrt Accounts.
Linked_Account__c con_ac = new Linked_Account__c(name = ac.name, account__c = ac.id);    //  Creating the Child Record data for industry                                                                                                                                               //account to insert in the Linked Account Custom object
Linked_acs.add(con_ac);
        }
        insert Linked_acs;        // insert the Linked Account records in Linked Account Custom object
    }    
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C

All Answers

Raj VakatiRaj Vakati
This code is working fine without any issue 

The trigger is doing the below think 

Whenever Account record is created .. its check for the accoun industry. if account industry is Link then its going to create a new child record for the account called Linked_Account__c ..
 
Maharajan CMaharajan C
Hi ,

The Main purpose of this trigger is creating the Linked Account Custom Object Record when the new Account is Link Industry.

trigger Trigger_Name on Account (after insert) {
    Class_Name CN1 = new Class_Name();    // Intializing  the Helper Class
    CN1.Account_Link(Trigger.new);       // passing the newly inserted accounts to the helper. 
}    
 
public with sharing class Class_Name {
public void Account_Link(list<account> acs){
    list<account> marked_acs = new list<account>();
    for(Account ac : acs){
        if(ac.industry == 'Link'){        //if the incoming account industry is Link then that Account data is added to marked_acs list to proceed further
             marked_acs.add(ac);
        }
     }      
    list<Account_Link__c> Link_acs = new list<Account_Link__c>();   
    for(Account ac : marked_acs){        // Iterating the above list which holds the industrt Accounts.
Linked_Account__c con_ac = new Linked_Account__c(name = ac.name, account__c = ac.id);    //  Creating the Child Record data for industry                                                                                                                                               //account to insert in the Linked Account Custom object
Linked_acs.add(con_ac);
        }
        insert Linked_acs;        // insert the Linked Account records in Linked Account Custom object
    }    
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C
This was selected as the best answer