Upsert

Upsert 

this page updates the Account Name and Phone of the Account whose Id is available in apex current page, if no id is available then new account is created with the name and phone provided.
  1. <apex:page controller="testInputfieldsController">
  2. <apex:form >
  3. <apex:inputField value="{!acc.name}"/>
  4. <apex:inputField value="{!acc.phone}"/> <br/>
  5. <apex:commandButton value="Save" action="{!save}"/>
  6. </apex:form>
  7. </apex:page>
  1. public class testInputfieldsController {
  2.     public Account acc;
  3.     
  4.     public PageReference save() {
  5.         upsert acc;
  6.         return null;
  7.     }

  8.     public Account getAcc() {
  9.         return acc;
  10.     }
  11.     public testInputfieldsController () {
  12.     Id id = apexPages.currentPage().getParameters().get('id');
  13.     if(id==null){
  14.     acc=new Account();
  15.     }
  16.     else {
  17.     acc = [SELECT Id, Name, Phone from Account WHERE Id =: id];
  18.     }
  19.     }

  20. }

Comments

Popular posts