Get accountName or Username

Is there a way to get the User that logs in using a script and displaying the username in a textbox?

//Get Name

var name = dundas.context.currentSession.accountName;

//Get display Name

var name = dundas.context.currentSession.accountDisplayName;

//Put into a label

label1.labelText = name;

2 Likes

@david.glickman but how can I take the group name in dashboard?
Thank you!

That starts getting more complicated, as a single user can be in many groups.

The following script will output all the group names for the logged in user to the console. You will need to adapt as necessary.

var groupService = dundas.context.getService("GroupService");

groupService.getGroupMembershipForAccount(dundas.context.currentSession.accountId).done(function (groups)
{
	groups.forEach(function (group)
	{
		console.log(group.displayName)
	}
	)
});
2 Likes

This code is correct, however it should be noted that it requires Administration privileges to run. So if you put this script on a dashboard (for example) where regular viewers are using it, it will fail for them.

The groupMembershipIds are in the currentSession object, and you can use getGroupInfosByIds as any user so I’d recommend something like:

groupService.getGroupInfosByIds(window.dundas.context.currentSession.groupMembershipIds).done(function (infos) {
    console.log($E(infos).select(function (a) { return a.displayName; }).toArray());
});
2 Likes

Thank you both for your help @terrence and @david.glickman!

I have a similar request (#67222) for a feature about getting the Project name and being able to display it on a dashboard.

I do understand it can be scripted, but I would think there would be a way to drag and drop values like “current User” or “current project name” and be able to drop it on a dashboard or report, much like we can do with “current day” on a report. It’s all stored in a db so it sure would be nice to have the ability to add this with a simple drag and drop instead of having to script it out.

Hi Joyce,

I’ve added this as a feature request issue #80423.

Please note our R&D team evaluates feature requests by the severity of the issue, market demand, if there is an acceptable alternative solution, time and effort required and overall fit within the product architecture and road-map.