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
AlecSAlecS 

Create multiple custom object using trigger

Here's my scenario.

 

I have a custome object called Invoice__c.

 

When my Contract is activated, I want to create as many Invoice__c objects as there are years in the contract. That is, if I have a 3 year contract, then I should create 3 Invoice__c objects after Contract is activated.

 

How can I do that? 

 

I am able to create one intance of the Invoice__c object. I am having issues creating multiple objects. 

 

Thanks a lot!

Best Answer chosen by Admin (Salesforce Developers) 
Jerun JoseJerun Jose

You can use the following code to start off.

 

I have written a crude version for inserts. You will probably need to add logic for updates as well.

 

trigger trg1 on Contract(after insert){
	list<Invoice__c> invList = new list<Invoice__c>();
    for(Contract con : trigger.new){
		if(con.years__c != null)
			for(Integer i=0; i < con.years__c; i++){
				Invoice__c inv = new Invoice__c();
				// do some field mapping here.
				// eg. inv.Contract__c = con.ID;
				invList.add(inv);
			}
	}
	if(!invList.isEmpty())
		insert invList;
}

 @Anwar - I dont mean to discourage you, but you should improve your code quality. I can see that you have nested loops, SOQL inside loops and DML inside loops. These are just invitations to hit the governor limits.

All Answers

SimonJaiSimonJai

Have you tried passing a list of Invoice__c?

 

List<Invoice__c> listOfInvoices;

listOfInvoices.add(new Invoice__c());
listOfInvoices.add(new Invoice__c());
listOfInvoices.add(new Invoice__c());

insert(listOfInvoices);

 

Not sure if that works, but just a suggestion.

 

AnwarAnwar

Hi AlecS,

 

I think this will help to you.

Here When i create enquiry ,that time automatically create contact.

use this code and modify enquiry with contact and contact with invoice object

 

Trigger Autocreateenquirycontacts1 on Enquiry__c (After update) 
{

    Set<string> st = new set<string>();
   
    List<Contact> dlist1 = new List<Contact>();
  
    List<Enquiry__c> pid = Trigger.new;
 
  for (Enquiry__c i:pid)
    st.add(i.id);
    for (string s:st)
    for (Enquiry__c apt:Trigger.New )
        IF(apt.Rcd_Type_Name__c=='Qualified Enquiry')        
        {
          for (Enquiry__c pmst : [SELECT id,Enquiry_Name__c, First_Name__c,Last_Name__c, FROM Enquiry__c Where id=:s ])
           {  dlist1.add(new Contact(

           Enquiry_Name__c =pmst.Enquiry_Name__c,
           FirstName= pmst.First_Name__c,
           LastName = pmst.Last_Name__c,));                 
           }        insert dlist1;
    
    }
    }

Thanks

ANWAR

 

Jerun JoseJerun Jose

You can use the following code to start off.

 

I have written a crude version for inserts. You will probably need to add logic for updates as well.

 

trigger trg1 on Contract(after insert){
	list<Invoice__c> invList = new list<Invoice__c>();
    for(Contract con : trigger.new){
		if(con.years__c != null)
			for(Integer i=0; i < con.years__c; i++){
				Invoice__c inv = new Invoice__c();
				// do some field mapping here.
				// eg. inv.Contract__c = con.ID;
				invList.add(inv);
			}
	}
	if(!invList.isEmpty())
		insert invList;
}

 @Anwar - I dont mean to discourage you, but you should improve your code quality. I can see that you have nested loops, SOQL inside loops and DML inside loops. These are just invitations to hit the governor limits.

This was selected as the best answer
AlecSAlecS

Jerun,

 

You are a class apart!!!

 

Your code worked like a charm!

 

Thanks much.

 

-Alec

MohaMoha
Hello, well i'm kind of new with salesforce and i need a little help, well my request is that i need to select many Contacts and when i click on a custom button from list it create records for every Contact
to make my self clear, i ave a junction object betwen Contact and Event__c, and i need to invite many contacts to an Event using the JunctionObject of course,
any help would be great, Thankx :)