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
Suman BonthalaSuman Bonthala 

how to convert trigger into class

Trigger NoRelation on Candidate__c(After Insert,After Update)
{

    List<Candidate__c> updcanlist= new List<Candidate__c>();
    if(Utility.flag!=true)
    {
        map<Id,Id> canmap= new map<Id,Id>();
        for(Candidate__c c:trigger.new)
        {
            canmap.put(c.Id,c.Opportunity_Relation__c);
        }
        map<Id,string> oppmap= new map<Id,string>();
        List<opportunity> opplist= [Select id,name,Emp_Number__c from opportunity where Id IN:canmap.values()];
        for(opportunity opp:opplist)
        {
            oppmap.put(opp.Id,opp.Emp_Number__c);
        }
        map<string,Id> empmap= new map<string,Id>();
        List<Employee__c> emplist= [Select id,name,Emp_Number__c from Employee__c where Emp_Number__c IN:oppmap.values()];
        for(Employee__c emp:emplist)
        {
            empmap.put(emp.Emp_Number__c,emp.Id);
        }
        List<Candidate__c> canlist=[select id,name,Opportunity_Relation__c,Emp_RecordID__c from Candidate__c where Id IN:canmap.keyset()];
        for(Candidate__c can:canlist)
        {
            Candidate__c  c= new Candidate__c();
            c.Id=can.Id;
            c.Emp_RecordID__c=empmap.get(oppmap.get(can.Opportunity_Relation__c));
            
            updcanlist.add(c);
        }
    }
    Utility.flag=true;
    if(updcanlist.size()>0)
    {
        update updcanlist;
    }


}
Best Answer chosen by Suman Bonthala
Khan AnasKhan Anas (Salesforce Developers) 
Hi Suman,

Greetings to you!

Please try below code:

Trigger:
trigger NoRelation on Candidate__c(After Insert,After Update) {
     Handler_NoRelation.updateCandidate(trigger.new);
}

Handler Class:
public class Handler_NoRelation(After Insert,After Update) {

    public static void updateCandidate(List<Candidate__c> cand) {
        List<Candidate__c> updcanlist= new List<Candidate__c>();
        if(Utility.flag!=true) {
            map<Id,Id> canmap= new map<Id,Id>();
            for(Candidate__c c:cand) {
                canmap.put(c.Id,c.Opportunity_Relation__c);
            }
            map<Id,string> oppmap= new map<Id,string>();
            List<opportunity> opplist= [Select id,name,Emp_Number__c from opportunity where Id IN:canmap.values()];
           for(opportunity opp:opplist) {
              oppmap.put(opp.Id,opp.Emp_Number__c);
           }
           map<string,Id> empmap= new map<string,Id>();
            List<Employee__c> emplist= [Select id,name,Emp_Number__c from Employee__c where Emp_Number__c IN:oppmap.values()];
            for(Employee__c emp:emplist) {
                empmap.put(emp.Emp_Number__c,emp.Id);
            }
            List<Candidate__c> canlist=[select id,name,Opportunity_Relation__c,Emp_RecordID__c from Candidate__c where Id IN:canmap.keyset()];
            for(Candidate__c can:canlist) {
                Candidate__c  c= new Candidate__c();
                c.Id=can.Id;
              c.Emp_RecordID__c=empmap.get(oppmap.get(can.Opportunity_Relation__c));
            
               updcanlist.add(c);
           }
       }
       Utility.flag=true;
       if(updcanlist.size()>0) {
          update updcanlist;
       }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Ajay K DubediAjay K Dubedi
Hi Suman,
I found a link please refer to this below link:

https://developer.salesforce.com/forums/?id=906F0000000A1lmIAC

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Khan AnasKhan Anas (Salesforce Developers) 
Hi Suman,

Greetings to you!

Please try below code:

Trigger:
trigger NoRelation on Candidate__c(After Insert,After Update) {
     Handler_NoRelation.updateCandidate(trigger.new);
}

Handler Class:
public class Handler_NoRelation(After Insert,After Update) {

    public static void updateCandidate(List<Candidate__c> cand) {
        List<Candidate__c> updcanlist= new List<Candidate__c>();
        if(Utility.flag!=true) {
            map<Id,Id> canmap= new map<Id,Id>();
            for(Candidate__c c:cand) {
                canmap.put(c.Id,c.Opportunity_Relation__c);
            }
            map<Id,string> oppmap= new map<Id,string>();
            List<opportunity> opplist= [Select id,name,Emp_Number__c from opportunity where Id IN:canmap.values()];
           for(opportunity opp:opplist) {
              oppmap.put(opp.Id,opp.Emp_Number__c);
           }
           map<string,Id> empmap= new map<string,Id>();
            List<Employee__c> emplist= [Select id,name,Emp_Number__c from Employee__c where Emp_Number__c IN:oppmap.values()];
            for(Employee__c emp:emplist) {
                empmap.put(emp.Emp_Number__c,emp.Id);
            }
            List<Candidate__c> canlist=[select id,name,Opportunity_Relation__c,Emp_RecordID__c from Candidate__c where Id IN:canmap.keyset()];
            for(Candidate__c can:canlist) {
                Candidate__c  c= new Candidate__c();
                c.Id=can.Id;
              c.Emp_RecordID__c=empmap.get(oppmap.get(can.Opportunity_Relation__c));
            
               updcanlist.add(c);
           }
       }
       Utility.flag=true;
       if(updcanlist.size()>0) {
          update updcanlist;
       }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer