0

I'm trying to update an Account from a visualforce but so far I haven't managed to be able to save the changes. I'm using apex:inputField for the user interface but when I try to save the values they are not updated. My fields are not updating from the visualforce.

Here's the main part of my page:

<div class="portlet-body">
    <form class="form-horizontal" role="form">
        <div class="form-body">
            <apex:form>
            <img src="https://allizancespecializedsytems--emorandev.cs2.my.salesforce.com/img/social/unknown_company_pic.png"/>
            <h2 class="margin-bottom-20">{!Account.Name} </h2>
                <div class="row">
                    <!--/span-->
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label col-md-3">Nombre:</label>
                            <div class="col-md-9">
                                <apex:inputField value="{!account.Name}" styleClass="form-control"/>
                                <!--Trying to change this value but is never updated-->
                            </div>
                        </div>
                    </div>
                </div>
            </apex:form>
        </div>
        <div class="form-actions">
            <apex:form>
                <div class="row">
                    <div class="col-md-6">
                        <div class="row">
                            <div class="col-md-offset-3 col-md-9">
                                <apex:commandButton value="Guardar" action="{!updateRecord}" styleClass="btn green"/>
                                <a href="/apex/CustomerPortalAccounts?Id={!account.Id}" class="btn default" role="button">Cancelar</a>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6"> </div>
                </div>
            </apex:form>
        </div>
    </form>
</div>

And this is the updateRecord method in my controller:

public class CustomerPortalController {

    static final String URL_PARAM_ACCOUNT = 'Id';
    static final String URL_PARAM_CONTACT = 'contactId';
    static final String URL_PARAM_OPPORTUNITY = 'opportunityId';
    static final String URL_PARAM_SYSTEM = 'systemId';
    static final String URL_PARAM_EDIT = 'edit';

    public Account account{get;set;}
    public Contact contact{get;set;}
    public Opportunity opportunity{get;set;}
    public Sistemas__c sistem {get;set;}

    public String showAccountDetails{get;set;}
    public String showAccountList{get;set;}
    public String showAccountEdit{get;set;}

    public String accountId{get;set;}
    public String contactId{get;set;}
    public String opportunityId{get;set;}

    public String page{get;set;}

    public String accountName{get;set;}

    public List<Sistemas__c> sistemas{get;set;}

    public CustomerPortalController() {

        accountId     = ApexPages.currentPage().getParameters().get(URL_PARAM_ACCOUNT);
        contactId     = ApexPages.currentPage().getParameters().get(URL_PARAM_CONTACT);
        opportunityId = ApexPages.currentPage().getParameters().get(URL_PARAM_OPPORTUNITY);

        if(accountId != null){
            this.account = [Select Id,Name,Externo_temp__c,Phone,BillingCountry,BillingPostalCode,BillingState,BillingCity,Colonia__c,BillingStreet,Industry from Account where Id=:accountId];

            ParamValidation validationAccountId =  validatePageParam(URL_PARAM_ACCOUNT);
            this.showAccountDetails             = validationAccountId.showDetails;
            this.showAccountList                = validationAccountId.showList;
            this.showAccountEdit                = validationAccountId.showEdit;
        }
        else if(contactId != null){
            this.contact = [Select Id,Name,Title, Email from Contact where Id=:contactId];

            ParamValidation validationAccountId =  validatePageParam(URL_PARAM_CONTACT);
            this.showAccountDetails             = validationAccountId.showDetails;
            this.showAccountList                = validationAccountId.showList;
            this.showAccountEdit                = validationAccountId.showEdit;
        }
        else if(opportunityId != null){
            this.opportunity = [Select Id,Name,LeadSource,Amount,StageName,CloseDate from Opportunity where Id=:opportunityId];
            this.sistemas = [Select Id, Name, GM_TOTAL__c, MG_MEX__c, MG_US__c, Area_FT2__c, Bonds__c, Clase__c,
                             Cost_Hr_Eng__c,Cost_Hr_Installation__c,Cost_Hr_Supervision__c,Costo_MEX__c,
                             Costo_Total__c,Costo_USA__c,Cotizacion__c,Days__c,Engineering__c,Engineering_Trave__c,
                             Equipment__c,Equipo_Mayor__c,Fabrication__c,Freight__c,Import_Duties__c,Inst_Travel__c,
                             Installation__c,Installation_Cost__c,Local_Material__c,Material__c,Obra_Civil__c,
                             Oportunidad__c,Otros__c,Paint__c,Partida__c,Per_Team__c,Precio_Mex__c,Precio_Total__c,
                             Precio_USA__c,Se_Cotiza__c,Sistema__c,Sub_Contrato__c,Supervision__c,Supervision_Cost__c,
                             Tipo_de_Cambio__c,Total__c,Unidad__c,Union__c,Vendedor__c,Version__c
                             from Sistemas__c where Oportunidad__r.Id=:opportunityId];


            ParamValidation validationAccountId =  validatePageParam(URL_PARAM_OPPORTUNITY);
            this.showAccountDetails             = validationAccountId.showDetails;
            this.showAccountList                = validationAccountId.showList;
            this.showAccountEdit                = validationAccountId.showEdit;
        }
        else{
            this.showAccountDetails             = 'none';
            this.showAccountEdit                = 'none';
            this.showAccountList                = '';
        }
    }

