I created a trigger on Lead object which creates two records in Contact at a time. Now I want to catch those newly created two contacts and creates a record in relationship object.

I did completed the trigger but I am getting error on lead object when I save the record. "ContactCreationFromLead: execution of AfterInsert caused by: System.ListException: List index out of bounds: 1 Trigger.ContactCreationFromLead: line 56, column 1"

Can anyone help me out in this issue,my trigger is below.

trigger ContactCreationFromLead on Lead (After insert) {
List<Contact> conInsertList = new List<Contact>();
List<hed__Affiliation__c> affiliation = new List <hed__Affiliation__c>();
List<hed__Relationship__c> relation = new List<hed__Relationship__c>();
List<String> listEmail = new List<String>();
if(trigger.isInsert || trigger.isUpdate){
for (Lead em : Trigger.new) {
    if(em.Email != null){

List<Contact> cem = [SELECT Id, Email FROM Contact WHERE Email = :listEmail];
String cemail;
for(Contact ce : cem){
    cemail = ce.Email;

for(Lead ld : Trigger.new) {
    if (ld.Email != cemail && ld.Parent_or_guardian__c == false) {

        Contact cnt = new Contact();

        cnt.FirstName = ld.FirstName;
        cnt.LastName = ld.LastName;
        cnt.Email = ld.Email;
        cnt.RecordTypeId = '012P00000005ZDpIAM';
        if(ld.Email != cemail && ld.Parent_or_guardian__c == true ){
            Contact cnt1 = new Contact();
            cnt1.RecordTypeId = '012P00000005ZDuIAM';
            cnt1.FirstName = ld.Parent_First_Name__c;
            cnt1.LastName = ld.Parent_Last_Name__c;
            cnt1.Email = ld.Parent_Email__c;
            Contact cnt2 = new Contact();
            cnt2.RecordTypeId = '012P00000005ZDpIAM';
            cnt2.FirstName = ld.FirstName;
            cnt2.LastName = ld.LastName;
            cnt2.Email = ld.Email;

    INSERT conInsertList;
    List<Id> conInsert = new List<Id>();
    for(Contact con : conInsertList){
        hed__Relationship__c hedrel = new hed__Relationship__c();
        hedrel.hed__Contact__c = conInsert[0];
        hedrel.hed__RelatedContact__c = conInsert[1];
        hedrel.hed__Type__c = 'Parent';
    insert relation;

1 Answer 1


conInsert only has one value when you get to the list access, so [1] is not yet a valid index. You'll want to access the original contact list in pairs of two to get at the data.

for(Integer i = 0, s = conInsertList.size(); i < s; i += 2) {
    hed__Relationship__c hedrel = new hed__Relationship__c();
    hedrel.hed__Contact__c = conInsertList[i].Id;
    hedrel.hed__RelatedContact__c = conInsertList[i + 1].Id;
    hedrel.hed__Type__c = 'Parent';
insert relation;

Regardless, your code has some other problems, too. You may want to keep the contacts in two separate lists so as not to get the relationships confused.

  • Hello sfdcfox, thanks a lot for the reply, after changing code, I am getting the following error "Illegal assignment from Contact to Id",can you please help me out in this issue
    – user70770
    Jul 15, 2019 at 8:12
  • @user70770 Yes, sorry, it should have been .Id in the code. Still, your trigger has more work to do. You might need to do more research on your design.
    – sfdcfox
    Jul 15, 2019 at 8:15
  • yeah I changed it and it worked, thanks a lot, can you suggest me where I can improve it further if possible please.
    – user70770
    Jul 15, 2019 at 8:16
  • hello @sfdcfox, your cod eworks for me if I insert one lead record at a time but for bulk upload its failing for bulk upload as I am getting the following error ""ContactCreationFromLead: execution of BeforeUpdate caused by: System.ListException: List index out of bounds: 3"", I believe this is due to if loop only, can you please help me out in this issue if possible.
    – user70770
    Jul 26, 2019 at 6:35

You must log in to answer this question.