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
SFDC@ErrorSFDC@Error 

Deep Clone with parent,child and grand child

Hi All

I have written a class for cloning parent,child and grand child ..(Quote--QuoteLine Item  and QuoteLineItem--ProductLineitem).Its working in only on parent and child,not on grand child..
 
public class CloneQuoteLineItems_AC {
    @AuraEnabled
    public static String getCloneQuote(String oldId) {
        system.debug('recordId: ' + oldId);
        // Initialize setup variables
        String objectName = 'Quote__c';
        String query = 'SELECT';
        Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap();
        
        // Grab the fields from the describe method and append them to the queryString one by one.
        for(String s : objectFields.keySet()) {
            query += ' ' + s + ',';
        }
        
        if(query.endsWith(','))
            query = query.removeEnd(',');    
        
        // Add FROM statement
        query += ' FROM ' + objectName;
        
        // Add on a WHERE/ORDER/LIMIT statement as needed
        query += ' WHERE Id =  \''+ oldId +'\'  LIMIT 1';
        system.debug('Final Query: ' + query);    
        
        Quote__c oldQuote = database.query(query);
        system.debug(oldQuote);
        Quote__c newQuote = oldQuote.clone(false, false, false, false);
        insert newQuote;   
        System.debug('new quote: '+newQuote);
        List<Quotation_Line_Item__c> qliList = new List<Quotation_Line_Item__c>();
        for(Quotation_Line_Item__c qli : [SELECT Id, 
                                          Product__c, 
                                          Quote_Name__c, 
                                          Qutantity__c, 
                                          Select_Type__c, 
                                          Size__c, 
                                          Basic_Unit_Price__c FROM Quotation_Line_Item__c WHERE Quote_Name__c = :oldQuote.Id]) {
                                              
                                              system.debug('product List = ' + qli);
                                              
                                              system.debug('new Quote Id = ' + newQuote.Id);
                                              Quotation_Line_Item__c newQLI = qli.clone(false, false, false, false);
                                              newQLI.Quote_Name__c = newQuote.id;
                                              qliList.add(newQLI);
        
        }
        insert(qliList);
        
        system.debug('newProdList ' + qliList);
        
        List<Quote_Product_Line_Item__c> qpliList = new List<Quote_Product_Line_Item__c>();
        for(Quote_Product_Line_Item__c qpli : [SELECT Id, Name, Size__c, Basic_Unit_price__c
                                                                         ,Product__r.name,Quantity__c,Quotation_Line_Item__c,
                                                                    Total_Price__c,Type__c, Quote__c
                                                                    FROM Quote_Product_Line_Item__c WHERE Quote__c= :oldQuote.Id]) {
                                              
                                              system.debug('product List = ' + qpli);
                                              
                                              system.debug('new Quote Id = ' + newQuote.Id);
                                              Quote_Product_Line_Item__c  newQPLI = qpli.clone(false, false, false, false);
                                              newQPLI.Quote__c = newQuote.id;
                                              qpliList.add(newQPLI);
        
        }
        insert(qpliList);
        system.debug('newProdList ' + qpliList);
        
        return newQuote.id;
    }    
}

 
SKolakanSKolakan
Please see this post: http://nwilliamsscu.blogspot.com/2013/09/deeper-clone-clone-salesforce-objects.html
 
SFDC@ErrorSFDC@Error
I have tried that is not working