0

this is a follow-up question to: How to clone and selectively change fields on cloned object?

Situation: I want to clone a custom object, update a few fields and insert the cloned object into the database. The object request__c is a child object. I have since learned that I don't even need to clone the object, but can just use the standard controller, change the field I want to change, null the id of the initial object, and then insert into the DB (see Controller below).

My Problem: I am trying to redirect to the inserted record after insertion. However, all I see after inserting is a blank page.

ExtensionCreatorController:

public class ExtensionCreatorController {

    private final Request__c requ;

    public ExtensionCreatorController(ApexPages.StandardController stdController) {
        stdController.addFields(new List<String>{ 'Opportunity__c', 'Landlord__c', 'Billing_Address__c'});
        this.requ = (Request__c)stdController.getRecord();
    }

    public System.PageReference InsertClone() {
        requ.Extension_of__c = requ.name;           
        requ.id = null;
        requ.BookingID__c = requ.name;
        requ.name = 'EX-'+requ.name;
        requ.BookingDateBackEnd__c = Date.today();
        insert requ;
        return new ApexPages.StandardController(requ).view();
}}

Visualforce Page:

<apex:page standardController="Request__c" extensions="ExtensionCreatorController">
    <apex:form>
        <apex:pageBlock title="My Request Input"> 
            <apex:pageBlockSection title="Input Section" columns="3">
                <apex:inputField value="{!Request__c.name}"/>
                <apex:inputField id="inpMovein" value="{!Request__c.Moveindate__c}"/>
                <apex:inputField id="inpMoveout" value="{!Request__c.Moveoutdate__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form> 
</apex:page>`
1
  • Do you have any action overrides on Request__c? – David Reed Mar 21 '18 at 19:18
2

You are not calling an InsertClone method from commandLink/button or action function.

<apex:page standardController="Request__c" extensions="ExtensionCreatorController">
    <apex:form>
        <apex:pageBlock title="My Request Input"> 
            <apex:pageBlockSection title="Input Section" columns="3">
                <apex:inputField value="{!Request__c.name}"/>
                <apex:inputField id="inpMovein" value="{!Request__c.Moveindate__c}"/>
                <apex:inputField id="inpMoveout" value="{!Request__c.Moveoutdate__c}"/>
            </apex:pageBlockSection>
           <apex:commandButton action="{!InsertClone}" value="Save"/>
        </apex:pageBlock>
    </apex:form> 
</apex:page>

In your Apex controller, You are not clonning properly. I would advise using standard Sobject clone.

public System.PageReference InsertClone() {
        Request__c cloned=req.clone(); 
        cloned.Extension_of__c = requ.name;           
        cloned.id = null;
        cloned.BookingID__c = requ.name;
        cloned.name = 'EX-'+requ.name;
        cloned.BookingDateBackEnd__c = Date.today();
        insert cloned;
        return new ApexPages.StandardController(cloned).view();
}
3
  • Absolutely correct, I had an older version of the code included. I am indeed calling the InsertClone(). Tried your update, but still only see a blank page :( – Fabian Bosler Mar 21 '18 at 16:39
  • I tested this code in my org to confirm. So Am sure this works. Are you testing it on classic or lightning? – Pranay Jaiswal Mar 21 '18 at 16:40
  • I am currently developing on a sandbox in classic mode. – Fabian Bosler Mar 21 '18 at 16:54

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.