Display Logged in User Name in "Rich Text label" Html variable on Portal

Fysl
Kilo Explorer

Hello Everyone,

I want to display logged in user name on the Catalog item, saying "Welcome Username". I want to use HTML to achieve this.

So, I have created a Rich Text Variable on the catalog item and in its source code i tried below codes but none worked.

Please can anyone help me achieve this.

find_real_file.png

find_real_file.png

Regards,

 

4 REPLIES 4

Michael Jones -
Giga Sage

Unfortunately the Rich Text Label and other HTML fields do not permit scripting and you can't dynamically set the content (setValue is not supported). 

https://docs.servicenow.com/bundle/paris-servicenow-platform/page/product/service-catalog-management...

You could do this with a Macro type variable and Service Portal widget. 

HTML:

<div>
  <p>Welcome {{data.user_name}}</p>
</div>

Server Script

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
data.user_name = gs.getUserDisplayName();
})();

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Thank you Michael, it is interesting.

Would it be possible to have some more detailed instructions for newbies like me, on how to get a text which reads like this in a record producer:

           You identified as Flavio Costa

where "Flavio Costa" is the name of the currently logged in user?

I understand that a Macro type variable should be used in the, with a definition of a Macro and a Widget in the Type Specifications tab.

What exactly should be entered in the XML field of the Macro and in the 'Body HTML template' field of the Widget or whatever else?

Thanks in advance
Flavio

Certainly; 

Step 1: Create your Widget

Go to Service Portal > Widgets

Click New

Name: Show Name

Check the box for Public

HTML Template: 

<div>
  <p>You identified as {{data.user_name}}</p>
</div>

CSS:

Nothing needed here specifically but you could define styles here. 

Server script: 

(function() {
	
    //Create the data object and push the display name of the logged in user to data.user_name
    data.user_name = gs.getUserDisplayName();

})();

Submit. 

 

Step 2: Add a variable to your Catalog Item

Open your catalog item

Under Variables click New

Select a Type of Macro (Paris or earlier) or Custom (Rome or later)

Question / Name: Greeting

Order: As you like

Under Type Specifications / Widget: Select the widget you created (Show Name)

Submit. 

 

Load your catalog item in the Portal: 

find_real_file.png

I hope this helps!

If this was helpful, or correct, please be kind and mark the answer appropriately.

Michael Jones - Proud member of the GlideFast Consulting Team!

 

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Thanks! In my previous attempt I was not setting the function in the right place (Server script).

Hope I marked the answer appropriately.