3

I'm trying to build a VF page to simplify the process of creating an opportunity. Everything works fine, but for some reason I cannot get the Account ID/Account to populate correctly, even though the Contact works just fine. Instead of populating the field with the Account, it puts in the AccountID string into a lookup box, which of course returns 0 results.

VF Page

As you can see, I'm able to pull the AccountID in correctly, but it is not populating the Account correctly.

Here is my VF page:

<apex:page standardController="Opportunity" extensions="RouteSalesOppExtension" title="Route Sales - Opportunity">
    <apex:form >
        <apex:pageBlock title="New Opportunity Information">
            <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!save}" value="Save"/>
            <apex:commandButton action="{!cancel}"
        value="Cancel"/>
            </apex:pageBlockButtons>
         <apex:pageBlockSection columns="1">
        <apex:inputField value="{!Opportunity.Name}" 
        label="Opportunity Name"/>
        <apex:inputField value="{!Opportunity.CloseDate}" 
        label="Close Date"/>  
       <apex:inputField value="{!Opportunity.StageName}"
        label="Stage"/>
        <apex:inputHidden value="{!Opportunity.PrimaryContact__c}"
        id="contact"/>
        <apex:inputField value="{!Opportunity.AccountId}"
        id="accountid"/>
        <script>
            var jsName = "{!$Component.contact}";
            var jsName2 = "{!$Component.accountid}";
        </script>
        </apex:pageBlockSection>  
        </apex:pageBlock>
    </apex:form>
     <script>
        window.onload=function()
        {

         document.getElementById(jsName).value = "{!$CurrentPage.parameters.contID}";
         document.getElementById(jsName2).value = "{!$CurrentPage.parameters.acctID}";
        };
    </script>
</apex:page>

And here is the original VF page that directs them to the above page when they click the New Opportunity button:

<apex:page standardController="Task" extensions="RouteSalesExtension" title="Begin Route Sales">
    <apex:form >
        <apex:pageBlock title="Route Sales Information">
            <div align="center">
            <apex:pageBlockButtons location="top">
                <apex:commandButton action="{!SetStartTime}" value="Start Meeting" rerender="true"/>
                <apex:commandButton action="{!SetFinishTime}" value="End Meeting"/>
            </apex:pageBlockButtons>
            </div>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!URLFOR($Page.RouteSalesOpp1,$ObjectType.Opportunity,[retURL=$Currentpage.URL, contID=task1.Who.Id, acctID=task1.AccountID])}" 
                value="New Opportunity"/>
                <apex:commandButton action="{!URLFOR($Page.RouteSalesTask1,$ObjectType.Task,[retURL=$Currentpage.URL, contID=task1.Who.Id])}" 
                value="New Follow-Up Meeting"/>
                <apex:commandButton action="{!CreateOpportunityforOrder}" value="Create Order"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
        <apex:inputField id="Comments" value="{!Task.Description}" 
        label="Meeting Notes"/>
        <apex:outputField value="{!task1.Who.Id}" rendered="false"/>
        <apex:outputField value="{!task1.What.Id}" rendered="false"/>
        <apex:outputField value="{!task1.Id}" rendered="false"/>
        <apex:outputField value="{!task1.AccountId}" rendered="true" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

I have some things rendered just so I could ensure I was passing Account ID correctly, which I am. On the original VF page, it shows the actual account name just fine, but I'm not sure why it's having such a hard time populating the account correctly when it gets to the "New Opportunity Information" page!

2

Your JavaScript is quite simply copying the Account Id from the URL and inserting it into the Account Name input box. You can actually pre-populate fields on the VisualForce page without needing JavaScript, by passing the ID of the input field and associating a value in the URL. Try this for your New Opportunity button:

<apex:commandButton action="{!URLFOR($Page.RouteSalesOpp1,$ObjectType.Opportunity,[retURL=$Currentpage.URL, contID=task1.Who.Id, opp4=URLENCODE(task1.Account.Name), opp4_lkid=task1.AccountId])}" 
                value="New Opportunity"/>

opp4 should be the ID of the Account Name input field on your New Opportunity VF page. You can validate this by right clicking the input field -> Inspect Element, and checking the ID (if in Chrome).

When you pre-populate lookup fields, you need to populate both the name and the ID. Which is why: opp4 = task.Account.Name
opp4_lkid = task.AccountId

Are both set.

EDIT: If you want to keep your field hidden, you can populate using JavaScript, however there are 2 fields you need to populate:

 document.getElementById('opp4').value = "{!$CurrentPage.parameters.opp4}";
 document.getElementById('opp4_lkid').value = "{!$CurrentPage.parameters.opp4_lkid}";
| improve this answer | |
  • Thank you, bnedwards. This has me closer to a solution. When I try this, the field populates with the Account Name, however there are + signs between each word in the account name, which causes the lookup field to be unable to find any results. – grimsvotn Nov 10 '14 at 16:04
  • I have managed to get it to somewhat work by removing the URLENCODE() part of the parameter, so that it only has opp4=task1.Account.Name. This populates the lookup field without any +'s, however making the field hidden makes you unable to save. I will just leave the field visible so that everything works somewhat. Thank you! – grimsvotn Nov 10 '14 at 17:31
  • Ah yep, remove the URLENCODE(). It must do the encoding and decoding itself without requiring it. If you want the field hidden, then you will need to use JavaScript to populate the field or do it in your controller. Doing it in a controller would be the most robust step, but has the overheads of creating a controller and test class. For JavaScript, you are pretty close, you just need to populate two fields rather than just the one. For inputFields that are lookups, the VisualForce will render two inputs. I have updated the original answer with new JavaScript. – benedwards44 Nov 11 '14 at 9:05
2

Could you try with below change

 <apex:inputField value="{!Opportunity.Account}"
    id="accountid"/>

Looks like code uses AccountId instead of Account lookup.

| improve this answer | |
  • Thank you, I have tried this, however when I click the New Opportunity button, it says that Opportunity.Account does not resolve to a field. – grimsvotn Nov 10 '14 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.