+ Start a Discussion
MrBungleMrBungle 

How can I get access to the account name of a customer portal user?

This is really frustrating!

 

I have setup Live Agent so that the button to initiate the pre-chat dialog, which is a visualforce page, is in our Customer Portal. We only want to give customer portal users access to Live Agent Chat. I need to retrieve the user’s name, email and account name so that the rep can have this referenced in the Live Agent Console chat. I have no problems getting the user’s name and email address using the typical declarative method:

 

<input type='text' size="40" name='liveagent.prechat:Name' id='prechat_field' value='{!$User.FirstName} {!$User.LastName}' /> 

 

And

 

<input type='text' size="40" name='liveagent.prechat:Email' width="500" value='{!$User.Email}' />

 

But I cannot, for the life of me, figure out what tag value to use to get the customer portal user’s account name. AccountId is a field on the User object that is populated for all active customer portal users. It seems access to this is restricted? I don’t now but it very frustrating none the less.

 

I have tried the following and they all do not pull the account name:

{!User.AccountId}

{!User.Account}

{!User.Account.Name}

{!User.Contact.Account}

{!User.Contact.Account.Name}

 

OK, So I figured that it’s time to create an extension class and strong arm the account name into the visualforce page using a SOQL query! Guess what? I can’t get the account name this way either.

 

PLEASE HELP!

 

 

Visualforce page syntax:

<apex:page showHeader="false" standardController="User" extensions="NICP_Functions">

    <!-- This script takes the endpoint URL parameter passed from the deployment page and makes it the action for the form -->
    <script type="text/javascript">
        (function() 
        { 
            function handlePageLoad() 
            {
                var endpointMatcher = new RegExp("[\\?\\&]endpoint=([^&#]*)");
                document.getElementById('prechatForm').setAttribute('action', decodeURIComponent(endpointMatcher.exec(document.location.search)[1]));
            } 
            if (window.addEventListener) 
            {
                window.addEventListener('load', handlePageLoad, false);
            } 
            else 
            { 
                window.attachEvent('onload', handlePageLoad, false);
            }
        })();
    </script>
    
    <apex:image url="{!$Resource.NI_CustPortal_Logo}" /><br /> 
    <h1>Pre-chat Form</h1>
    <form method='post' id='prechatForm'>
    
        <style type="text/css">
            body {
                margin: 0 auto;
                padding: 0;
                font-family: 'Trebuchet MS', Helvetica, sans-serif;
                font-size: 12px;
                color: #737373;
                line-height: 16px;
            }                    

            p {
                font-weight: bolder
            }
            
            td {
                font-family: 'Trebuchet MS', Helvetica, sans-serif;
            }
            
            h1 {
                font-family: 'Trebuchet MS', Helvetica, sans-serif; 
            }

            .btnClass {
                font-family: 'Trebuchet MS', Helvetica, sans-serif; 
            }

        </style>

        <!-- Creates an auto-query for a matching Contact record’s Email field based on the value of the liveagent.prechat:Email field -->    
        <table border="0" width="400px">
            <tr>
                <td>
                    <b>Name: </b>
                </td>
                <td>
                    <input type='text' size="40" name='liveagent.prechat:Name' id='prechat_field' value='{!$User.FirstName} {!$User.LastName}' />          
                </td>
            </tr>
            <tr>
                <td>
                    <b>Email Address: </b>
                </td>
                <td>
                    <input type='text' size="40" name='liveagent.prechat:Email' width="500" value='{!$User.Email}' />
                </td>
            </tr>
            <tr>
                <td>
                    <b>Property Name: </b>
                </td>
                <td>
                    <input type='text' size="40" name='liveagent.prechat:Account' value='{!User.Contact.Account.Name}' />
 </td> </tr> <tr> <td> </td> <td align="center"> <input type='submit' value='Request Chat' id='prechat_submit' class="btnClass" /> </td> </tr> </table> <input type="hidden" name="liveagent.prechat.query:Email" value="Contact,Contact.Email" /> </form> </apex:page>

 

extension class code:

public with sharing class NICP_Functions 
{

    public String getAccountName{get; set;} 
    private User u;
    
    public NICP_Functions(ApexPages.StandardController controller) 
    {
        this.u = (User)controller.getRecord(); 
        User u2 = [SELECT Id, AccountId FROM User WHERE Id =: u.Id];
        Account a = [SELECT Id, Name FROM Account WHERE Id =: u.AccountId];
        getAccountName = a.Name;
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
MrBungleMrBungle

I figured it out. A little balder but very relieved!

 

Need to use UserInfo.getUserId() in the class to query for the account name.

 

public with sharing class NICP_Functions 
{

    public String getAccountName{get; set;} 
    public User u;
    
    public NICP_Functions(ApexPages.StandardController controller) 
    {
        User u = [SELECT Id, AccountId FROM User WHERE Id =: UserInfo.getUserId()];
        Account a = [SELECT Id, Name FROM Account WHERE Id =: u.AccountId];
        getAccountName = a.Name;
    }
}