You need to sign in to do that
Don't have an account?

trigger to create multiple records when check box is checked
Hello everyone,
Can anyone help me out in writing a trigger on lead object.
On lead I have following fields
1)LastName
2)FirstName
3)Parent__c (Checkbox)
4)ParentFirstName__c
5)ParentSecondName__c
6)ParentEmail__c
Once lead is created then I want to create one record in contact if Parent__c = 'False'.
If Parent__c is true then I want to create two records in contact where one is for parent and other is for student(standard lead field belongs to student details).
If Parent__c is true then I need to catch the newly created contact Id's then I need to create a record in Relationship__c object where Contact1__c = ParentContactId and Contact__2=StudentContactId
Can anyone helps me out in writing trigge that is bulkified for multiple insertions lead please
Can anyone help me out in writing a trigger on lead object.
On lead I have following fields
1)LastName
2)FirstName
3)Parent__c (Checkbox)
4)ParentFirstName__c
5)ParentSecondName__c
6)ParentEmail__c
Once lead is created then I want to create one record in contact if Parent__c = 'False'.
If Parent__c is true then I want to create two records in contact where one is for parent and other is for student(standard lead field belongs to student details).
If Parent__c is true then I need to catch the newly created contact Id's then I need to create a record in Relationship__c object where Contact1__c = ParentContactId and Contact__2=StudentContactId
Can anyone helps me out in writing trigge that is bulkified for multiple insertions lead please
here is the code to create records based on your requirement.
I hope this solution is helpful for you. If it does please mark it as best answer.
Regards
karthik
thanks a lot for helping me out in this issue.
Can you please help me out how can I convert lead here, the moment lead created I will be creating contacts then I want to convert the lead too, after converting I want to merge with the contact that was created in trigger
I tried your code but I am getting many errors, I tried to fix them but failed.
My Code:
I did many changes to the code but on inserting lead record with student details(standard lead fields) and parent__c = true and ParentFname != null, parentLname != null then I am getting the following error
LeadTrigger: execution of AfterInsert caused by: System.ListException: List index out of bounds: 1 Class.LeadTriggerHandler.afterInsertProcess: line 41, column 1 Trigger.LeadTrigger: line 3, column 1
my code is below:
trigger: class:
I havent created the fields in my organization so I havent executed the code in my organization. Sorry for the inconvenience. I have corrected the code (your code). please check it.
Note: Looks like the field hed__Contact__c is not available in the Object hed__Relationship__c.
Regards
Karthik
To answer your comment in line no 37
>> //FRED: Does this actually works? Since the "get" returns a list of contact, I think the syntax should be something like
insertConMap.get(IdKey)[0].Id but I could be wrong
A : There is nothing wrong with the Syntax and you can use get. Try it out in execute anonymous
line no . : 69
>> //FRED QUESTION: If I understand correctly , you're building a Map of contacts by lead ID? Why is the loop set to "2"?
//Why is the RETURN ContactMap in the loop itself? It means it will always contain only ONE item in it.
//This return doesnt make sense to me
A: In my code I missed ou the return statement and you have added the return wrongly inside the loop. It should be outside the for loop.
Anyway I have corrected the errors in the code try it in your org.
Regards
Karthik
Thanks for helping me out here, I am able to modify all the changes and get it worked perfectly.
Actually I am converting lead now which is throwing the following error
LeadTrigger: execution of AfterInsert caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, accountId must be specified if contactId is not null: [Id] Class.LeadTriggerHandler.afterInsertProcess: line 84, column 1 Trigger.LeadTrigger: line 3, column 1
I did not specified AccountId in createcontactparent because, I have backend process which will create accountid once contact is inserted, can you help me out in this issue so that I can merge the contacts while converting lead.
Actually if parent checkbox is checked then I am creating relationship record and affiliation records, like that if parent checkbox is not checked then only one contact will get created now for that contact its creating affiliation records for only one lead, can you review it once plz.