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
p1errep1erre 

VF:field validation before to create record

I would like to create a new ACCOUNT by first fetching the ACCOUNT details from an external data-source like a database
or a web service, then assign the details to the salesforce ACCOUNT fields and display these in the browser before
the user eventually press the SAVE button to create the account => the user should first verify if the datails are ok before to create the account.
I thougth about using the standardController Account with an extension controller for calling the external data source,
but i haven't managed to find out how to display/assign the details from my external data source to the standard salesforce ACCOUNT fields since the ACCOUNT is not yet created


jwetzlerjwetzler
An extension is exactly what you want to do.  The standardController will havea record to give you.  That record will just be empty, and will not be assigned an id until you successfully save.  So all you need to do is give your extension that record right in the constructor, and then fill out the necessary fields.

Code:
public class accountext {
    Account acc;


    public accountext(ApexPages.StandardController controller) {
        acc = (Account) controller.getRecord();
        acc.name = 'Account Name';
        //you can set your fields here
    }

}

 
Code:
<apex:page standardController="account" extensions="accountext">
  <apex:outputField value="{!account.name}"/>
</apex:page>

 
This page and controller correctly show 'Account Name' as the value for my outputField.

p1errep1erre
Thanks for your reply and your code sample

I've tried it out and it seems ok.
Now I would like to add a SAVE button in the apex page to save this accout and switch to the standard account page.

something like this:
      <apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
how can I reference the account (id) from the controller ?

Code:
public class accountext {
Account acc;


public accountext(ApexPages.StandardController controller) {
acc = (Account) controller.getRecord();
acc.name = 'Account Name';
//you can set your fields here
}

}

 
Code:
<apex:page standardController="account" extensions="accountext">
<apex:outputField value="{!account.name}"/>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
</apex:page>

jwetzlerjwetzler
Did you try the code you currently pasted there?  It should work.

You don't need to write your own save method. Because you're using an extension you can use everything in the standard controller that has not been overridden by your extension.  It will automatically redirect you to the detail page for your new component.

If you want it to do something different you can override your save action in your extension.  You will want to save off the controller passed in to your constructor in a local variable and call controller.save(), then do whatever other logic you wanted to do.

As far as getting the id, the syntax is controller.getId(), but in your case with creating a new record, your account is not going to have an id until after you've saved it.



p1errep1erre
yes, the code should work but somehow it does not :smileysad:

In my sample page I first fetch the account data defined in my controller.
After pressing the "Get fields" button the account fields are displayed on the page.
But when I then try to create the account by pressing the "save" button
no account is create

here is my sample page/controller

Code:
public class accountNewController {

public Account acc;

public accountNewController(ApexPages.StandardController controller) {

this.acc = (Account)controller.getRecord();
}

public Account getAccount() {
if(acc == null) acc = new Account();
return acc;
}


public void getAccountParam() {
acc.name = 'NewAccount';
acc.BillingCity = 'NewCity';
acc.BillingStreet = 'NewStreet 6';
acc.Phone = '0311234567';
}

}

 


Code:
<apex:page standardController="account" extensions="accountNewController">

<apex:sectionHeader title="Proto" />
<apex:form>
<apex:pageBlock title="Account lookup" mode="edit">
<apex:pageBlockSection >
<apex:commandButton action="{!getAccountParam}"value="Get fields"/><p/>
</apex:pageBlockSection>
</apex:pageBlock>

<apex:pageBlock title="Create a New Account">

<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Account Information">
<apex:outputField id="accountId" value="{!account.id}"/>
<apex:outputField id="accountName" value="{!account.name}"/>
<apex:outputField id="accountBillingCity" value="{!account.BillingCity}"/>
<apex:outputField id="accountBillingStreet" value="{!account.BillingStreet}"/>
<apex:outputField id="accountPhone" value="{!account.Phone}"/>
</apex:pageBlockSection>
<apex:detail subject="{!account}" relatedList="false"/>
</apex:pageBlock>
</apex:form>
<apex:detail/>
</apex:page>

 
Any idea what could be wrong in my code?

jwetzlerjwetzler
You don't need that getAccount function.  The standardController takes care of getting the record and creating a new one for you.  I think you've confused it by instantiating a new one.

Code:
public class accountNewController {

public Account acc;

public accountNewController(ApexPages.StandardController controller) {
  this.acc = (Account)controller.getRecord();
}

public void getAccountParam() {
  acc.name = 'NewAccount';
  acc.BillingCity = 'NewCity';
  acc.BillingStreet = 'NewStreet 6';
  acc.Phone = '0311234567';
}
}

 

Does that work?
jwetzlerjwetzler
You should also take advantage of our partial page updates by just rerendering the portion of the page that you need:
Code:
<apex:pageBlockSection >
<apex:commandButton action="{!getAccountParam}"value="Get fields" rerender="block"/><p/>
</apex:pageBlockSection>
</apex:pageBlock>

<apex:pageBlock title="Create a New Account" id="block">

 

p1errep1erre
Great! now it works fine

Thanks very much for your help and advices.