+ Start a Discussion
BellaBella 

Updating Records Based on Input to Custom VF Page

Could someone please tell me how I would update a record from a custom vf page? I'm selecting fields from opportunities and useres and passing them to a custom vf page, then possibly editing them on that page, and those edits should be reflected in the records. How would I do that? Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
bmabma

You can use outputField to show the information for those not-updatable fields. They would be rendered the field as text on the page instead of input box.

 

Your markup would be something that look like this:

 

<apex:component controller="findOpportunity" access="global">
    <apex:form >
        <apex:pageBlock title="Opportunity Proposal" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Generate Proposal"/>
            </apex:pageBlockButtons>
                <apex:outputLabel value="Draft " for="DraftProposal"/>
                <apex:inputCheckbox value="{!o.Draft__c}"<br/>
            <apex:pageBlockSection title="User" columns="2">
                <apex:inputField value="{!u.Title}" /></br>
                <apex:outputField value="{!u.FirstName }" />
                <apex:outputField value="{!u.LastName }" />
                <apex:inputField value="{!u.Phone}" />
                <apex:inputField value="{!u.MobilePhone}" />
                <apex:outputField value="{!u.Email }" />
                <apex:outputField value="{!u.Fax }" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:component>

 

 

Please note, firstname and name are also fields you can't update through apex / visualforce.

 

Hope this solves your issue.

All Answers

ahab1372ahab1372

very basic question ...

did you read the VF docu, especially the Standard Component Reference about apex:form, apex:inputField and about Custom Controllers and extensions?

link to VF docu

 

in brief:

You controller needs to query the records and store them in one or more lists.

You VF page then references these records and their fields in apex:inputFields within an apex:from.

A Save button on the page calls a action method in your controller that saves the list variables mentioned in step 1 back to the database (update).

 

Hope this gets you started

BellaBella

I tried that and I actually manage to update the fields that belong to the record itself. But when it comes to the referanced records, I get an error that reads "Error: j_id0:j_id1:j_id2:j_id13:j_id17: An error occurred when processing your submitted information." And I'm not sure what that means because I get it even if I don't change anything.

ahab1372ahab1372

related records need to be updated in a separate call, see this page in the apex docu: update DML

 

if you have multiple records, you might have to iterate through the records, add the related records to a separate list, and then update that list.

BellaBella

Well I only have two things: an opportunity and a user that that opportunity refrences. I'm also getting something like DML currently not allowed exception. I know it has something to do with the way I'm calling my methods, but I'm really at a loss. I'll keep at it, I guess. Thanks.

ahab1372ahab1372

if you post your VF and controller code, somebody might be able to help

:-)

BellaBella

Good call. Ok so this is my controller:

 

 

public class findOpportunity {
    public final Opportunity o;
    public User u;
    ApexPages.Action saveAction = new ApexPages.Action('{!save}');
	
    public findOpportunity(){
    	o = [Select o.Id, o.Owner.Id, .... from Opportunity o Where o.id =: ApexPages.currentPage().getParameters().get('id') limit 1];
        u = [select Id, FirstName, LastName, Title, Phone, Fax, Email, MobilePhone from User where Id =: o.Owner.Id limit 1];
    }
    public Opportunity getO(){
    	return o;
    }
    public User getU(){
    	return u;
    }

    public void save(){
	try{
		update u;
	}catch(DmlException ex){
	ApexPages.addMessages(ex);
	}
    }
}

 And then the component for the page is roughly like this:

 

 

<apex:component controller="findOpportunity" access="global">
    <apex:form >
        <apex:pageBlock title="Opportunity Proposal" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Generate Proposal"/>
            </apex:pageBlockButtons>
                <apex:outputLabel value="Draft " for="DraftProposal"/>
                <apex:inputCheckbox value="{!o.Draft__c}"<br/>
            <apex:pageBlockSection title="User" columns="2">
                <apex:inputField value="{!u.Title}" /></br>
                <apex:inputField value="{!u.FirstName }" />
                <apex:inputField value="{!u.LastName }" />
                <apex:inputField value="{!u.Phone}" />
                <apex:inputField value="{!u.MobilePhone}" />
                <apex:inputField value="{!u.Email }" />
                <apex:inputField value="{!u.Fax }" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:component>

 

So what I need to happen is when I update the fields in the user like name or email or whatever, it then needs to update in the object. Thanks in advance!

 

 

ahab1372ahab1372

your page references 

 

controller="findOpportunityProposals"

Shouldn't it use the outer class "findOpportunity" instead? Your save method is not part of  findOpportunityProposals, so the Save button might not be able to call the Save method.

What does the debug log say?

BellaBella

Sorry I forgot to rename it everywhere when I posted. Pretend it all says findOpportunity. The names match and the correct fields do appear on the VF page so it's not that.

ahab1372ahab1372

What does the debug log say?

 

I also would look into the source code of the output in the browser and search for that long id stated in the error message. That should give you an idea what field is causing the trouble

ahab1372ahab1372

and o is final. Is that causing the conflict? You are returning something to o, even if the value has not changed

bmabma

There are some fields on the user you can't update through Apex / Visualforce.

 

From the markup of your components, these fields are:

User.Lastname

User.Fax

User.Email

 

If you remove these fields from your component, you can save correctly.

 

A question, why would you want to change the user information this way? The user object manages login access for your account. If someone forgot their password and you changed their email, how would they retrieve their password?

 

If you intention is to switch the owner to a different user, you can probably provide a drop-down of the available user in the system.

BellaBella

Well the short answer is that I need to do it this way because that's my assignment. And that makes sense because those are the fields that are giving me the errors. So I guess the question now is, is there any way I can display all the fields on the VF page but make only the updatable ones actually... well, update?

bmabma

You can use outputField to show the information for those not-updatable fields. They would be rendered the field as text on the page instead of input box.

 

Your markup would be something that look like this:

 

<apex:component controller="findOpportunity" access="global">
    <apex:form >
        <apex:pageBlock title="Opportunity Proposal" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Generate Proposal"/>
            </apex:pageBlockButtons>
                <apex:outputLabel value="Draft " for="DraftProposal"/>
                <apex:inputCheckbox value="{!o.Draft__c}"<br/>
            <apex:pageBlockSection title="User" columns="2">
                <apex:inputField value="{!u.Title}" /></br>
                <apex:outputField value="{!u.FirstName }" />
                <apex:outputField value="{!u.LastName }" />
                <apex:inputField value="{!u.Phone}" />
                <apex:inputField value="{!u.MobilePhone}" />
                <apex:outputField value="{!u.Email }" />
                <apex:outputField value="{!u.Fax }" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:component>

 

 

Please note, firstname and name are also fields you can't update through apex / visualforce.

 

Hope this solves your issue.

This was selected as the best answer
BellaBella

Thank you very much! That works perfectly ^_^