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
Mohd NabeelMohd Nabeel 

The code is making a copy of the record how can i prevent this??? The code is working fine the only thing is whenever i am updating the record it is creating a duplicate record.. how can i prevent this???

public static void HireContactForm(List<HireForm__c> HireList){
        List<Contact> contList = new List<Contact>();
        List<Case> CaseList = new List<Case>();
        
        for(HireForm__c hireForm: HireList){
            Contact con = new Contact();
            con.FirstName = hireForm.First_Name__c;
            con.LastName =  hireForm.Last_Name__c;
            con.Phone = hireForm.Phone__c;
            con.Email = hireForm.Email__c;
            contList.add(con);
        }
        insert contList;
        for(Contact cont: contList){
            Case cases = new Case();
            cases.Status = 'New';
            cases.ContactId = cont.Id;
            caseList.add(cases);
        }
        insert caseList;    
        
        
        for(HireForm__c hire: HireList){
            for(Case c: CaseList){
                if(hire.status__c == 'Completed'){
                    c.Status = 'Closed';
                }
                update c;
            }
            
        }
    }
//Trigger
trigger ContactFormTrigger on HireForm__c (before insert, before update, after update) {
    if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isBefore){
        AccountContact.HireContactForm(Trigger.new);
    }
}

 
Best Answer chosen by Mohd Nabeel
Ajay K DubediAjay K Dubedi
Hi Nabeel,

* Use below code it works fine in my org and makes sure there is no Contact exist that were created previously with this trigger:

