I have read that as long as I query the fields while cloning it copy the values across. I want to be able to carry all fields across while cloning.

My goal is to clone an existing record which is stored in a field on the account and clone it with all field values or create a new Pre_Clarification__C if none exist with the addition of updating a lookup value with the newly inserted Review_Brief__c which is also the trigger point.

I am getting no errors and it correctly attaches to the new record but no values come across.

public class ReviewBrief_HDL {
private static Map<String,Schema.RecordTypeInfo> rtMapByName;
private static Schema.RecordTypeInfo rtCST ;

public static void CreatePreClara(Review_Brief__c[] rbriefs){

Map<Id,Pre_Clarification__c> preclaraMap = new Map<Id,Pre_Clarification__C>();
Pre_Clarification__c[] preclaraDMLList = New Pre_Clarification__C[]{};

//String soql = PreClarificationUtil.getCreatableFieldsSOQL('Pre_Clarification__c');
Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get('Pre_Clarification__c'.toLowerCase()).getDescribe().Fields.getMap();
List<String> fields = new List<String>();
if (fMap != null){
    for (Schema.SObjectField ft : fMap.values()){ // loop through all field tokens (ft)
        Schema.DescribeFieldResult fd = ft.getDescribe(); // describe each field (fd)
        if (fd.isCreateable()){ // field is creatable
String soql = 'select ' + 'Related_Opportunity__r.Account.FirstName, Related_Opportunity__r.Account.LastName, '
                + 'Related_Opportunity__r.AccountID, Related_Opportunity__c, Related_Opportunity__r.Account.Regarding_Pre_Clarification__c'
                + ', Related_Opportunity__r.Account.' +String.join(fields,', Related_Opportunity__r.Account.Regarding_Pre_Clarification__r.')
                + ' from Review_Brief__c where Id in : rbriefs';
    for(Review_Brief__c rbrief: Database.query(soql)){          
            new Pre_Clarification__C(
                Related_Review_Brief__c = rbrief.id,
                Related_Account__c = rbrief.Related_Opportunity__r.AccountId,
                Related_Opportunity__c = rbrief.Related_Opportunity__c,
                Id = rbrief.Related_Opportunity__r.Account.Regarding_Pre_Clarification__c, //if not null we know it is not the initial
                Name = 'Pre-Clarification - ' +  rbrief.Related_Opportunity__r.Account.FirstName + ' ' + 
                        rbrief.Related_Opportunity__r.Account.LastName + ' - ' + date.today(),
                Related_ASAP_FP__c = null,
                Related_ASAP_Super__c = null


//Cycle through the preclara records and for those that do not have an ID, create the clone and add it to the DML list
for(Pre_Clarification__c p : preclaraMap.values()){     
        Pre_Clarification__c tmp = p.id == null ? p : p.clone(false, true);


insert preclaraDMLList;

  • Have you debugged anything like the query string, the resultant records returned, etc. You may not even be entering the code where the query is done because I believe based on my answer it will throw an error when the dynamic query is ran. – Eric Nov 18 '16 at 2:49

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm Can you switch to the 4 parameter clone method? I'm not sure if the 2 parameter method is reliable any more.

Pre_Clarification__c tmp = p.id == null ? p : p.clone(false, true, false, false);

This line does not look correct:

+ ', Related_Opportunity__r.Account.' +String.join(fields,', Related_Opportunity__r.Account.Regarding_Pre_Clarification__r.')

You are joining by Related_Opportunity__r.Account.Regarding_Pre_Clarification__r. but the initial field is missing the Regarding_Pre_Clarification__r as it is started as Related_Opportunity__r.Account. only

  • Hey Eric, thank you for pointing that mistake out, however it still doesn't bring the field values from the old record. The only fields that are populated are the manual ones from 'new Pre_clarification__c(...)' I tried removing the dynamic field query and had a go with a one random field and that didn't copy over just blank. – Michael Nov 18 '16 at 3:41
  • @Michael I don't see a clone in your code so hard to say. Obviously that query in not executing since you said no errors were thrown – Eric Nov 18 '16 at 3:43
  • I was following your guidance from my question yesterday where I asked how I could clone it, keep all old field values except update the lookup to the newly inserted record: salesforce.stackexchange.com/questions/149061/… – Michael Nov 18 '16 at 3:51
  • Not sure how it relates here as this code is different objects etc. you may want to post all your related code but first do some debugging yourself. Learn how to use dev console and read the logs. I suspect you are not even performing the query since no error was thrown. – Eric Nov 18 '16 at 3:54
  • I am performing the same function for 2 different objects and have literally just reused the same code from the other question. It also has the same insert record trigger point so I am unsure how this is that different. – Michael Nov 18 '16 at 4:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.