The problem arising clone recorded id does'not updated so and does'not appear related list and updated field so how should i do this?

Apex class

Public Class CloneVersionwithTextController{

Public List<Master_Service_Agreement_Version__c> msalist{get;set;}
Public Master_Service_Agreement_Version__c msaCopy;
public string msaid{get;set;}

Public CloneVersionwithTextController(){

    msaid = ApexPages.CurrentPage().getParameters().get('msaid');    

public void cloneRec(){
    system.debug('===clonme()===='+msaid );
    List<Master_Service_Agreement_Version__c> msalist = new List<Master_Service_Agreement_Version__c>();
    msalist = [SELECT  Name, Status__c FROM Master_Service_Agreement_Version__c WHERE Id = : msaid ];
    if(msaList.size() > 0){
        msaList.get(0).Name = msaList.get(0).Name + '    ' + Datetime.now();
    Master_Service_Agreement_Version__c msaCopy = msalist[0].clone(false,true); 
    insert msaCopy;
    List<MSA_Text_Block__c> mtext = new List<MSA_Text_Block__c>();
    mtext = [SELECT Id, Name, Service__c, Order_within_Service__c,Master_Service_Agreement_Version__r.ID,LastModifiedBy.Name FROM MSA_Text_Block__c WHERE Master_Service_Agreement_Version__r.ID = : msaid];
    for(MSA_Text_Block__c c : mtext)
        MSA_Text_Block__c mtextCopy = c.clone(false,true);
        mtextCopy.Master_Service_Agreement_Version__c = msaCopy.Id;


Visualforce Page

<apex:page controller="CloneVersionwithTextController" action="{!cloneRec}">
   <apex:detail subject="{!msaid}" relatedList="true" title="false"/>
  • can you try clone(true,true,false,true) – San Aug 18 '15 at 12:40
  • sam i clone the record show that error Insert failed. First exception on row 0 with id a3MP0000000CdtiMAC; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Error is in expression '{!cloneRec}' in component <apex:page> in page cloneversionwithtext: Class.CloneVersionwithTextController.cloneRec: line 20, column 1 – Ravi Vadher Aug 18 '15 at 12:47
  • Clone(true,true,false,true) is trying to retain ID of current record so you have to use upsert. – San Aug 18 '15 at 12:49

As you only want to update the record. Change the statement to:

    Master_Service_Agreement_Version__c msaCopy = msalist[0].clone(true,true,false,false); 
    update msaCopy;

or every place where you want update record. To update record Id is required.

If you want to insert record then clone would be like:

        // Don't retain ID
        Master_Service_Agreement_Version__c msaCopy = msalist[0].clone(false,true,false,false); 
        update msaCopy;

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.