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
SFDC ROCKSFDC ROCK 

Copy from one object to another object

Problem statement:

from Items object (related object on oppty) copy type__c values and Amount__c values and create (only insert) records on object item_product__c (related object on oppty)after calulating amount__c and type__c.

type__c  value from Items object
A
B
C
D

if records is getting created on items object 

type__c=A   Amount__c=5
type__c=A   Amount__c=6
type__c=C   Amount__c=10
type__c=c   Amount__c=6
type__c=B   Amount__c=8
.....

records should be created on item_product__c like below

type__c=A   Amount__c=11   //(5+6)
type__c=B   Amount__c=8
type__c=C   Amount__c=16

total 3 records should get created.

Note : amount is formula field on item but same amount is currecy field on item_product__c


Createrecord(itemIdFrompreviousmethodIds); // taking id from previous method
}
 public static void Createrecord(Set<Id> itemIdFrompreviousmethodIds){
        Set<String> Types = new Set<String>();
        for(Type__mdt TypeCM : [Select type__c from Type__mdt]){ //>>>>>>>>>>> metadata where store all type__c values.
            Types.add(TypeCM.type__c);                // adding all type__c values and keeping it into set
        }
        
        
 Map<Id,List<items__c>> opptyitemList = new Map<Id,List<items__c>>();
        
         for(items__c item : [select Id,type__c,Amount__c,Opportunity__c from items__c where Id IN : itemIdFrompreviousmethodIds and product_type__c IN :Types]){    
                                  // getting all records from item which is related object of oppty 
            
            
            //mapping for item with opptyId.
             if(opptyitemList.containsKey(item.id)){
             
                  list<items__c> LineItemLst=opptyitemList.get(item.id);
                   LineItemLst.add(item);
                    opptyitemList.put(item.Opportunity__c,LineItemLst); // not getting any values.
                     
             } else { 
                 opptyitemList.put(item.Opportunity__c, new List<items__c>());  
                  opptyitemList.get(item.Opportunity__c).add(item);   // creating records and getting mapped value.
                  
                  //how should I write code so that I can create records on item_product__c 
                
                         
                     }
                     
                 }
          
             } 
             
   Any modification or any other suggestions will be appreciated.
ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

You can create the flow to copy the data from the parent and create the record on Child easily.

Please use this blog (https://www.simplysfdc.com/2018/03/salesforce-quick-create-record-with-flow.html) to start the flow.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri