0

I have read blogs posted on dev community too but this is not getting resolved. I am creating a custom button to clone the cases.

    <apex:page standardController="case" extensions="VFController">
<apex:form id="frmId">

       <apex:pageBlock id="pb">

         <apex:messages ></apex:messages>
    <!--<apex:sectionHeader title="objcase Edit" subtitle="{!objcase.objcaseNumber}"/>
    <apex:form >-->
        <apex:pageBlockSection columns="1" title="Clones objcase" id="pbs_Clone">





                <apex:inputField value="{!objcase.OwnerId}" required="false"/>
                <apex:inputField value="{!objcase.AccountId}" required="true"/>
                <apex:inputField value="{!objcase.caseNumber}" required="false"/>
                <apex:inputField value="{!objcase.Status}" required="true"/>
                <apex:inputField value="{!objcase.Customer_Status__c}" required="true"/>

            </apex:pageBlockSection>




<apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" rerender="theForm"/>
 />
            <apex:commandButton value="Cancel" action="{!Cancel}" rerender="theForm"/>
 />
         </apex:pageBlockButtons> 

        </apex:pageBlock>
    </apex:form>
</apex:page>

This is my controller:

    public Class VFController
{
    public Case objcase {get;set;}        
    public string caseID;                        

    private Case objNewcase;
    private string queryString = '';
    public string strPrevCurrency {get;set;}
    Map<String, Schema.SObjectField> mapcaseFields;

    //constructor
    public VFController(ApexPages.StandardController controller) {
          caseID = ApexPages.currentPage().getParameters().get('id');

        if(caseID != null)
        { 
                mapcaseFields = Schema.SObjectType.case.fields.getMap() ;
                objNewcase = [ select id,OwnerId,AccountId,caseNumber,Status,Customer_Status__c from case where id = :caseID ] ;
                objcase = objNewcase.clone(false,true,false,false);


        }    
    }
}

How should i fix this?

Thanks, Richa

  • You should post your whole controller, especially the save method. Also post the full error message, and from the debug line what row is failing? – Mike Katulka Jan 15 '16 at 16:22
1

You are using the standard controller Save method which works on the case record from which the request was originated and thus it is giving this error.

You need to write a custom save method in your extension controller like this :

public pageReference save(){
        insert objcase ;
        return null;
    } 

with this you would be able to save the record.

  • 1
    But that's not really the appropriate way to save the record. – sfdcfox Jan 15 '16 at 17:22
1

Quit reinventing the wheel! If you have a StandardController, take advantage of OOB functionality.

Retrieve Id

You are retrieving the Id in a case-sensitive way. Normally Apex is case-insensitive, but maps (e.g. VF/REST parameters) enforce case-sensitivity for the most part. Try using the built in getId functionality instead.

Id caseId = controller.getId();

Retrieve record

Also, you do not need to manually build the query, let alone clone anything. You can use addFields and getRecord and get everything you need. Note you can't call addFields from a test.

List<String> fields = new List<String>
{
    'OwnerId', 'AccountId', 'CaseNumber', 'Status', 'Customer_Status__c'
};
if (!Test.isRunningTest()) controller.addFields(fields);
Case record = controller.getRecord();
0

This is full custom controller:

public Class VFController
{
    public Case objcase {get;set;}        
    public string caseID;                        

    private Case objNewcase;
    private string queryString = '';
    public string strPrevCurrency {get;set;}
    Map<String, Schema.SObjectField> mapcaseFields;

    //constructor
    public VFController(ApexPages.StandardController controller) {
          caseID = ApexPages.currentPage().getParameters().get('id');

        if(caseID != null)
        { 
                mapcaseFields = Schema.SObjectType.case.fields.getMap() ;
                objNewcase = [ select id,OwnerId,AccountId,caseNumber,Status,Customer_Status__c from case where id = :caseID ] ;
                objcase = objNewcase.clone(false,true,false,false);

                        }    
    }
}

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.