Script returning sys_id instead of value in transform map runscript

Rashmi Rao
Tera Expert

Hi All, 

 

I am trying to retrieve the username and update the value in discovered source user in a transform map runscript using function.  Its returning sys_id than its value. Below is the script I'm using,

 

function setDiscoveredSourceUser() {
    var userName = source.u_username;

    if (JSUtil.nil(userName))
        return;

    // Only match domain users
    if (userName.indexOf("\\") == -1)
        return;

    var domainName = userName.split("\\")[0];
    userName = userName.substring(domainName.length + 1);

    var sys_user = new GlideRecord("sys_user");
    sys_user.addQuery("domain_name", domainName);
    sys_user.addQuery("account", userName);
    sys_user.addActiveQuery();
    sys_user.orderByDesc("sys_updated_on");
    sys_user.setLimit(1);
    sys_user.query();

    if (!sys_user.next())
        return;

    target.u_discovered_source_user = sys_user.getDisplayValue();
}
1 ACCEPTED SOLUTION

the above syntax did not work too. I created a separate field mapping for discovered source user, it worked good with the same script. 

View solution in original post

3 REPLIES 3

Shaqeel
Mega Sage

Hi @Rashmi Rao 

 

you need to use the getDisplayValue() function. This function returns the display value (name) of a referenced field (sys_id) from a record.

Here are the steps to fix this:

1. Identify the field that is returning sys_id. This is usually a reference field.

2. In your script, instead of directly using the field, use the getDisplayValue() function.

For example, if your field is 'assignment_group', and you are getting sys_id, you should use:

var groupName = current.assignment_group.getDisplayValue();

3. Replace all instances of the field in your script with the variable you just created.

4. Test your script to ensure it's now returning the correct value.

Here is a sample code:

javascript
var gr = new GlideRecord('incident');
gr.get('incident_number', 'INC0010023');
var groupName = gr.assignment_group.getDisplayValue();
gs.info(groupName);


In this code:

- A new GlideRecord is created for the 'incident' table.
- The get() function is used to get a specific incident.
- The getDisplayValue() function is used to get the display value of the 'assignment_group' field.
- The value is then logged to the system log.

Remember, the getDisplayValue() function can only be used on fields that are references to other tables. If the field is not a reference field, this function will not work.


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel

Abhijeet_Pawar
Tera Guru

Hello @Rashmi Rao 

 

 

 

target.u_discovered_source_user = sys_user.getDisplayValue('user_name');//add field name which you are  trying to access.

 

 

 

Above line needs to be corrected.

If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 

Thanks and Regards,
Abhijeet Pawar.

the above syntax did not work too. I created a separate field mapping for discovered source user, it worked good with the same script.