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
AlaaAlaa 

Which apex tag to update the value of some field in opportunity object

Hi, I created a visualforce page and mapped it to opportunity object ... in some scenario I want to update the value of one field in the opportunity object from this visualforce page so what apex tag should I use? Is there anyway to do so?

Afzal MohammadAfzal Mohammad

Yes, you can use <apex:inputfield> tag. Here is an example for updating an account.

 

 

<apex:page standardController="Account">
    <apex:form>
        <apex:pageBlock title="My Content" mode="edit">
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="My Content Section" columns="2">
                <apex:inputField value="{!account.name}"/>
                <apex:inputField value="{!account.site}"/>
                <apex:inputField value="{!account.type}"/>
               <apex:inputField value="{!account.accountNumber}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

Hope that helps.

 

Afzal

@imtimran.ax982@imtimran.ax982

You can use apex:inputField tag to do so.

It will bing the value on the Visualforce page directly to the object.

 

Are you using Custom controller?

AlaaAlaa

Thank you.. but how can I do this automatically without asking the user to click the save button ? what I need is to update some field value when user update or edit an opportunity so I want to update the fiedl value automatically without asking the user to fill a from and click save. .. is there anyway to do so?

TheSwamiTheSwami

If I understand correctly, here is an example:

 

 

<apex:page standardController="Opportunity" extensions="myControllerExtension">
  <apex:form >
      <apex:inputField value="{!Opportunity.name}"/>
      <apex:inputField value="{!Opportunity.stagename}"/>
      <apex:inputField value="{!Opportunity.closedate}"/>            
      <apex:commandButton value="Save" action="{!saveOpportunity}"/>
  </apex:form>
</apex:page>

 

public class myControllerExtension {

    private final Opportunity opp;
    
    public myControllerExtension(ApexPages.StandardController stdController) {
        this.opp = (Opportunity)stdController.getRecord();
    }

    public void saveOpportunity() {
        opp.description = 'Created on ' + System.today();
        insert opp;
    }
}

 

When the user clicks the "Save" button, the controller will set the Opportunity Description automatically, and insert the record.

 

AlaaAlaa

can I create a method "updateDescription()" in this controller and call it once the page loads to update the description field based on some logic without having any form ?

TheSwamiTheSwami

Yes.  Here are some possible ways to do it:

- Specify an action in the <apex:page> tag.  This will be called when the page is loaded.  

- Create an actionFunction and call it using javascript when the page loads.

 

I'm not sure that performing DML when the page loads is a good idea, but it can be done.

AlaaAlaa

I agree with you its not a good idea to do this on page load but i dont want to ask the user to trigger some action or rely on him to do this... I want to do this automatically.... whenever an opportunity is getting updated I want to check some logic and then update some field value in the opportunity object.... I can do it using appex triggers and put the event "on update"  but then  I dont know how to get the current record being updated and update the field value ... I appreciate it if you have any example on this ?

TheSwamiTheSwami

Getting the current record in a trigger is easy.

There is a context variable called trigger.new which is an array of all the records to be updated.  This is a simple example which will set the description for accounts when they are updated:

 

 

Trigger t on Account (before update) {
    for (Account a : Trigger.new) {
        // Iterate over each sObject  
        a.description = 'My Description';
    }
}

 

Take a look at the section on "Invoking Apex | Triggers" in the documenation:

http://www.salesforce.com/us/developer/docs/apexcode/index.htm

 

AlaaAlaa

Thnak you for your help.