+ Start a Discussion
dwwrightdwwright 

All fields won't clone on Child Objects

I'm creating an application which involves creating a Master Object (Quality Document) and than creating many child objects (SDQA) which are used as forms. I have over 100 custom 'generic' fields for the child object which I adapt to whichever type of form I need the child object to be. I need to be able to clone the master object along with all of its child objects. I've created a method in a controller which does this, and it will clone the master object, and create records for the child objects--but the problem is that the child objects are not completely cloned--only a few of the fields actually get copied over. 

 

My Controller:

public class qualityDocumentExtension { 

private Quality_Document__c QD;

ApexPages.StandardController cont;

 

//Constructor, links QD to current VF pages Quality Document record

  public qualityDocumentExtension (ApexPages.StandardController qdController) {

this.QD = (Quality_Document__c)qdController.getRecord();

cont = qdController;

}//end constructor 

 

public PageReference cloneDoc() {

Quality_Document__c newQD = QD.clone(false,true);

newQD.Name += ' (CLONE - RENAME)';

insert newQD;

 

//Iterate through attached SDQAs and create clones

List<SDQA__c> sdqas = QD.SDQAs__r;

for(SDQA__c S : sdqas) {

SDQA__c newS = S.clone(false,true);

newS.Quality_Document__c = newQD.Id;

insert newS;

}

//Navigate to view the cloned Document

PageReference theRef = new PageReference('/apex/QualityDocumentOverride?id=' + newQD.Id); theRef.setRedirect(true);

return theRef;

}//end cloneDoc method

 

 I simply call the cloneDoc() method from a commandbutton component on the Quality Document.

 

Please offer any solutions you may have =) 

 

Message Edited by dwwright on 03-29-2010 11:04 AM
Best Answer chosen by Admin (Salesforce Developers) 
gtindugtindu

When in doubt - you could try to query matching subrecords - explicitly stating the fields that you want to copy.  We do something similar - only we are explicitly trying to copy data from specific subset of fields (not all fields).

 

  public PageReference cloneDoc() {

Quality_Document__c newQD = QD.clone(false,true);

newQD.Name += ' (CLONE - RENAME)';

insert newQD;

 

//Iterate through attached SDQAs and create clones

for(SDQA__c S : [SELECT fieldA__c, fieldB__c, etc__c FROM SDQA__c WHERE Quality_Document__c =: QD.id]) {

SDQA__c newS = S.clone(false,true);

newS.Quality_Document__c = newQD.Id;

insert newS;

}

//Navigate to view the cloned Document

PageReference theRef = new PageReference('/apex/QualityDocumentOverride?id=' + newQD.Id); theRef.setRedirect(true);

return theRef;

}//end cloneDoc method