Cloning the records using clone method

Apex Class


How to clone records based on related list of 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.


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>
We need your support,Please share/Like us on Facebook ⛅
Cloning the records using clone method Cloning the records using clone method Reviewed by dasfrogpractice on 04:54 Rating: 5

No comments:

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