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
AshDonAshDon 

Records from Custom object to Standard object

Hi,
I have an apex class in there I need to transfer a record from custom object to standard object if the orderID (field in custom object) is matching with the orderID i have in the apex class.
Can someone help.

Thanks.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi AshDon,

We cannot change the record from one object to another object. Instead of that we can delete the old record and create a new record for the required object.

If this solution helps, Please mark it as best answer.

Thanks,
 
AshDonAshDon
Hi Sai, 
Thank you fo your reply.

Is there any possible way we can store the records that are in custom object to standard object ? I dont want to delete the record in custom object I just want the same record in standard object.
 
sunil yogisunil yogi

Hi AshDon,

Please following the steps to move records from custom object to standard object

Please Mark if you helps :)

(1) Export all records from your custom object
(2) Import file by the dataloader.io 
(3) please do mapping the fields to store record

Thanks

AnkaiahAnkaiah (Salesforce Developers) 
Hi AshDon,

1. Is there any relationship between custom and standard object?

2.Standard object have same fields which are available in custom object?
AshDonAshDon
@sunil yogi
Hi,
I want to do it inside an apex class.

@Ankaiah
Hi,
1. No relation between the objects.
2.Yes both objects have the same fields.
AnkaiahAnkaiah (Salesforce Developers) 
Hi AshDon,

Standard object will have predefiend records with orderIds?. if yes then we can able to transfer the data from custom object to standard object.
try with below code.
trigger Totalagreement on CustomObject__c (after insert, after update) 
{

set<string> orderids=new set<string>();
     for(CustomObject__c b:trigger.new)
    {
	if(b.orderId__c!=null){
    orderids.add(b.orderId__c);
	}
}
    }
List<StandardObject> standardobjectupdate = new List<StandardObject>();

//query list of fields from standard object..    
   list<StandardObject> standardobj =[select id, Name,orderId__c,field1__c,field2__c from Unit__c where orderId__c =:orderids];

    for(StandardObject sb:standardobj)
    {
       for (CustomObject__c  cb : trigger.new){
            
                sb.Name = cb.Name;
                sb.field1__c = cb.field1__c;
                sb.field2__c = cb.field2__c;
                sb.field3__c = cb.field3__c;				
                standardobjectupdate.add(sb); 

				
				}
        				
          
    }
 update standardobjectupdate;
}

If this helps, please mark it as best answer.

Thanks,
Ankaiah
sunil yogisunil yogi
Hello AshDon,

Please Try Below Code and Select Best If it helps.

Map<String,Account> MapAcc = New Map<String,Account>();
        
        for(Account standardobj : [select id,Name,orderID FROM Account]){
            if(!MapAcc.containsKey(standardobj.orderID)){
                MapAcc.put(standardobj.orderID,standardobj);
            }
        }
        
        List<Account> AddAccList = new List<Account>();
        for(CustomObject__c customobj : [select id,Name,orderID FROM CustomObject__c]){
            if(MapAcc.containsKey(customobj.orderID)){                
                MapAcc.get(customobj.orderID).Name = customobj.Name;
                AddAccList.add(MapAcc.get(customobj.orderID));
            }
        }
        
        if(AddAccList.size() > 0){
            update AddAccList; 
        }


Thanks