    public List<Account> getAccounts(){

        List<Account> accountsAvailable = [Select Id,Name,CreatedDate,Externo_temp__c,Phone from Account];
        return accountsAvailable;
    }

    public List<Contact> getContacts(){
        List<Contact> contact_list;
        if(accountId!=null){
            contact_list = [Select Id,Name,FirstName,LastName,Email,CreatedDate,Title from Contact where AccountId = :accountId];
        }
        else{
            contact_list = [Select Id,Name,FirstName,LastName,Email,CreatedDate,Title from Contact];
        }

        if(contact_list.size()>0){
            return contact_list;
        }
        else{
            return null;
        }
    }

    public List<Opportunity> getOpportunities(){
        List<Opportunity> opportunity_list = [Select Id,Name,CreatedDate,CloseDate,StageName,Amount,LeadSource from Opportunity where AccountId = :accountId];
        if(opportunity_list.size()>0){
            return opportunity_list;
        }
        else{
            return null;
        }
    }

    public List<SelectOption> getAccountIndustry() {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Schema.Account.Industry.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

        for( Schema.PicklistEntry f : ple) {
            options.add(new SelectOption(f.getLabel(), f.getValue()));
        }
        return options;
    }

    public List<SelectOption> getBillingStates() {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Schema.Account.BillingState.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

        for( Schema.PicklistEntry f : ple) {
            options.add(new SelectOption(f.getLabel(), f.getValue()));
        }
        return options;
    }

    public List<SelectOption> getBillingCountries() {
        List<SelectOption> options = new List<SelectOption>();
        Schema.DescribeFieldResult fieldResult = Schema.Account.BillingCountry.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

        for( Schema.PicklistEntry f : ple) {
            options.add(new SelectOption(f.getLabel(), f.getValue()));
        }
        return options;
    }

    public ParamValidation validatePageParam(String paramName){
        String result = '';
        ParamValidation validation = new ParamValidation();

        if (ApexPages.currentPage().getParameters().get(paramName) != null) {
            validation.showList = 'none';

            String editMode = ApexPages.currentPage().getParameters().get(URL_PARAM_EDIT);
            if (editMode != null) {
                validation.showDetails = editMode == 'true' ? 'none' : '';
                validation.showEdit = editMode == 'true' ? '' : 'none';
            } else {
                validation.showDetails = '';
                validation.showEdit = 'none';
            }
        } else {
            validation.showList = '';
            validation.showDetails = 'none';
            validation.showEdit = 'none';
        }
        return validation;
    }

    public class ParamValidation{
        public String showList{get;set;}
        public String showDetails{get;set;}
        public String showEdit{get;set;}
    }

