You need to sign in to do that
Don't have an account?
Mohd 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); } }
* 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---> Handler Class-->
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
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
* 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--->
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
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
* First you are not able to get contact id before you insert it.
* You have store con.id first and then insert your contList.
* If you want to invoke your method in Handler Class's Method use this way:
Trigger--> Handler Class--->
* Or if you want to access second method on update you can do this:
Trigger---> Handler Class---->
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
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
* 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---> Handler Class-->
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