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
ckellieckellie 

Creating Multiple Records from one Record more Efficiently

The below code is evaluating multiple fields on the same record and then creating the multiple records

For example:
The fields, Plain_Text_Desc_Line_01__c and HTML_Description_Line_01__c, will create the first product description record.
The fields, Plain_Text_Desc_Line_02__c and HTML_Description_Line_02__c, will create the second product description record
The fields, Plain_Text_Desc_Line_03__c and HTML_Description_Line_03__c, will create the third product description record
The fields, Plain_Text_Desc_Line_04__c and HTML_Description_Line_04__c, will create the fourth product description record
The fields, Plain_Text_Desc_Line_05__c and HTML_Description_Line_05__c, will create the fifth product description record
 
/*This for loop will create multiple Product Description records from one App Matrix record. Each Product Description Record comprises of a Plain_Text_Desc field and a HTML_Description field.
      The new record is attached to the appropriate template record and is given a sequence number.
    */
    for(App_Matrix__c apm : alist){
         String extid = oid+'-'+apm.name;
         linesort = 1;
      If(apm.Plain_Text_Desc_Line_01__c<>null){ //If Plain_Text_Desc_Line_01 is not null a new product description record is created and attached to the parent template record.
     
         Product_Description__c pd = new Product_Description__c();
             pd.Template_ID__c = templates.get(extid).id;
             pd.sequence__c = linesort;
             pd.Product_Description_English__c = apm.Plain_Text_Desc_Line_01__c;
             pd.Product_Description_HTML__c = apm.HTML_Description_Line_01__c;
             
             pdlist.add(pd);
             linesort=linesort+1;
             
        }
        
      If(apm.Plain_Text_Desc_Line_02__c<>null){ //If Plain_Text_Desc_Line_02 is not null a new product description record is created and attached to the parent template record.
     
         Product_Description__c pd = new Product_Description__c();
             pd.Template_ID__c = templates.get(extid).id;
             pd.sequence__c = linesort;
             pd.Product_Description_English__c = apm.Plain_Text_Desc_Line_02__c;
             pd.Product_Description_HTML__c = apm.HTML_Description_Line_02__c;
             
             pdlist.add(pd);
             linesort=linesort+1;
        }

      If(apm.Plain_Text_Desc_Line_03__c<>null){ //If Plain_Text_Desc_Line_03 is not null a new product description record is created and attached to the parent template record.
     
         Product_Description__c pd = new Product_Description__c();
             pd.Template_ID__c = templates.get(extid).id;
             pd.sequence__c = linesort;
             pd.Product_Description_English__c = apm.Plain_Text_Desc_Line_03__c;
             pd.Product_Description_HTML__c = apm.HTML_Description_Line_03__c;
             
             pdlist.add(pd);
             linesort=linesort+1;
        }
        
      If(apm.Plain_Text_Desc_Line_04__c<>null){ //If Plain_Text_Desc_Line_04 is not null a new product description record is created and attached to the parent template record.
     
         Product_Description__c pd = new Product_Description__c();
             pd.Template_ID__c = templates.get(extid).id;
             pd.sequence__c = linesort;
             pd.Product_Description_English__c = apm.Plain_Text_Desc_Line_04__c;
             pd.Product_Description_HTML__c = apm.HTML_Description_Line_04__c;
             
             pdlist.add(pd);
             linesort=linesort+1;
        }
                
      If(apm.Plain_Text_Desc_Line_05__c<>null){ //If Plain_Text_Desc_Line_05 is not null a new product description record is created and attached to the parent template record.
     
         Product_Description__c pd = new Product_Description__c();
             pd.Template_ID__c = templates.get(extid).id;
             pd.sequence__c = linesort;
             pd.Product_Description_English__c = apm.Plain_Text_Desc_Line_05__c;
             pd.Product_Description_HTML__c = apm.HTML_Description_Line_05__c;
             
             pdlist.add(pd);
             linesort=linesort+1;
        }
     }  

        Insert pdlist;

How do I make this code more efficient?