1

I am creating a page where I am able to create a new case related the contact. In my controller extension in createCase() method, I am unable to populate the contact field( Lookup object on case). Thus the case gets created but the contact field is blank. Not sure how to insert the value of contact field here:

VF page:

<apex:page standardController="Contact" extensions="CreateNewCase">

<apex:form >

 <apex:pageBlock >
        <apex:pageBlockSection title="Contact Information" columns="2">

            <apex:inputField value="{!Contact.Firstname}" /> 
          <apex:inputCheckbox value="{!checkfirstname}" label="Verified FirstName" id="Firstname"> 
             <apex:actionSupport event="onchange" action="{!displayButton}" reRender="buttons"/> 
          </apex:inputCheckbox> 
            <apex:inputField value="{!Contact.Lastname}" />
            <apex:inputCheckbox value="{!checkLastName}" label="Verified LastName" id="LastName">
             <apex:actionSupport event="onchange" action="{!displayButton}" reRender="buttons"/> 
             </apex:inputCheckbox>
            <apex:inputField value="{!Contact.SSN__c}" />
            <apex:inputCheckbox value="{!checkSSN}" label="Verified SSN" id="SSN">
             <apex:actionSupport event="onchange" action="{!displayButton}" reRender="buttons"/>     
            </apex:inputCheckbox>
            <apex:inputField value="{!Contact.name}" /> 
        </apex:pageBlockSection>
          <apex:pageBlockButtons location="bottom">
          <apex:outputPanel id="buttons">
        <apex:commandButton action="{!save}" value="Search" id="SearchButton"/>
        <apex:commandButton action="{!createCase}" disabled="{!casestatus}"  value="New Case" id="NewCase"/>
        </apex:outputPanel>

         </apex:pageBlockButtons>
    </apex:pageBlock>

</apex:form>

</apex:page>

Controller extension:

public class CreateNewCase {

    public Boolean casestatus {get;set;}
    public Boolean checkfirstname{get;set;}
    public Boolean checkLastName{get;set;}
    public Boolean checkSSN{get;set;}

    private Contact cnt;

    public CreateNewCase.CreateNewCase(ApexPages.StandardController stdController){
        casestatus=true;
        checkfirstname=false;
        checkLastName=false;
        checkSSN=false;
        this.cnt= (Contact)stdController.getRecord();

    }

    public PageReference displayButton() {
        if(checkfirstname && checkLastName && checkSSN){
            casestatus=false;
        }else{
        casestatus=true;
        } 
      return null;
  }

    public PageReference createCase(){
        **Case c=new Case(Status='New',Origin='Phone',Subject='This is a New Case Created',Contact=[select id,name from Contact where Id=:cnt.id]);** 
        System.debug('Contact is *************' + cnt);
         System.debug('Contact is *************' + cnt.ID);
        Insert c;    
        return null;
    }




}
1

I found the Solution, I was using the wrong syntax to assign the contact field. here is how it should be used in the controller extension in createCase() method:

 public PageReference createCase(){
        Case c=new Case(Status='New',Origin='Phone',Subject='This is a New Case Created',ContactId=cnt.id);
        System.debug('Contact is *************' + cnt);
         System.debug('Contact is *************' + cnt.ID);
        Insert c;    
        return null;
    }

This workd now. I am able to connect the new case with my contact.

| improve this answer | |

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.