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
ani123ani123 

need help in deeep clone to be automatic

hi everyone,

 

I have an visual force page which has both patrent and child values on click of Clone button need tolone values.

But this should perform automatic even if we add fields to the custom object ,without including fields in query in each time.

Can anyone help me with the sample code.

 

Thanks in advance,

Anitha

SFDC_LearnerSFDC_Learner

To clone the record with in VF page Table,

<apex:page controller="clonerecords">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!records}" var="x">
                    <apex:column headerValue="Clone">
                        <apex:commandLink Value="Clone" action="{!clonerec}">
                            <apex:param name="xId" value="{!x.Id}" assignTo="{!xId}"/>
                        </apex:commandLink>
                    </apex:column>
                    <apex:column headerValue="EmpName">
                        {!x.name}
                    </apex:column>
                    <apex:column headerValue="DeptName">
                        {!x.Departmentt__r.name}
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
public with sharing class clonerecords {
    public Id xId{get;set;}
    Employeee__c lstonerec=new Employeee__c();
    public PageReference clonerec() 
    {
        lstonerec=[select Id,name,Departmentt__r.name from Employeee__c where Id=:xId];
        Employeee__c empobj=lstonerec.clone(false);
        insert empobj;
        pagereference ref=new pagereference('/apex/insertingrecordvfpage');
        ref.setredirect(true);
        return ref;
        
    }
    List<Employeee__c> lstemp=new List<Employeee__c>();
    public List<Employeee__c> getRecords() {
        lstemp=[select id,name,Departmentt__r.name from Employeee__c];
        return lstemp;
    }
}

 

ani123ani123

i want to perform cloning with map.

Map<String, Schema.SObjectField> M =Schema.SObjectType.Billing__c.fields.getMap();

SFDC_LearnerSFDC_Learner

Could you please share your sample code (What you have implemented till)?

ani123ani123
public class PurchaseOrderCloneWithItemsController {
 
    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    private Purchase_Order__c po {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}
 
    // initialize the controller
    public PurchaseOrderCloneWithItemsController(ApexPages.StandardController controller) {
 
        //initialize the stanrdard controller
        this.controller = controller;
        // load the current record
        po = (Purchase_Order__c)controller.getRecord();

    }

    // method called from the VF's action attribute to clone the po
    public PageReference cloneWithItems() {
    List<String> listaCampos = new List<String>(); 
    Map<String, Schema.SObjectField> Mappurchase =Schema.SObjectType.Purchased_Item__c.fields.getMap();
    system.debug('@@@@@@@@@@'+Mappurchase);
    for(String campo : Mappurchase.keyset()){
     listaCampos.add(Mappurchase.get(campo).getDescribe().getName());
                   System.debug('@@@@@@@@@'+listaCampos);
    }
    
    // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Purchase_Order__c newPO;
 
         try {
 
              //copy the purchase order - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             po = [select Id, Name, Ship_To__c, PO_Number__c, Supplier__c, Supplier_Contact__c, Date_Needed__c, Status__c, Type_of_Purchase__c, Terms__c, Shipping__c, Discount__c from Purchase_Order__c where id = :po.id];
             newPO = po.clone(false);
             //instead of this query how to get dynamic values from Map
             insert newPO;
             
              System.debug('@@@@@@@@@'+newPO);
             // set the id of the new po created for testing
               newRecordId = newPO.id;
 
             // copy over the line items - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<Purchased_Item__c> items = new List<Purchased_Item__c>();
             for (Purchased_Item__c pi : [Select p.Id, p.Unit_Price__c, p.Quantity__c, p.Memo__c, p.Description__c From Purchased_Item__c p where Purchase_Order__c = :po.id]) {
                  Purchased_Item__c newPI = pi.clone(false);
                  newPI.Purchase_Order__c = newPO.id;
                  items.add(newPI);
             }
             insert items;
             System.debug('@@@@@@@@@'+items);
 
         } catch (Exception e){
             // roll everything back in case of error
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
         }
 
        return new PageReference('/'+newPO.id+'/e?retURL=%2F'+newPO.id);
    }
 
}