    public PageReference editRecord() {
        String editUrl = '';
        if (accountId != null) {
            editUrl = 'CustomerPortalAccounts?Id=' + accountId + '&';
        } else if (opportunityId != null) {
            editUrl = 'CustomerPortalOpportunities?opportunityId=' + opportunityId + '&';
        } else if (contactId != null) {
            editUrl = 'CustomerPortalContacts?contactId=' + contactId + '&';
        }
        PageReference editPage = new PageReference('/apex/' +  editUrl + URL_PARAM_EDIT + '=true');
        editPage.setRedirect(true);
        return editPage;
    }

    public void updateRecord() {
        try {
            System.debug('About to update');
            System.debug('Simple var: ' + accountName);
            System.debug(account);
            update account;
        } catch (DmlException e) {
            System.debug('Fail!!! ' + e.getMessage());
            ApexPages.addMessages(e);
        }
        /*PageReference detailPage = new PageReference('/apex/CustomerPortalAccounts?Id=' + accountId);
        detailPage.setRedirect(true);
        return detailPage;*/
    }
}
7
  • what are the values not getting updated. can you be more precise. Put a debug for each individual values and trace that Jul 7, 2017 at 21:24
  • @SantanuBoral I've shortened the code to make it more clear. Just now I'm trying to update the account name, but when updateRecord get's called, no matter what I type on my inputField the account name remains the same. Jul 7, 2017 at 21:25
  • I think that is lookup field of Account, check my ans Jul 7, 2017 at 21:28
  • @CarlosJiménez - You are most likely getting a DML exception. Do you have a page messages component on your VF page? If not add it so you can see the message. You can also place a debug in the catch block and a return null in the catch block so you are not redirected in the case of an error
    – Eric
    Jul 7, 2017 at 22:13
  • @Eric, I do have a page messages tag on my page and I am logging the exceptions but I am not getting one. What could it be? Jul 7, 2017 at 22:17

2 Answers 2

4

You have multiple forms on your page. You button is in a separate form so when you submit it does not grab the value from the input fields in the other form.

Wrap you entire code in the form as follows:

<div class="portlet-body">
    <apex:form styleClass="form-horizontal" html-role="form">
        <div class="form-body">
            <img src="https://allizancespecializedsytems--emorandev.cs2.my.salesforce.com/img/social/unknown_company_pic.png"/>
            <h2 class="margin-bottom-20">{!Account.Name} </h2>
                <div class="row">
                    <!--/span-->
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label col-md-3">Nombre:</label>
                            <div class="col-md-9">
                                <apex:inputField value="{!account.Name}" styleClass="form-control"/>
                                <!--Trying to change this value but is never updated-->
                            </div>
                        </div>
                    </div>
                </div>
        </div>
        <div class="form-actions">
                <div class="row">
                    <div class="col-md-6">
                        <div class="row">
                            <div class="col-md-offset-3 col-md-9">
                                <apex:commandButton value="Guardar" action="{!updateRecord}" styleClass="btn green"/>
                                <a href="/apex/CustomerPortalAccounts?Id={!account.Id}" class="btn default" role="button">Cancelar</a>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6"> </div>
                </div>
        </div>
    </apex:form>
</div>

Note: I also removed your html form tag. Not sure of the top of my head if the apex:form allows html passthrough attributes.

2
  • Thanks Eric. That was the problem! the html tag seems to be working fine. Jul 7, 2017 at 23:35
  • @Eric, I have just overlooked the apex:form which you have pointed out correctly Jul 8, 2017 at 17:37
0

Since you are using apex:inputField to display Account as lookup then use the Id instead of Name.

<apex:inputField value="{!account.Id}"/>

If you use apex:inputtext then use "{!account.Name}" to display the account name.

10
  • Thanks for the answer. I tried using apex:inputText but the account name is not changed either. Jul 7, 2017 at 21:34
  • what type of user is changing this name? portal user? Jul 7, 2017 at 21:37
  • right now I'm testing as an administrator Jul 7, 2017 at 21:41
  • is that other values are saving? Jul 7, 2017 at 21:43
  • None of the values are saving Jul 7, 2017 at 21:47

You must log in to answer this question.

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