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

asish1989asish1989

Try this 

public class Patient {

      private final Patient__c pat{get;set;};
	  public Patient(){
		Id id = ApexPages.currentPage().getParameters().get('id');
		pat = (id==null) ? new Book__c() : [Select id, necessary filed which are in vf page.... From Patient__c];
	  }
	  
	  public PageReference save(){
		upsert pat;
		return null;
	  }
}	
in vf page write 
 <apex:inputField value="{!pat.Title__c}" label="Title" />

 

TechnosTechnos

Hi Ashish Thanks for reply.

 

I have one question by this statement "Select id From Patient__c" we will get all the ids of  Patient__c which are available , I guess we can not upsert all patient in one shot. Please try to understand my requirment again.

 

I am geetting the id of particular patient onclick of one button now my requirment is to compare this id with all the available ids of patient, if id exist than update the record

 

else insert the record.

 

 Suppose the variable i got is patientid

if (patientid == select ids from patient)

update

else

insert;

 

like that

 

Thanks in advance, I am wating for ur reply !!

 

Regards

Raman

asish1989asish1989

sory I just forgot to add condition

 

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;
	  }
}	

 If this helps you please mark it as solved and give kudos