0

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>
2
  • Please add VFP also Mar 27, 2017 at 0:04
  • Added code for VFP
    – SFDC
    Mar 27, 2017 at 0:10

1 Answer 1

0

Better gather the data during page load and assign in the controller Constructor.

And finally perform upsert operation (which is combination of insert and update) in Save method. Refer Upsert Records in documentation.

Controller

public contact con{get;set;}
public thankyoupageredirectext (ApexPages.StandardController stdController)    
{
    con = (contact)stdController.getRecord();     
}     



 public PageReference save()
 { 
     try
    {   upsert con;     
        return null;
    }
     catch (exception ex)
    {
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
        ApexPages.addMessage(msg);
        return null;
    }
}

Change the <apex:inputField/> value attribute from Contact to con (which is Contact instance)

Visualforce

<apex:form>
     <apex:inputField value="{! con.firstname}"/> 
     <apex:inputField value="{! con.lastname}"/>
     <!--  add other attributes -->

</apex:form>

Updated based on comments

Put id explicitly in the parameter

PageReference pg = new PageReference(ApexPages.CurrentPage);
pg.getParameters().put('id', controller.getId());
pg.setRedirect(true);
return pg;
6
  • Thanks Santanu. I modified the code as you mentioned. I tested it. After a new row is inserted, if I update any fields on the page , it is updating the record . But if I open the page again or refresh the page and try to update the existing row , it's creating new row again. Not sure what is causing this and what is missing??
    – SFDC
    Mar 27, 2017 at 1:07
  • I think, if you open the page again or refresh the page, id attribute in the url is missing Mar 27, 2017 at 1:43
  • Yes, that's true.Any work around??
    – SFDC
    Mar 27, 2017 at 2:04
  • then try to pass explicitly id into that parameter Mar 27, 2017 at 2:17
  • Santanu, Where should I place this in the class.Is it before getRecord()
    – SFDC
    Mar 27, 2017 at 10:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .