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
affuaffu 

Auto Create Record

Hi all,

I have written a Trigger in Opportunity it fires when an opportunity is created automatically a record will be created in another object

Here i want multiple records to be created for one Opportunity

 

Objects :

Opportunity and

Record

 

Trigger:

trigger AutoCreateRecord on Opportunity (after insert){
    List<Record__c> Records = new List<Record__c>();
    for (Opportunity newOpportunity: Trigger.New){
        if(newOpportunity.CloseDate!=null && newOpportunity.StageName=='Closed Won'){
            Records.add(new Record__c(
                        Name ='1',
                        Opportunity__c = newOpportunity.Id,
                        Amount__c = 12));
        }
    }
    insert Records;
}

 

Best Answer chosen by Admin (Salesforce Developers) 
SFDC_EvolveSFDC_Evolve

Hi Affu....

 

I think Its possible to multiple records for a opportunity....but  this loop won't in that case...we need two for loop for this .  

 

trigger AutoCreateRecord on Opportunity (after insert){
    List<Record__c> Records = new List<Record__c>();
    for (Opportunity newOpportunity: Trigger.New){
        if(newOpportunity.CloseDate!=null && newOpportunity.StageName=='Closed Won'){
           for (Integer i = 0; i < newopportunity.no_of_records; i++ ){ 
Records.add(new Record__c( Name ='1', Opportunity__c = newOpportunity.Id, Amount__c = 12)); }
insert Records; } }

 

    Please let me know if there is concerns in this . .  Thanks :)

All Answers

kiranmutturukiranmutturu

multiple records for one opportunity means what is the base logic to create the number of records for one opportunity? 

affuaffu

HI Thanks for the reply,

 

There is no logic but i want to get multiple records with the same data as the first record has, when an Opportunity is created

lets say in Opportunity we have number field as No_Of_Records once u create an Opportunity enter No_Of_Records as 2 or 3 or4 so on those no of records should be created in that Object(Record) related to that Opportunity. Is it Possible? 

 

Thanku........

MandyKoolMandyKool

Hi,

 

You can create multiple records. You can access your "No_Of_Records" field on Opportunity and then you can use it in your trigger.

 

example code:

 

List<Records__c> lstRecords = new List<Records__c>();
Integer iNumber = 1;
for (Integer iCount = 0; iCount < Trigger.New.size(); iCount++)
{
   if(newOpportunity.CloseDate!=null && newOpportunity.StageName=='Closed Won')
   {
      Records__c objRec = new Records__c();
      objRec.Name = iNumber + ''; 
      objRec.Opportunity__c = newOpportunity.Id;
      objRec.Amount__c = 12;
      iNumber++;
      lstRecords.add(objRec);
   }
}

if(lstRecords.size() > 0)
  insert lstRecords;

 

Hope this solves your problem!!

SFDC_EvolveSFDC_Evolve

Hi Affu....

 

I think Its possible to multiple records for a opportunity....but  this loop won't in that case...we need two for loop for this .  

 

trigger AutoCreateRecord on Opportunity (after insert){
    List<Record__c> Records = new List<Record__c>();
    for (Opportunity newOpportunity: Trigger.New){
        if(newOpportunity.CloseDate!=null && newOpportunity.StageName=='Closed Won'){
           for (Integer i = 0; i < newopportunity.no_of_records; i++ ){ 
Records.add(new Record__c( Name ='1', Opportunity__c = newOpportunity.Id, Amount__c = 12)); }
insert Records; } }

 

    Please let me know if there is concerns in this . .  Thanks :)

This was selected as the best answer
MandyKoolMandyKool

Thanks SFDC_Evolve, for correcting me. Written the code on the fly ;).