0

Unable to pass contact from VF to the controller. I have to check if the contact is inactive on VF page if it inactive, allow user to change the contact and pass the changes to the controller. If contact is active, use the same. Here's what my VF page looks like:

    <apex:page standardController="Case" tabStyle="Case" lightningstylesheets="true"
 extensions="TestCase_Controller">
 <apex:sectionHeader title="{!$Label.ClaimAP_Clone_Title}" subtitle="{!Case.CaseNumber}"/>
<apex:form >
    <apex:pageBlock mode="inlineEdit" title="">
        <apex:pageblockButtons >
            <apex:commandButton action="{!cloneCase}" value="{!$Label.ClaimAP_Clone_ButtonClone}" />
            <apex:commandButton action="{!Cancel}" value="{!$Label.ClaimAP_Clone_ButtonCancel}" />
        </apex:pageblockButtons>
        <apex:pageBlockSection columns="1" title="{!$Label.ClaimAP_Clone_CloneLegend}" collapsible="false">
            <apex:inputField value="{!Case.ContactId}"/>
        </apex:pageBlockSection> 
    </apex:pageBlock>
</apex:form>
 <apex:pageMessages />

My controller is this:

    public class TestCase_Controller {
private ApexPages.StandardController controller {get; set;}
private Case CaseRecordQueried{get;set;}
public Case CaseRecord {get;set;}
public Case Contactid;

public TestCase_Controller(ApexPages.StandardController controller) {

    this.controller = controller;
    CaseRecord = (Case)controller.getRecord();
    Contactid = (Case)controller.getRecord();
    //Contactid = (Case)
    CaseRecordQueried = [select Id,
                            AccountId,
                            CaseNumber,
                            External_Reference_Number__c,
                            FSR__c,
                            Total_Lines__c,
                            Opportunity__c,
                            EU_Sample_Type__c,
                            Comments_For_Customer__c,
                            Shipment_Status__c,
                            Comments_for_Stratos__c,
                            Customer_PO__c,
                            Comments_for_Manager__c,
                            Division__c,
                            Lead__c,
                            Shipment__c,
                            Customer_Language__c,
                            Shipment_Contact__c,
                            Shipment_Contact_Phone__c,
                            Shipment_Address__c,
                            Work_in_progress__c,
                            Carrier__c,
                            UPS_Tracking_Nbr__c,
                            AWB_Number__c,
                            Country__c,
                            Comments_from_Stratos__c,
                            Cost_of_Sample__c,
                            Shipment_Cost__c,
                            Total_Cost__c,
                            Origin,
                            Description,
                            Priority,
                            Requires_Approval__c,
                            RecordTypeId
                     from Case
                     where id =: CaseRecord.Id];
}

public PageReference cloneCase() {

     String contactid = Contactid.ContactId;
     Savepoint sp = Database.setSavepoint();
     Case CaseCloned;
     try {
         CaseCloned = CaseRecordQueried.clone(false);
         insert CaseCloned;
     } catch (Exception e){
        Database.rollback(sp);
        ApexPages.addMessages(e);
        return null;
     }
    return new PageReference('/'+CaseCloned.id);

}

}

3
  • value of Contactid.ContactId will always be null, because on VF page you have bind contact with Case.ContactId. Commented Oct 21, 2018 at 13:20
  • Could you please help me how do I rectify this to get the desired result?
    – VChandra
    Commented Oct 21, 2018 at 13:27
  • Please omit content which has nothing to do with what you are asking, such as "I am new to X". Stick to the facts and where specifically you are stuck. Also you can format a code block by highlighting int and hitting CTRL + K.
    – Adrian Larson
    Commented Oct 21, 2018 at 16:15

1 Answer 1

1

Above code should be like:

<apex:page standardController="Case" tabStyle="Case" lightningstylesheets="true"
extensions="TestCase_Controller">
<apex:sectionHeader title="{!$Label.ClaimAP_Clone_Title}" subtitle=" 
{!CaseRecordQueried.CaseNumber}"/>
<apex:form >
    <apex:pageBlock mode="inlineEdit" title="">
       <apex:pageblockButtons >
          <apex:commandButton action="{!cloneCase}" value="{!$Label.ClaimAP_Clone_ButtonClone}" />
          <apex:commandButton action="{!Cancel}" value="{!$Label.ClaimAP_Clone_ButtonCancel}" />
        </apex:pageblockButtons>
        <apex:pageBlockSection columns="1" title="{!$Label.ClaimAP_Clone_CloneLegend}" collapsible="false">
            <apex:inputField value="{!CaseRecordQueried.ContactId}"/>
        </apex:pageBlockSection> 
       </apex:pageBlock>
    </apex:form>
  <apex:pageMessages />
</apex:page>

Controller code should be like:

  public class TestCase_Controller {
     private ApexPages.StandardController controller {get; set;}
     public Case CaseRecordQueried{get;set;}

     public TestCase_Controller(ApexPages.StandardController controller) {
         this.controller = controller;
         CaseRecordQueried = [select Id,
                        AccountId,
                        ContactId,
                        CaseNumber,
                        External_Reference_Number__c,
                        FSR__c,
                        Total_Lines__c,
                        Opportunity__c,
                        EU_Sample_Type__c,
                        Comments_For_Customer__c,
                        Shipment_Status__c,
                        Comments_for_Stratos__c,
                        Customer_PO__c,
                        Comments_for_Manager__c,
                        Division__c,
                        Lead__c,
                        Shipment__c,
                        Customer_Language__c,
                        Shipment_Contact__c,
                        Shipment_Contact_Phone__c,
                        Shipment_Address__c,
                        Work_in_progress__c,
                        Carrier__c,
                        UPS_Tracking_Nbr__c,
                        AWB_Number__c,
                        Country__c,
                        Comments_from_Stratos__c,
                        Cost_of_Sample__c,
                        Shipment_Cost__c,
                        Total_Cost__c,
                        Origin,
                        Description,
                        Priority,
                        Requires_Approval__c,
                        RecordTypeId
                 from Case
                 where id =: controller.getId()];
       }

      public PageReference cloneCase() {
         String contactid = CaseRecordQueried.ContactId;
         Savepoint sp = Database.setSavepoint();
         Case CaseCloned;
        try {
           CaseCloned = CaseRecordQueried.clone(false);
           insert CaseCloned;
           } catch (Exception e){
         Database.rollback(sp);
         ApexPages.addMessages(e);
         return null;
       }
       return new PageReference('/'+CaseCloned.id);
    }
  }
0

You must log in to answer this question.

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