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
sharrissharris 

How can I reference the value in an apex:inputField component on an unsaved record?

 Here is my goal: I have a custom page for new cases. I have a custom object that contains "heads up" information on accounts called a flash. As soon as a user selects an account via the lookup icon I need to throw a modal popup that lists the messages for the selected account. I am really close to getting this to work but I need to pass the selected account name to my controller. How can I get this value? I'm thinking by id name on the apex:inputField component but I don't know how.

 

Here's the section of code:

 

<apex:pageBlockSectionItem >
    <apex:outputLabel value="* Account Name" styleClass="RequiredLabelColor"/>
    <apex:inputField id="txtAccountName" value="{!Case.AccountId}">
        <apex:actionSupport event="onchange" action="{!showFlash}" rerender="FlashMessage">
            <apex:param name="AcctNameParam" value="{!txtAccountName.value}" assignTo="{!strAccountName}"/>
        </apex:actionSupport>
    </apex:inputField>
</apex:pageBlockSectionItem>

 The value="{!txtAccountName.value}" part is a syntax error but it's how I thought it might have worked. The case record is unsaved at this moment so I cannot use {!Case.AccountId}.

 

Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The way that you are passing Case.AccountId will use the value that was present when the page was originally rendered. 

 

Presumably you are using an extension controller to a Case record?  If so, you don't need to pass any parameters back to the controller, the record that the standard controller is managing will be updated with the selected account id as part of the postback for the actionsupport.

All Answers

bob_buzzardbob_buzzard

As you are using an actionSupport, you should be able to use the Case.AccountId.  By the time your action method "showFlash" is executed, the Case record will be updated with the latest values from the viewstate.  

sharrissharris

Thanks for the reply!

 

I tried adding the Case.AccountId like you recommended.

 

Here's my page code snippet:

                <apex:pageBlockSectionItem id="AccountName">
                    <apex:outputLabel >Account Name</apex:outputLabel>
                    <apex:outputPanel layout="block" styleClass="requiredInput">
                    <apex:outputPanel layout="block" styleClass="requiredBlock"/>
                        <apex:inputField id="txtAccountName" value="{!Case.AccountId}">
                            <apex:actionSupport event="onchange" action="{!showFlash}" rerender="FlashMessage">
                                <apex:param name="AcctNameParam" value="{!Case.AccountId}" assignTo="{!strAccountName}"/>
                            </apex:actionSupport>
                        </apex:inputField>       
                    </apex:outputPanel>
                </apex:pageBlockSectionItem>

I get the following Visualforce Error:

System.QueryException: List has no rows for assignment to SObject

 

Here is my showFlash code snippet:

    public boolean displayFlash {get; set;}
    public string strAccountName {get; set;}
    public Flash_Message__c[] messages {get;set;}
    
    public void closeFlash() 
    {                
        displayFlash = false;
    }
       
    public void showFlash() 
    {                
        displayFlash = true;
        
                
            System.debug('------------ strAccountName Value ------------');
            System.debug(strAccountName);        
        
        messages = new Flash_Message__c[0];
        String accountId;
        
        if (strAccountName != null)
        {
            Account a = [SELECT Id FROM Account WHERE Name = :strAccountName LIMIT 1];
            //accountId = '001E000000CAPTB';
            accountId = a.Id;
            System.debug('------------ accountId Value ------------');
            System.debug(accountId);
            
            //Get the flashmessages for the account
            if (accountId != null) 
            {
                
                
                for (Flash_Message__c[] mess : [Select Id, Account__c, Name, Message__c, Priority__c, Solution__c, Viewable_to_All_Children__c, CreatedDate from Flash_Message__c where Account__c = :accountId]) 
                {
                    messages.addAll(mess);
                }
            }        
        
        }       
        
    }   

 

and here is my debug log:

22.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
01:45:22.032 (32052000)|EXECUTION_STARTED
01:45:22.032 (32147000)|CODE_UNIT_STARTED|[EXTERNAL]|066E00000004QWo|VF: /apex/CaseNewPage
01:45:22.033 (33188000)|VF_DESERIALIZE_VIEWSTATE_BEGIN|066E00000004QWo
01:45:22.057 (57835000)|VF_DESERIALIZE_VIEWSTATE_END
01:45:22.066 (66011000)|CODE_UNIT_STARTED|[EXTERNAL]|01pE0000000Cw2h|CaseNewPageExtension get(messages)
01:45:22.066 (66059000)|SYSTEM_MODE_ENTER|true
01:45:22.066 (66137000)|CODE_UNIT_STARTED|[EXTERNAL]|01pE0000000Cw2h|messages
01:45:22.066 (66174000)|CODE_UNIT_FINISHED|messages
01:45:22.066 (66208000)|CODE_UNIT_FINISHED|CaseNewPageExtension get(messages)
01:45:22.103 (103776000)|CODE_UNIT_STARTED|[EXTERNAL]|01pE0000000Cw2h|CaseNewPageExtension get(displayFlash)
01:45:22.103 (103829000)|SYSTEM_MODE_ENTER|true
01:45:22.103 (103892000)|CODE_UNIT_STARTED|[EXTERNAL]|01pE0000000Cw2h|displayFlash
01:45:22.103 (103931000)|CODE_UNIT_FINISHED|displayFlash
01:45:22.103 (103967000)|CODE_UNIT_FINISHED|CaseNewPageExtension get(displayFlash)
01:45:22.134 (134004000)|CODE_UNIT_STARTED|[EXTERNAL]|CaseNewPageExtension set(strAccountName,)
01:45:22.134 (134051000)|SYSTEM_MODE_ENTER|true
01:45:22.134 (134105000)|CODE_UNIT_STARTED|[EXTERNAL]|CaseNewPageExtension set(strAccountName,)
01:45:22.134 (134166000)|CODE_UNIT_FINISHED|CaseNewPageExtension set(strAccountName,)
01:45:22.134 (134205000)|CODE_UNIT_FINISHED|CaseNewPageExtension set(strAccountName,)
01:45:22.134 (134942000)|CODE_UNIT_STARTED|[EXTERNAL]|01pE0000000Cw2h|CaseNewPageExtension invoke(showFlash)
01:45:22.135 (135040000)|METHOD_ENTRY|[1]|01pE0000000Cw2h|CaseNewPageExtension.CaseNewPageExtension()
01:45:22.135 (135089000)|SYSTEM_MODE_ENTER|false
01:45:22.135 (135127000)|SYSTEM_MODE_EXIT|false
01:45:22.135 (135158000)|METHOD_EXIT|[1]|CaseNewPageExtension
01:45:22.135 (135219000)|SYSTEM_MODE_ENTER|false
01:45:22.135 (135291000)|SYSTEM_METHOD_ENTRY|[18]|System.debug(ANY)
01:45:22.135 (135335000)|USER_DEBUG|[18]|DEBUG|------------ strAccountName Value ------------
01:45:22.135 (135378000)|SYSTEM_METHOD_EXIT|[18]|System.debug(ANY)
01:45:22.135 (135420000)|SYSTEM_METHOD_ENTRY|[19]|System.debug(ANY)
01:45:22.135 (135459000)|USER_DEBUG|[19]|DEBUG|
01:45:22.135 (135497000)|SYSTEM_METHOD_EXIT|[19]|System.debug(ANY)
01:45:22.135 (135664000)|SOQL_EXECUTE_BEGIN|[26]|Aggregations:0|SELECT Id FROM Account WHERE Name = :strAccountName LIMIT 1
01:45:22.140 (140222000)|SOQL_EXECUTE_END|[26]|Rows:0
01:45:22.140 (140332000)|EXCEPTION_THROWN|[26]|System.QueryException: List has no rows for assignment to SObject
01:45:22.140 (140415000)|SYSTEM_MODE_EXIT|false
01:45:22.140 (140462000)|CODE_UNIT_FINISHED|CaseNewPageExtension invoke(showFlash)
01:45:23.268 (361270000)|CUMULATIVE_LIMIT_USAGE
01:45:23.268|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 6 out of 200000
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

01:45:23.268 (361270000)|CUMULATIVE_LIMIT_USAGE_END

01:45:22.361 (361392000)|CODE_UNIT_FINISHED|VF: /apex/CaseNewPage
01:45:22.361 (361425000)|EXECUTION_FINISHED

 

 It appears that the Case.AccountId is null when it reaches the showFlash function. Can you tell me what else I can try? Thank you very much!!!!

 

bob_buzzardbob_buzzard

The way that you are passing Case.AccountId will use the value that was present when the page was originally rendered. 

 

Presumably you are using an extension controller to a Case record?  If so, you don't need to pass any parameters back to the controller, the record that the standard controller is managing will be updated with the selected account id as part of the postback for the actionsupport.

This was selected as the best answer
sharrissharris

Thanks for the tip! I got it to work!