function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TechnosTechnos 

Insert and Update on Same Button on VF page

Hi all,

 

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 ??

 

Regards
Raman

AsitM9AsitM9
use upsert..
asish1989asish1989

Hi

Try this

public class Patient {

      private final Patient__c pat{get;set;};
	  public Patient(){
		Id patId = ApexPages.currentPage().getParameters().get('id');
		pat = (id==null) ? new Book__c() : [Select id, necessary filed which are in vf page.... From Patient__c WHERE id =:patId ];
	  }
	  
	  public PageReference save(){
		upsert pat;
		return null;
	  }
}
TechnosTechnos

I am not able to update the record its inserting if Patientid not found but data is not updating of the input filed, please take a look:

 


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()
 {
  insertpatient()
  upsert objPatient;
  return Null;
  objAccession.Patient__c = objPatient.id;
 }
 public void insertPatient()
  {
       
        
          if( Patientid == null )
          {
           objPatient = new Patient__c();
           
          }
         else
          {
        
              objPatient = [select ID,name,FirstName__c,LastName__c,MiddleName__c from Patient__c where id = :Patientid ];
             
   }
  }
 }

 

------------------------------Visual Force 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:pageBlockSection>
 <apex:pageBlockButtons >
                  <apex:commandButton action="{!save}" value="Done"/>
            </apex:pageBlockButtons>
        </apex:pageblock>
  </apex:form>
</apex:page>

 

 

 

 

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

Laxman RaoLaxman Rao

Try this 

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()
{
insertpatient()
upsert objPatient;
return Null;
objAccession.Patient__c = objPatient.id;
}
public void insertPatient()
{

Patientid = ApexPages.currentPage().getParameters().get('PatientID');
if( Patientid == null )
{
objPatient = new Patient__c();

}
else
{

objPatient = [select ID,name,FirstName__c,LastName__c,MiddleName__c from Patient__c where id = :Patientid ];

}
}
}

TechnosTechnos

Thanks for reply but, its creating new patient no updating the existing one and one more thing, all the fields are showing as blank like firstname, middlename last name .  Patientid  and 'PatientID' are different ????? If yes than we need to declare Patientid   this also ????

 

Regards

Raman

asish1989asish1989

Patientid = ApexPages.currentPage().getParameters().get('PatientID');

give in Patientid in the url and see what happens.

 

https://na12.salesforce.com/apex/pagename?Patientid =''