I am writing a class to update a record with data from visual force page if it exists in contact, if it does not it will insert a row.I am using Update and Insert statements for this purpose.Looks like the Id of current page not retrieved for update operation. I am using this to update the record. ApexPages.currentPage().getParameters().get('Id') I am using getrecord() to insert a row I am using Try Catch to perform Update and Insert. It always performs Insert.
I put Insert code in Catch because it is giving 'No rows in list to assign to SObject' error for SELECT statement .Let me know if you need code for the page.
Thank you for the help.
Code for Apex Class
public class thankyoupageredirectext {
public contact con{get;set;}
public contact con1{get;set;}
string Other_In_Kind_Donation__c,Scoop_value,description_value;
string Clothing_In_Kind_Donation_value;
string contact_url;
string Food_In_Kind_Donation_value;
public thankyoupageredirectext (ApexPages.StandardController stdController) {
con1 = (contact)stdController.getRecord();
}
public PageReference save(){
try
{
con = [SELECT Id,firstname, Title,lastname,description,The_Scoop__c,email,Other_In_Kind_Donation__c,Clothing_In_Kind_Donation__c,Food_In_Kind_Donation__c FROM contact WHERE Id = :ApexPages.currentPage().getParameters().get('Id')];
update con;
retun null;
}
catch (exception e)
{
this.con = con1;
insert con;
return null;
}
}
}
Code for Visual Force Page
<apex:page standardController="contact" extensions="thankyoupageredirectext" sidebar="false" showHeader="false" >
<style type="text/css">
.myClass {
color:Black;
font-size: 30px;
text-align:center;
}
</style>
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1" >
<apex:outputLabel value="In-Kind Donation" styleClass="myClass"/>
<apex:inputField value="{! contact.firstname}"/>
<apex:inputField value="{! contact.lastname}"/>
<apex:inputField value="{! contact.GW_Volunteers__Volunteer_Organization__c}"/>
<apex:inputField value="{! contact.Title}"/>
<apex:inputField value="{! contact.email}"/>
<apex:inputField value="{! contact.phone}"/>
<apex:inputField value="{!contact.MailingStreet}"/>
<apex:inputField value="{!contact.MailingCity}"/>
<apex:inputField value="{!contact.MailingState}"/>
<apex:inputField value="{!contact.MailingPostalCode}"/>
<apex:inputField value="{!contact.MailingCountry}"/>
<apex:inputField value="{! contact.Clothing_In_Kind_Donation__c}"/>
<apex:inputField value="{! contact.Food_In_Kind_Donation__c}"/>
<apex:inputField value="{! contact.The_Scoop__c}" required="true"/>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom" >
<apex:commandbutton value="Submit" action="{!save}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>