Trigger--->
trigger ContactFormTrigger on HireForm__c (before insert,before Update) 
{
    if(Trigger.isBefore && Trigger.isInsert)
    {
        AccountContact.HireContactForm(Trigger.new);
    } 
    if(Trigger.isBefore && Trigger.isUpdate)
    {
         AccountContact.hireCaseList(Trigger.new);
    }
}
Handler Class-->
public class AccountContact 
{
    public static void hireContactForm(List<HireForm__c> HireList){
        List<Contact> contList = new List<Contact>();
        List<Case> CaseList = new List<Case>();
        for(HireForm__c hireForm: HireList)
        {
            if(hireForm.status__c == 'In Progress'){
                Contact con = new Contact();
                con.FirstName = hireForm.First_Name__c;
                con.LastName =  hireForm.Last_Name__c;
                con.Phone = hireForm.Phone__c;
                con.Email = hireForm.Email__c;
                contList.add(con);
            }   
            else
            {
                hireForm.addError('Select In Progress first, then try again');
            }
        }         
        insert contList;
        integer i=0;
        for(HireForm__c hireForm: HireList)
        {
            if(hireForm.status__c == 'In Progress'){
                hireForm.Candidate__c = contList[i].id; 
            }
        }
        for(Contact cont: contList){
            Case cases = new Case();
            cases.Status = 'New';
            cases.ContactId = cont.Id;
            caseList.add(cases);
        }
        insert caseList; 
    }
    public static void hireCaseList(List<HireForm__c> HireListNew){
        set<id> ContactId_Set = new set<id>();
        for(HireForm__c hire: HireListNew)
        {
            ContactId_Set.add(hire.Candidate__c);
        }
        List<Case> CaseList = [select id,ContactId from case where ContactId in:ContactId_Set];
        for(HireForm__c hire: HireListNew){
            for(Case c: CaseList){
                if(hire.Candidate__c == c.ContactId && hire.status__c == 'Completed'){
                    c.Status = 'Closed';
                }
                update c;
            }            
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Mohd,
Above code in both insert and update operation invokes HireContactForm method which creates contact and cases.A new record is being inserted during insert and once you update the HireForm__c record trigger again invokes HireContactForm method which then creates a duplicate record.
Invoke different methods for insert and update.In insert method write your logic to insert records and in update call fetch existing case record and do required changes to that record.

Please refer below link which might help you further.
https://salesforce.stackexchange.com/questions/145838/trigger-creating-duplicate-records

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
Ajay K DubediAjay K Dubedi
Hi,

* You can not stop duplication until you create a lookup field on contact to HireForm__c
  or else run this trigger on  AfterInsert only

Trigger--->
 
trigger ContactFormTrigger on HireForm__c (After insert) 
{
    if(Trigger.isAfter && Trigger.isInsert || Trigger.isUpdate)
    {
        AccountContact.HireContactForm(Trigger.new);
    } 
}

Apex class--->

public class AccountContact
{
    public static void HireContactForm(List<HireForm__c> HireList){
        List<Contact> contList = new List<Contact>();
        List<Case> CaseList = new List<Case>();
        Map<id,Contact> HireForm_Map = New Map<id,Contact>();
        for(HireForm__c hireForm: HireList){
            Contact con = new Contact();
            con.FirstName = hireForm.First_Name__c;
            con.LastName =  hireForm.Last_Name__c;
            con.Phone = hireForm.Phone__c;
            con.Email = hireForm.Email__c;
            contList.add(con);
        }
        insert contList;
        integer i = 0;
        for(HireForm__c hireForm: HireList){
            HireForm_Map.put(hireForm.id, contList[i]);
            i++;
        }
        for(HireForm__c hire: HireList){
            Contact cont = HireForm_Map.get(hire.id);
            Case cases = new Case();
            if(hire.status__c == 'Completed'){
                cases.Status = 'Closed';
            }
            else{
                cases.Status = 'New';
            }
            cases.ContactId = cont.Id;
            caseList.add(cases);
        }
        insert caseList;    
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Mohd NabeelMohd Nabeel
Hi Ajay, Thanks for ur answer.. Actually i want to achieve this task by the looup field and i am assigning the value of hireForm.Candidate__c = con.id; in the if condition but here i am confused that how will i get that id in my hireCaseList method to validate it.. while i have tried my best to do this.. but still stuck here.. the only thing is how will i invoke my second method.
public static void hireContactForm(List<HireForm__c> HireList){
        List<Contact> contList = new List<Contact>();
        List<Case> CaseList = new List<Case>();
        
        for(HireForm__c hireForm: HireList){
            
            Contact con = new Contact();
            con.FirstName = hireForm.First_Name__c;
            con.LastName =  hireForm.Last_Name__c;
            con.Phone = hireForm.Phone__c;
            con.Email = hireForm.Email__c;
            if(hireForm.status__C == 'In Progress'){
                contList.add(con);
                hireForm.Candidate__c = con.id; 
            }   
            else{
                hireForm.addError('Select In Progress first, then try again');
            }
        }         
        insert contList;
        for(Contact cont: contList){
            Case cases = new Case();
            cases.Status = 'New';
            cases.ContactId = cont.Id;
            caseList.add(cases);
        }
        insert caseList;    
    }
    public static void hireCaseList(List<HireForm__c> HireListNew){
        
        for(HireForm__c hire: HireListNew){
            for(Case c: CaseList){
                if(hire.status__c == 'Completed'){
                    c.Status = 'Closed';
                }
                update c;
            }            
        }
    }
Mohd NabeelMohd Nabeel
Just want to add one more thing that when i am trying to get the contact id in the candidate field then it is not showing in the hirefrom field..What am i doing wrong in ths???
Deepali KulshresthaDeepali Kulshrestha
Hi Mohd,


Greetings to you!

I have gone through your query please try below code:

Trigger
---------------------
trigger ContactFormTrigger on HireForm__c (before insert, before update, after update) {
    if(Trigger.isInsert && Trigger.isBefore){
        AccountContact.HireContactFormForInsert(Trigger.new);
    }
    if(Trigger.isUpdate && Trigger.isBefore)
    {
        AccountContact.HireContactFormForUpdate(Trigger.new);
    }
}

---------------------
Helper class
---------------------

public class AccountContact {
    
    public static void HireContactFormForInsert(List<HireForm__c> HireList){
        List<Contact> contList = new List<Contact>();
        List<Case> CaseList = new List<Case>();
        
        for(HireForm__c hireForm: HireList){
            Contact con = new Contact();
            con.FirstName = hireForm.First_Name__c;
            con.LastName =  hireForm.Last_Name__c;
            con.Phone = hireForm.Phone__c;
            con.Email = hireForm.Email__c;
            contList.add(con);
        }
        insert contList;
        for(Contact cont: contList){
            Case cases = new Case();
            cases.Status = 'New';
            cases.ContactId = cont.Id;
            caseList.add(cases);
        }
        insert caseList;    
    }
    public static void HireContactFormForUpdate(List<HireForm__c> HireList){        
        List<Case> NewCaseList=new List<Case>();
        NewCaseList=[Select Id,Status,Origin from Case];
        for(HireForm__c hire: HireList){
            for(Case c: NewCaseList){
                if(hire.status__c == 'Completed'){
                    c.Status = 'Closed';
                }
                update c;
            }
            
        }
    }
}
     
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
 
Mohd NabeelMohd Nabeel
Hi Devika, Thanku for the quick response but the only glitch in this code is that when i am updating any of the form field to completed then it is closing all the cases. It should close all those cases which are realted to that form..
Ajay K DubediAjay K Dubedi
Hi Nabeel,

* First you are not able to get contact id before you insert it.
* You have store con.id first and then insert your contList.    
Contact con = new Contact();
            con.FirstName = hireForm.First_Name__c;
            con.LastName =  hireForm.Last_Name__c;
            con.Phone = hireForm.Phone__c;
            con.Email = hireForm.Email__c;
            if(hireForm.status__C == 'In Progress'){
                contList.add(con);
                hireForm.Candidate__c = con.id;          <---Here
            }   
            else{
                hireForm.addError('Select In Progress first, then try again');
            }
            }         
            insert contList;
 
* If you want to invoke your method in Handler Class's Method use this way:

Trigger-->
trigger ContactFormTrigger on HireForm__c (before insert,before Update) 
{
    if(Trigger.isAfter && Trigger.isInsert || Trigger.isUpdate)
    {
        AccountContact.HireContactForm(Trigger.new);
    } 
}
Handler Class--->
public class AccountContact 
{
    public static void hireContactForm(List<HireForm__c> HireList){
        List<Contact> contList = new List<Contact>();
        List<Case> CaseList = new List<Case>();
        for(HireForm__c hireForm: HireList){
            Contact con = new Contact();
            con.FirstName = hireForm.First_Name__c;
            con.LastName =  hireForm.Last_Name__c;
            con.Phone = hireForm.Phone__c;
            con.Email = hireForm.Email__c;
            if(hireForm.status__C == 'In Progress'){
                contList.add(con);
                hireForm.Candidate__c = con.id; 
            }   
            else{
                hireForm.addError('Select In Progress first, then try again');
            }
        }         
        insert contList;
        for(Contact cont: contList){
            Case cases = new Case();
            cases.Status = 'New';
            cases.ContactId = cont.Id;
            caseList.add(cases);
        }
        insert caseList;    
        hireCaseList(HireList,caseList);
    }
    public static void hireCaseList(List<HireForm__c> HireListNew,List<Case> CaseList){    
        for(HireForm__c hire: HireListNew){
            for(Case c: CaseList){
                if(hire.status__c == 'Completed'){
                    c.Status = 'Closed';
                }
                update c;
            }            
        }
    }
}

* Or if you want to access second method on update you can do this:

Trigger--->
trigger ContactFormTrigger on HireForm__c (before insert,before Update) 
{
    if(Trigger.isBefore && Trigger.isInsert)
    {
        AccountContact.HireContactForm(Trigger.new);
    } 
    if(Trigger.isBefore && Trigger.isUpdate)
    {
         AccountContact.hireCaseList(Trigger.new);
    }
}
Handler Class---->
public class AccountContact 
{
    public static void hireContactForm(List<HireForm__c> HireList){
        List<Contact> contList = new List<Contact>();
        List<Case> CaseList = new List<Case>();
        for(HireForm__c hireForm: HireList){
            Contact con = new Contact();
            con.FirstName = hireForm.First_Name__c;
            con.LastName =  hireForm.Last_Name__c;
            con.Phone = hireForm.Phone__c;
            con.Email = hireForm.Email__c;
            if(hireForm.status__C == 'In Progress'){
                contList.add(con);
                hireForm.Candidate__c = con.id; 
            }   
            else{
                hireForm.addError('Select In Progress first, then try again');
            }
        }         
        insert contList;
        for(Contact cont: contList){
            Case cases = new Case();
            cases.Status = 'New';
            cases.ContactId = cont.Id;
            caseList.add(cases);
        }
        insert caseList; 
    }
    public static void hireCaseList(List<HireForm__c> HireListNew){
        set<id> ContactId_Set = new set<id>();
        for(HireForm__c hire: HireListNew)
        {
            ContactId_Set.add(hire.Candidate__c);
        }
        List<Case> CaseList = [select id,ContactId from case where ContactId in:ContactId_Set];
        for(HireForm__c hire: HireListNew){
            for(Case c: CaseList){
                if(hire.Candidate__c == c.ContactId && hire.status__c == 'Completed'){
                    c.Status = 'Closed';
                }
                update c;
            }            
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Mohd NabeelMohd Nabeel
Thanku Sir for you help, but i have copy pasted the above code but its still not working it is still showing New status on cases when i am selecting completed on the HireForm field. i will try to figure out what is going wrong.. Thanks a lot for ur help..
Deepali KulshresthaDeepali Kulshrestha
Hi Mohd,


I have gone through your query please replace below code for HireContactFormForUpdate() method:

public static void HireContactFormForUpdate(List<HireForm__c> HireList){        
        
        Set<String> Con_Phone = new Set<String>();
        for(HireForm__c hf : HireList)
        {
            if(hf.status__c == 'Completed')
            {
            Con_Phone.add(hf.Phone__c);
            }
        }
        List<Contact> Con_List =  new List<Contact>();
        Con_List = [Select Id,Name,Phone from contact where Phone in : Con_Phone];
        
        List<Case> NewCaseList=new List<Case>();
        NewCaseList=[Select Id,Status,Origin,ContactId from Case where ContactId in : Con_List];
        
        for(Contact con: Con_List){
            for(Case c: NewCaseList){
                if(c.ContactId == con.Id){
                    c.Status = 'Closed';
                }
                update c;
            }
            
        }
    }
     
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
 
Ajay K DubediAjay K Dubedi
Hi Nabeel,

* Use below code it works fine in my org and makes sure there is no Contact exist that were created previously with this trigger:

Trigger--->
trigger ContactFormTrigger on HireForm__c (before insert,before Update) 
{
    if(Trigger.isBefore && Trigger.isInsert)
    {
        AccountContact.HireContactForm(Trigger.new);
    } 
    if(Trigger.isBefore && Trigger.isUpdate)
    {
         AccountContact.hireCaseList(Trigger.new);
    }
}
Handler Class-->
public class AccountContact 
{
    public static void hireContactForm(List<HireForm__c> HireList){
        List<Contact> contList = new List<Contact>();
        List<Case> CaseList = new List<Case>();
        for(HireForm__c hireForm: HireList)
        {
            if(hireForm.status__c == 'In Progress'){
                Contact con = new Contact();
                con.FirstName = hireForm.First_Name__c;
                con.LastName =  hireForm.Last_Name__c;
                con.Phone = hireForm.Phone__c;
                con.Email = hireForm.Email__c;
                contList.add(con);
            }   
            else
            {
                hireForm.addError('Select In Progress first, then try again');
            }
        }         
        insert contList;
        integer i=0;
        for(HireForm__c hireForm: HireList)
        {
            if(hireForm.status__c == 'In Progress'){
                hireForm.Candidate__c = contList[i].id; 
            }
        }
        for(Contact cont: contList){
            Case cases = new Case();
            cases.Status = 'New';
            cases.ContactId = cont.Id;
            caseList.add(cases);
        }
        insert caseList; 
    }
    public static void hireCaseList(List<HireForm__c> HireListNew){
        set<id> ContactId_Set = new set<id>();
        for(HireForm__c hire: HireListNew)
        {
            ContactId_Set.add(hire.Candidate__c);
        }
        List<Case> CaseList = [select id,ContactId from case where ContactId in:ContactId_Set];
        for(HireForm__c hire: HireListNew){
            for(Case c: CaseList){
                if(hire.Candidate__c == c.ContactId && hire.status__c == 'Completed'){
                    c.Status = 'Closed';
                }
                update c;
            }            
        }
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer
Mohd NabeelMohd Nabeel
Thaanku so much sir.. It is working fine now.