Need portal widget that looks up a User record from a field on the logged in user's Company record

JR42
Giga Guru

Hi, I am attempting to create a widget on a portal to show the contact details for the primary support contact for the logged in user's Company.  The primary support contact is listed on the Company record in a field called u_csm_primary. It is a Reference field that references the sys_user table.

 

I need the widget to display details from the the user record listed in the u_csm_primary field, including their name, email, and phone.  I've been unsuccessful at gathering the details (name, email, phone) from the user record, and printing them out in the widget. 

I created a widget with the details below, but the widget is displaying "Primary user details not found." where it should be listing the primary support contact's name, email and phone.

 

 

Server Script:

(function() {
    var user = gs.getUser();
    var userCompany = user.getCompanyID();
    var result = {};

    if (userCompany) {
        var companyRecord = new GlideRecord('core_company');
        if (companyRecord.get(userCompany)) {
            result.companyName = companyRecord.getValue('name');
            
            var primaryUser = companyRecord.getValue('u_csm_primary');
            if (primaryUser) {
                var userRecord = new GlideRecord('sys_user');
                if (userRecord.get(primaryUser)) {
                    result.primaryUserName = userRecord.getValue('name');
                    result.primaryUserPhone = userRecord.getValue('phone');
                    result.primaryUserEmail = userRecord.getValue('email');
                }
            }
        }
    }

    data.result = result;
})();

 

Client Script:

function($scope) {
    $scope.companyDetails = $scope.data.result;
}

 

HTML:

<div>
    <h1>{{::companyDetails.companyName}}</h1>
    <div ng-if="companyDetails.primaryUserName">
        <p>Primary User: {{::companyDetails.primaryUserName}}</p>
        <p>Phone: {{::companyDetails.primaryUserPhone}}</p>
        <p>Email: {{::companyDetails.primaryUserEmail}}</p>
    </div>
    <div ng-if="!companyDetails.primaryUserName">
        <p>Primary user details not found.</p> <!-- Debug info -->
    </div>
</div>

 

 

Thanks for taking the time to help!  I have tried many variations and ran into the same issue with all of them.

1 ACCEPTED SOLUTION

JR42
Giga Guru

I was able to get this working by including userRecord.queryNoDomain(); in the script. 

View solution in original post

3 REPLIES 3

Anirudh Pathak
Mega Sage

Hi @JR42 ,

Please update you Server side code as below -

(function() {
	var comp = '';
    var user = gs.getUserID();
	var userCompany = new GlideRecord('sys_user');
	if(userCompany.get(user)) {
		comp = userCompany.company;
	}
    var result = {};
    if (comp) {
        var companyRecord = new GlideRecord('core_company');
        if (companyRecord.get(comp)) {
            result.companyName = companyRecord.getValue('name');
            
            var primaryUser = companyRecord.getValue('u_csm_primary');
            if (primaryUser) {
                var userRecord = new GlideRecord('sys_user');
                if (userRecord.get(primaryUser)) {
                    result.primaryUserName = userRecord.getValue('name');
                    result.primaryUserPhone = userRecord.getValue('phone');
                    result.primaryUserEmail = userRecord.getValue('email');
                }
            }
        }
    }

    data.result = result;
})()

Hi Anirudh, thanks for your response.  I updated the server script but that did not resolve the problem.  The only thing returned in the widget is the company name and "Primary user details not found."

JR42
Giga Guru

I was able to get this working by including userRecord.queryNoDomain(); in the script.