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
Rahul Singh 1384Rahul Singh 1384 

With Apex: Copy Records From One Object to Another Object

Hi friends,

I have a custom object 'MyFixOrder' which has 2 Record Types 'AccountMyOrder' and 'ContactMyOrder'.
I have another custom object 'MyPartOrder' which also has 2 Record Types 'AccountMyOrder' and 'ContactMyOrder'.

Fields for both Objects are Same.
- Name(Text)
- Date of Start(Date)
- Date of End(Date)
- Active(Checkbox)
- OrderType(Text)
Suppose I have two 2 records in 'MyFixOrder' object of 'AccountMyOrder' and 'ContactMyOrder' RecordType
then i have to copy those records in 'MyPartOrder' object with their record types.

Any help would be greatly appreciated.
Best Answer chosen by Rahul Singh 1384
RKSalesforceRKSalesforce
Please use below code:
List<MyFixOrder__c> fixOrderList = [Select Id, Name, Date_of_Start__c, Date_of_End__c, Active__c, OrderType__c, Recordtype.Name from MyFixOrder__c];
List<MyPartOrder__c> partOrderList = New List<MyPartOrder__c>();
for(MyFixOrder__c fixOrder : fixOrderList){
	String fixOrderRecordType = fixOrder.Recordtype.Name;
	MyPartOrder__c partOrder = New MyPartOrder__c();
	partOrder.Name = fixOrder.Name
	partOrder.Date_of_Start__c =  fixOrder.Date_of_Start__c;
	partOrder.Date_of_End__c = fixOrder.Date_of_End__c;
	partOrder.Active__c = fixOrder.Active__c;
	partOrder.OrderType__c =  fixOrder.OrderType__c;
	partOrder.RecordTypeId  = Schema.SObjectType.MyPartOrder__c.getRecordTypeInfosByName().get(fixOrderRecordType).getRecordTypeId();
	partOrderList.add(partOrder);
} 
Insert partOrderList;
Hope this will work.

Regards,
Ramakant

All Answers

RKSalesforceRKSalesforce
Hello Rahul,

Please execute Below code in Anonymous window:
List<MyFixOrder__c> fixOrderList = [Select Id, Name, Date_of_Start__c, Date_of_End__c, Active__c, OrderType__c, Recordtype.Name from MyFixOrder__c];
List<MyPartOrder__c> partOrderList = New List<MyPartOrder__c>();
for(MyFixOrder__c fixOrder : fixOrderList){
	MyPartOrder__c partOrder = New MyPartOrder__c();
	partOrder.Name = fixOrder.Name
	partOrder.Date_of_Start__c =  fixOrder.Date_of_Start__c;
	partOrder.Date_of_End__c = fixOrder.Date_of_End__c;
	partOrder.Active__c = fixOrder.Active__c;
	partOrder.OrderType__c =  fixOrder.OrderType__c;
	partOrder.RecordType.Name = fixOrder.RecordType.Name;
	partOrderList.add(partOrder);
} 
Insert partOrderList;
Steps To use Anonymous window : Open Developer Console --> Debug --> Open Execute Anonymous window --> Paste your code --> Execute.

Please let me know for any issue and mark as best answer if helped.

Regards,
Ramakant
Sanjay Bhati 95Sanjay Bhati 95
Hy Ramakant ,
 you are right.
Rahul Singh 1384Rahul Singh 1384
Hi Ramakant,

Thank You so much for the response.
but it is giving me error(System.NullPointerException: Attempt to de-reference a null object) at line number 10.
 
partOrder.RecordType.Name = fixOrder.RecordType.Name;

I think we are not getting the RecordType Name directly in a list 'fixOrderList'.

any other way or Anything else we can do to overcome this problem? that would be helpful for me.
RKSalesforceRKSalesforce
Please use below code:
List<MyFixOrder__c> fixOrderList = [Select Id, Name, Date_of_Start__c, Date_of_End__c, Active__c, OrderType__c, Recordtype.Name from MyFixOrder__c];
List<MyPartOrder__c> partOrderList = New List<MyPartOrder__c>();
for(MyFixOrder__c fixOrder : fixOrderList){
	String fixOrderRecordType = fixOrder.Recordtype.Name;
	MyPartOrder__c partOrder = New MyPartOrder__c();
	partOrder.Name = fixOrder.Name
	partOrder.Date_of_Start__c =  fixOrder.Date_of_Start__c;
	partOrder.Date_of_End__c = fixOrder.Date_of_End__c;
	partOrder.Active__c = fixOrder.Active__c;
	partOrder.OrderType__c =  fixOrder.OrderType__c;
	partOrder.RecordTypeId  = Schema.SObjectType.MyPartOrder__c.getRecordTypeInfosByName().get(fixOrderRecordType).getRecordTypeId();
	partOrderList.add(partOrder);
} 
Insert partOrderList;
Hope this will work.

Regards,
Ramakant
This was selected as the best answer
Rahul Singh 1384Rahul Singh 1384
Hey, this worked for me, this was really helpful.
Thank You So Much 
Sai Manideep Yadav GaddaguntaSai Manideep Yadav Gaddagunta
Hi Ramakanth, can u send me how to create an add button?
if I click the add button, it must go to the second custom object.
How can I do?
Please...User-added image
arti rathodarti rathod
Rahul Singh 1384
Hi rahul, can i get whole code for it i have get same requirement.