I have one Custom Object Patient__c.On VF page onclick of commandlink button I am filling all the correspondant fields of Patient like firstname, middle name like that. I am getting the particular id also. Now my requirment is this to insert the new patient if we did not get the id from SQL query else update the patient if we got the id. Suppose the variable name in which I am getting the id is PatientID on Apex page

Please tell me I got stuck here ??


public class ExtentionController1
  public Patient__c objPatient {get;set;}
  public string PatientID{get;set;}

  public ExtentionController1()
    objPatient = new Patient__c();
    objAccession = new Case_Accession__c(); 

  public PageRefrence save()
    upsert objPatient;
    return Null;
    objAccession.Patient__c = objPatient.id;

  public void insertPatient()
    if( Patientid == null )
      objPatient = new Patient__c();
      objPatient = [select ID,name,FirstName__c,LastName__c,MiddleName__c from Patient__c where id = :Patientid ];

Visualforce Page

<apex:page Controller="ExtentionController1"  >
  <apex:form >
    <apex:pageblock title="Accessioning" >
      <apex:dataTable id="dtPatientRecords"  value="{!objPatientList}" var="pt" width="100%" >
        <apex:param name="PatientID" assignTo="{!PatientID}" value="{!pt.id}"/>
        <apex:pageBlockSection title="Patient">
        <apex:inputHidden value="{!PatientID}"/>
        <label> Patient ID </label>
        <apex:inputText value="{!testPatientid}"/>
        <apex:inputField value="{!objPatient.FirstName__c}" />
        <apex:inputField value="{!objPatient.MiddleName__c}" />
        <apex:inputField value="{!objPatient.LastName__c}" />
        <apex:inputField value="{!objPatient.Address__c}"/>
        <apex:inputField value="{!objPatient.Country__c}"/>
        <apex:inputField value="{!objPatient.State__c}"/>
        <apex:inputField value="{!objPatient.City__c}"/>
        <apex:inputField value="{!objPatient.Gender__c}"/>
        <apex:inputField value="{!objPatient.DOB__c}"/>
        <apex:commandButton action="{!save}" value="Done"/>

FYI :: I am getting the patientid no issues in that, I cheked with static id also but no success, please help me !!!

Regards Raman

  • What part of your logic is not working? Is the upsert not happening? From the code i see is that when you click save you are creating a new instance of objPatient which would be empty and if there is a record then you are querying from Database and resaving it. None of the values you are entering in VF page would be saved – Prady Jul 12 '13 at 6:32
  • Let me know if this is my understanding of your requirement is correct. When an user enters the patient id testPatientid in Vf page then if the record exists you want to update that record with all the values you entered in the VF page otherwise create a new record and fill it with all the values from VF pages – Prady Jul 12 '13 at 6:43
  • @Prady !! Yes Prady exactly , I am getting the patientid also but update is not working fine. Please help – Raman Jul 12 '13 at 6:46
  • where are you populating objPatientList and where are you closing the datatable – Prady Jul 12 '13 at 6:50

Ideally i wouldnt be doing the way you are doing it the way you are doing. i wouldnt be allowing the user to enter the id directly. But the code below should work for what you are achieving. Remove your upsert statement in the save method

I really dont think this is the correct approach. I am not sure of what your business requirement is. It would be almost impossible for someone to know the Id of the record and key that in. If provide a clear description of what the business requirement is then probably we could give you a more elegant solution

public void insertPatient()
  if( Patientid == null )
    insert objPatient
    Patient__c objPatient1 = [select ID,name,FirstName__c,LastName__c,MiddleName__c from Patient__c where id = :Patientid ];
    If (objPatient1 != null)
      upsert objPatient
  • !! how we will get the updated value in objPatient which we entered from our text boxes of vf page ???? – Raman Jul 12 '13 at 6:57
  • That values are stored in objPatient that is why i am upserting objPatient and not objpatient1 – Prady Jul 12 '13 at 6:59
  • 1
    I removed the extra line where objPatient1 was set to a new instance because it made things a tad confusing. That said, you could replace this whole method with upsert objPatient; and it would do the same! – Matt Lacey Jul 12 '13 at 7:16

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.