+ Start a Discussion
jpbenjpben 

Populate Lookup Field

Hi All, 

Need assistance on my trigger.  I have this trigger that creates 2 Invoice Schedule records (custom object) when a new contract is created.  The part that I need help on is linking the 2 Invoice Schedule records into the Contract.  I need to populate the Contract lookup field (Contract__c) on the Invoice Schedule record.  

Thanks in advance.  

trigger Technology_AddInvoiceSchedule on Contract (before insert) {
//When a Lynx or iKM contract are created, insert 2 Invoice Schedule records

List<Invoice_Schedule__c> invschedlist = new List<Invoice_Schedule__c>();
    for (Contract cont: Trigger.new){
        if(cont.RecordTypeId=='012J00000004xUI'||cont.RecordTypeId=='012J00000004xUJ'){
//Record Types are Technology: Lynx and Technology: iKnowMed.

Invoice_Schedule__c  invsched1 = new Invoice_Schedule__c();
            invsched1.Name='First Invoice';
            invsched1.Invoice_Number__c='TBD';
            invsched1.Invoice_Amount__c=0;
            invsched1.Opportunity__c=cont.Opportunity__c;
            invsched1.RecordTypeId='012J00000004zD9';
            
            
 Invoice_Schedule__c  invsched2 = new Invoice_Schedule__c();
            invsched2.Name='Recurring Invoice';
            invsched2.Invoice_Number__c='TBD';
            invsched2.Invoice_Amount__c=0;
            invsched2.Opportunity__c=cont.Opportunity__c;
            invsched2.RecordTypeId='012J00000004zD9';    
            
            invschedlist.add(invsched1); 
            invschedlist.add(invsched2);
        }
        if(invschedlist.size()>0)       
        insert invschedlist;
    }
    
}
Best Answer chosen by jpben
pconpcon
That is easy, all you need to do is say Contact__c = cont.Id

I have taken the liberty of updating and cleaning up your code with this update
 
Trigger Technology_AddInvoiceSchedule on Contract (before insert) {
    // This is bad.  You should look this up instead
    Id recordTypeId = '012J00000004zD9';
    Set<Id> validRecordTypeIds = new Set<Id> {
        '012J00000004xUI',
        '012J00000004xUJ'
    };

    List<Invoice_Schedule__c> invschedlist = new List<Invoice_Schedule__c>();

    for (Contract cont: Trigger.new) {
        if (validRecordTypeIds.contains(cont.RecordTypeId)) {
            invschedlist.add(new Invoice_Schedule__c (
                Name='First Invoice',
                Invoice_Number__c = 'TBD',
                Invoice_Amount__c = 0,
                Opportunity__c = cont.Opportunity__c,
                Contract__c = cont.Id,
                RecordTypeId = recordTypeId
            ));

            invschedlist.add(new Invoice_Schedule__c (
                Name = 'Recurring Invoice',
                Invoice_Number__c = 'TBD',
                Invoice_Amount__c = 0,
                Opportunity__c = cont.Opportunity__c,
                Contract__c = cont.Id,
                RecordTypeId = recordTypeId
            ));
        }
    }

    if (!invschedlist.isEmpty()) {
        insert invschedlist;
    }   
}

NOTE: This code has not been tested and my contain typographical or logical errors

All Answers

pconpcon
That is easy, all you need to do is say Contact__c = cont.Id

I have taken the liberty of updating and cleaning up your code with this update
 
Trigger Technology_AddInvoiceSchedule on Contract (before insert) {
    // This is bad.  You should look this up instead
    Id recordTypeId = '012J00000004zD9';
    Set<Id> validRecordTypeIds = new Set<Id> {
        '012J00000004xUI',
        '012J00000004xUJ'
    };

    List<Invoice_Schedule__c> invschedlist = new List<Invoice_Schedule__c>();

    for (Contract cont: Trigger.new) {
        if (validRecordTypeIds.contains(cont.RecordTypeId)) {
            invschedlist.add(new Invoice_Schedule__c (
                Name='First Invoice',
                Invoice_Number__c = 'TBD',
                Invoice_Amount__c = 0,
                Opportunity__c = cont.Opportunity__c,
                Contract__c = cont.Id,
                RecordTypeId = recordTypeId
            ));

            invschedlist.add(new Invoice_Schedule__c (
                Name = 'Recurring Invoice',
                Invoice_Number__c = 'TBD',
                Invoice_Amount__c = 0,
                Opportunity__c = cont.Opportunity__c,
                Contract__c = cont.Id,
                RecordTypeId = recordTypeId
            ));
        }
    }

    if (!invschedlist.isEmpty()) {
        insert invschedlist;
    }   
}

NOTE: This code has not been tested and my contain typographical or logical errors
This was selected as the best answer
jpbenjpben
Thanks pcon.  That was easy.  I actually changed the trigger to "After Insert" and it worked.