I'm trying to clone an object, here is the code:

global static void cloneRow(String idProj){
    System.debug('@@ clone: '+idProj);
    ConfigeroPSA__Expense__c oldExp = [SELECT Id,Name, ConfigeroFIN__Approval_Status__c,ConfigeroFIN__ApproverId__c,ConfigeroFIN__Contributes_to_Service__c,ConfigeroFIN__Estimated_Direct_Charge__c,ConfigeroPSA__Amount__c,ConfigeroPSA__Bill_Amount__c,ConfigeroPSA__Category_Type__c,ConfigeroPSA__Comment__c,ConfigeroPSA__Date__c FROM ConfigeroPSA__Expense__c WHERE Id =: idProj];
    ConfigeroPSA__Expense__c newExp = oldExp.clone(false,true);
    insert newExp;
    System.debug('@@ cloned: '+newExp.Id);

I got this error:

Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Mancano le informazioni nei campi obbligatori: [Project]: [Project]

It looks like I missed some field but it's false. Do you know how I could resolve? Thanks in advance!

  • What is Project? – PepeFloyd May 18 '15 at 14:57
  • I think is ConfigeroFIN__Estimated_Direct_Charge__c because is a master/detail field. It doesn't exist any field with the name Project. – DarkSkull May 18 '15 at 14:59
  • Does this object have multiple Record Types? If so do different Record Types have different fields marked as Required? It may be that the record you query is of one Record Type, but the running user, has a different default Record Type which requires a field that the first doesn't? – dBeltowski May 18 '15 at 15:28
  • If I try to change clone(false,true) to clone(true,true) (in this way it clones also the id), I got this error: Insert failed. First exception on row 0 with id a0be0000004MS15AAG; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Why? – DarkSkull May 18 '15 at 15:42

Any fields you do not query for are going to be null in your query result. Hence if there are required fields you do not query for, your clone will fail.

You may want to determine required fields programmatically with describes and merge them into dynamic SOQL, but that is somewhat outside the scope of this question. Check out this question for a starting point, and if you are still struggling, ask a new question around that problem.


The solution is in this page: http://sfdc.arrowpointe.com/2011/03/28/cloning-records-in-apex/

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.