+ Start a Discussion
AnidevAnidev 

Auto - create records based on a list

Hi All,

 

Here is my scenario,

 

I have a list of records in a particular object "A".

I have to query these records and put them in another list(on a different object "B") wherefrom the same number of records will be auto generated.

e.g.

 

Object - A

records: A1,A2,A3. (these will be queried in a list)

 

Object - B

('put' list)records created: B1(=A1),B2(=A2),B3(=A3)....

 

Kindly advice.

 

Many thanks in advance

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
KapilCKapilC

Hi AniDev,

 

You can try this code, Hope this will work for you.

 

list<objectA__c> aObjList = new list<objectA__c>();
list<objectB__c> bObjList = new list<objectB__c>();
for (objectA__c aObj : [SELECT id, field1__c,field2__c FROM objectA__c]) {
       objectB__c  bObj = new objectB__c();
	   bObj.field1__c = aObj.field1__c;
	   bObj.field2__c = aObj.field2__c;
	   
	   bObjList.add(bObj);
}

	insert bObjList;

 

[If you got answer from my post please mark it as solution.]

 

Thanks,

Kapil