How to clone records based on custom object records ?





How to clone records based on custom object records ?

A custom object (DataLoadTest__c) is created with fields as name,city,country and phone.

A controller fetches the vf pages selected record.


Apex Class


public with sharing class CloneTestingRecordClass {

    public PageReference clonedRec()
{
        DataLoadTest__c selectedRecord = [select Id,name,city__c,country__c,phone__c from DataLoadTest__c where id=:rId];
        dataLoadtest__c lstnew = selectedRecord.clone(false);
        insert lstnew;
       
        pagereference ref = new pagereference('/apex/cloneRecordTesting');
        ref.setredirect(true);
        return ref;
    }

    public String rId{get;set;}
   
    List<DataLoadTest__c> lstdlt = new List<DataLoadTest__c>();
    public List<DataLoadTest__c> getRecords() {
        lstdlt = [select Id,name,city__c,country__c,phone__c from DataloadTest__c];
        return lstdlt;
    }

}


A Visualforce page displaying all the objects records and on selection of the command link, we pass the records id through apex:param.

Visualforce Page



<apex:page sidebar="false" controller="CloneTestingRecordClass">

 <apex:form >

     <apex:pageBlock >
         <apex:pageBlockSection >
             <apex:pageBlockTable value="{!records}" var="r">
                 <apex:column headerValue="Name">
                     {!r.Name}
                 </apex:column>
                 <apex:column headerValue="City">
                     {!r.City__c}
                 </apex:column>
                 <apex:column headerValue="Country">
                     {!r.Country__c}
                 </apex:column>
                 <apex:column headerValue="Phone">
                     {!r.phone__c}
                 </apex:column>
                 <apex:column headerValue="Action">
                     <apex:commandLink value="Clone" action="{!clonedRec}">
                         <apex:param name="rId" value="{!r.Id}" assignTo="{!rId}"/>
                     </apex:commandLink>
                 </apex:column>
             </apex:pageBlockTable>
         </apex:pageBlockSection>
     </apex:pageBlock>
 </apex:form>
</apex:page>
How to clone records based on custom object records ? How to clone records based on custom object records ? Reviewed by dasfrogpractice on 22:26 Rating: 5

1 comment:

  1. Want to bulk clone multiple custom fields and objects in Salesforce in few clicks, Appexchange app named as Bulk Object Field Creator Most recommended and appreciable app by Salesforce MVP

    ReplyDelete

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe