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
Jason DunphyJason Dunphy 

Visualforce - New Case Page, Related Contact Input Dependent on Account Input field

Hoping someone might be able to help me out or at least streamline my research and point me to the right direction as I’m not really making quick progress.  I'm just learning apex and visualforce, looked around this forum and the docs, and just can't seem to find an example that matches my scenario.

I have a visual forcepage that will pop from the CTI and create a NEW case.  All I’m looking to do is make the contact input field dependent on the account input field that the user selects and then submit those to a new case.  This will be used when a contact or account match can't be found from the ANI passed in by the CTI.

The bare bones:
<apex:page sidebar="true" StandardController="Case" extensions="sampleExtension">
    <apex:form >
        <apex:pageBlock title="Create A New Case">
            <apex:pageBlockSection title="My Case" columns="2">
                
         <apex:inputField value="{!Case.AccountId}"
         <apex:inputField value="{Case:ContactId}"
         <apex:inputField value="{!Case.Description}"/>              
            </apex:pageBlockSection>
        </apex:pageBlock>  
<apex:commandButton action="{!save}" value="Save" id="theButton"/>
           
    </apex:form>



What is the best way to do this, SOQL in an extension?  I’ve pieced together some code and sample which returns some related contact vales of related contacts, but I'm not thrilled about the UX and it seems to also be breaking my save command.  I really just want it to be another pick list style input field.

Any help or guidance would be greatly appreciated.

Thanks
 
Rohit B ☁Rohit B ☁
Hey Jason,
I guess if you apply lookup filter on the Case.Contact will help you out.
Just try to edit you contact field on the Case and create a lookup filter. Please refer the below screenshot for better understanding.

Lookup Filter Example

Please tell me if it doesn't help you.
If it does say Cheers :)
Jason DunphyJason Dunphy
Hi Rohit,

Thank you for the suggestion.  I was also hopping the filter on the lookup field would solve this.  The problem in my case with that is that it only auto-dispalys recent contacts and users need to search to find hits.  Screenshot below is the results, my hacked code pulls in the related contacts on the bottom left based on account.  And the Contact Name search doesn't show any results.

Recent Only



For more detail, here's what I'm currently using, but don't like as it's not great if many contacts are found and I haven't figured out how to actually save a new case with the contact associated with the check box.
<!-- Visualforce Page  -->

<apex:page sidebar="true" StandardController="Case" extensions="contactsForServiceCloud">
    <apex:form >
     <apex:facet name="header">Titles can be overriden with facets</apex:facet>
        <apex:pageBlock title="Create A New Case">
            <apex:pageBlockSection title="My Case" columns="2">
             <apex:outputLabel value="Account Name"> &nbsp;&nbsp;
                
         <apex:inputField value="{!Case.AccountId}" required="false" id="lid">
         <apex:actionsupport event="onchange" rerender="check"/>
         </apex:inputField>
                
         </apex:outputLabel>
               <apex:inputField value="{!Case.ContactId}" />
                <apex:inputField value="{!Case.Description}"/>              
            </apex:pageBlockSection>
        </apex:pageBlock>
               <apex:outputPanel id="check">
         <apex:outputLabel value="Contacts:" rendered="{!Case.AccountId!=null}">
         <apex:selectCheckboxes value="{!selectedAccount}">
            <apex:selectOptions value="{!items}"/>
            </apex:selectCheckboxes><br/>
        </apex:outputLabel>
        </apex:outputPanel>   
<apex:commandButton action="{!save}" value="Save" id="theButton"/>


           
    </apex:form>

</apex:page>
And the Apex Class.
<!-- Controller Extension -->

public class contactsForServiceCloud
{
public case selectedAccount{get;set;}

 public contactsForServiceCloud(ApexPages.StandardController stdCtrl) {
selectedAccount=(case)stdCtrl.getRecord();
    }

     public List<SelectOption> getItems()
      {
   
      List<Contact> cntList=[select Name from Contact where AccountId=:selectedAccount.AccountID];
      List<SelectOption> options = new List<SelectOption>();
        
        for (Contact c:cntList) 
        {
            options.add(new SelectOption(c.Name,c.Name));
        }
        return options;
      }
      

}



Thanks,

Jason
Rohit B ☁Rohit B ☁
Hi Jason,
"The problem in my case with that is that it only auto-dispalys recent contacts and users need to search to find hits."
This is not a problem, this is the expected behaviour of salesforce.

And may be you have not configured your filter properly so you are not able to see the contacts otherwise it would have been working.
Could you please post a screenshot of your lookup filter?
Jason DunphyJason Dunphy
Hi Rohit,

The search does return only contacts related to the account selected when the search is executed, but only shows recent contact (based on Accout ID) when they first open the search page.  What I was hoping to do was have the contact input field show related contacts before user executes the search.

User-added image