-1

I have a custom VF page and apex controller that mimics the standard Clone for Contracts. Currently, it is copying the date fields as they're from the previous record. However, I want to make the date fields null and let the users enter new dates which will be inserted in the newly cloned contract record.

    <apex:page standardController="Contract" tabStyle="Contract" lightningstylesheets="true"
       extensions="ContractClone">
 <apex:sectionHeader title="Clone Contract" subtitle=" {!ContractRecordQueried.ContractNumber}"/>

<apex:form >
<apex:pageBlock mode="inlineEdit" title="">
   <apex:pageblockButtons >
      <apex:commandButton action="{!cloneContract}" value="Clone"/>
      <apex:commandButton action="{!Cancel}" value="Cancel"/>
    </apex:pageblockButtons>
    <apex:pageBlockSection columns="2" title="General Information" collapsible="false">         
        <apex:inputField value="{!ContractRecordQueried.Division__c}" required="true"/>
        <apex:inputField value="{!Contract.Status}" label="Status" required="TRUE" id="contractStatus"  />
        <script>
            document.querySelector('[id$=contractStatus]').value = 'Initial Contract Discussion';
        //document.querySelector('[id$=contractStartDate]').value = "";
        //$("#contractStartDate").val(""); 
        </script>
        <apex:pageBlockSectionItem />
        <apex:outputField value="{!ContractRecordQueried.CustomerSignedDate}"/>
        <apex:pageBlockSectionItem />
        <apex:outputField value="{!ContractRecordQueried.ESignature_Status__c}"/>
        <apex:inputField value="{!ContractRecordQueried.Name}" required="true"/>           
        <apex:inputField value="{!ContractCloned.StartDate}" required="true" id="contractStartDate"/>
        <apex:inputField value="{!ContractRecordQueried.Description}" required="true"/>
        <apex:inputField value="{!Contract.EndDate}" label="Contract End Date"/>             
    </apex:pageBlockSection>

</apex:form>
 <apex:pageMessages />
 </apex:page>

Apex Controller:

    public class ContractClone {

 private ApexPages.StandardController controller {get; set;}
 public Contract ContractRecordQueried{get;set;}
 public Contract ContractCloned {get;set;}

 public ContractClone(ApexPages.StandardController controller) {
     this.controller = controller;
     ContractRecordQueried = [select Id,
                              ContractNumber,
                              CustomerSignedDate,
                              StartDate,
                              EndDate,                                  
                              Name

             from Contract
             where id =: controller.getId()];
   }

  public PageReference cloneContract() {
     //String contactid = ContractRecordQueried.ContactId;
     Date start_date = ContractRecordQueried.StartDate;
     Date end_date = ContractRecordQueried.EndDate;
     Savepoint sp = Database.setSavepoint();
     Contract ContractCloned;
    try {
       ContractCloned = ContractRecordQueried.clone(false);
       insert ContractCloned;
       } catch (Exception e){
     Database.rollback(sp);
     ApexPages.addMessages(e);
     return null;
   }
   return new PageReference('/'+ContractCloned.id);
}

}
6
  • So, you're doing a clone() operation in cloneContract() - that looks like the place where you could change values on the record, right? I'm not sure what your question is.
    – David Reed
    Commented Feb 15, 2019 at 13:36
  • Actually, the contract on which I am going to perform a clone shall have a value (older date), so when I click on Clone button (which consists of VF input form page and Apex controller), I have a VF input form and I want the Date fields to be empty so that I can allow users to enter the new values and the new values should be inserted in the newly cloned record.
    – VChandra
    Commented Feb 15, 2019 at 13:46
  • 1
    OK. What's your question? Can you please edit your post to make clear what you are struggling with? It looks to me like you just need to set some fields to null.
    – David Reed
    Commented Feb 15, 2019 at 14:09
  • Yes, I need to set the Start Date and End Date to blank on input form (VF page) and allow user to enter new values so that the newly cloned record is created with the new date values entered by the user.
    – VChandra
    Commented Feb 15, 2019 at 14:28
  • 1
    You will have to do that in your constructor where you are querying for existing record. Set the startDate and endDate on the ContractRecordQueried record after the query.
    – Aayush K
    Commented Feb 15, 2019 at 17:10

1 Answer 1

1
public ContractClone(ApexPages.StandardController controller) {
     this.controller = controller;
     ContractRecordQueried = [select Id,
                              ContractNumber,
                              CustomerSignedDate,
                              StartDate,
                              EndDate,                                  
                              Name

             from Contract
             where id =: controller.getId()];
     // Empty out the dates --  What @Aayush K was saying
     this.ContractRecordQueried.StartDate = null;
     this.ContractRecordQueried.EndDate = null;
   }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .