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
beachbum50beachbum50 

programatically updating an object record in apex

I have a controller extension that needs to have it's own 'customised' !save command. I call it !save2

 

Question 1: how can I force a record to be saved in apex code? Can I call the standard controller's

!save from within my extension's !save2 ?

 

Question 2: how can I edit - in code - the object that is to be saved? I need to set one of the record fields

to a specific value before saving.

 

Question 3: once saved - how do I return to the original record window? How can I redirect to a record

from within apex code?

 

Any pointers to code examples would really behelpful.

TIA.

Best Answer chosen by Admin (Salesforce Developers) 
beachbum50beachbum50

Thanks for all your help above - I'm now getting the results that I need thanks to all of your replies above.

Many thanks for helping me get started in VF/Apex.

Cheers.

All Answers

aalbertaalbert

You can write your own save action method in your controller extension that performs its own DML operation (insert, update, ect).

 

For example:

 

public PageReference save2(){ CustomObject__c co = new CustomObject__c(); co.name='setting name field'; insert co; PageReference pf = new PageReference(co.id); return pf; }

 

 There are plenty of details and code samples here

 

beachbum50beachbum50

hey thanks - that has really helped me. The only reamaining problem I have is how to reference/pass

input field data on my VF page in my controller extension. For example, if I have the following in my VF

page:

 

<apex:inputField value="{!opportunity.Order_Job_Comment__c}" />  //----------- NOTE 1
<apex:commandButton action="{!opcancel}" value="Cancel Opp/Order" />

then in my 'custom' controller extension method (in apex) how would I read the value entered for

opportunity.Order_Job_Comment__c ?

 

public PageReference opcancel() {
      
      if (opportunity == null) {       
        opportunity = [select id, Opp_Order_Status__c from Opportunity where id =
                        :ApexPages.currentPage().getParameters().get('id')];   
        opportunity.Order_Job_Comment__c = <how to I read the inputField value as above at NOTE 1?>             
        update opportunity;                      
      }
}

How do you pass VF page input field data or read it in the controller extension apex code?

TIA.


  

harryZharryZ

you will need to create a setter method for that field or just simply add a set proprety to you opportunity like below.

 

public Opportunity oppo{get;set;}

 

you can view the doc for details about getter and setter methods.

 

beachbum50beachbum50

Thanks for all your help above - I'm now getting the results that I need thanks to all of your replies above.

Many thanks for helping me get started in VF/Apex.

Cheers.

This was selected as the best answer