How to Autofill the reference field in Mobile?

Rosfield
Tera Contributor

Hi

I created a script to autofill the contact field with the user

System Definition > Script Includes

var GetDefaultContactValue = Class.create();
GetDefaultContactValue.prototype = {
    initialize: function() {},

    getDefaultValue: function() {
        var userId = gs.getUserID();
		var userName = gs.getUserDisplayName();
        return {
			"Value" : userId,
			"DisplayValue" : userName
		};
    },

    type: 'GetDefaultContactValue'
};

 

 Executable scripts

new sn_csm_mobile.GetDefaultContactValue().getDefaultValue();

 

This will not be executed.
Please tell me what is wrong with this script

 

I referenced these articles

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1166399

https://www.servicenow.com/community/mobile-apps-platform-forum/mobile-app-builder-auto-fill-assigne...

6 REPLIES 6

HarshithaK
Tera Contributor

Hi @Rosfield ,

Autofill variable can also be done through creating a function, u need to provide the input form screen and give the input and variables
Under the Autofill variable select the variable that you have created.

Screenshot 2025-04-08 at 4.26.28 PM.png

 

Thanks

Wesley Govis
ServiceNow Employee
ServiceNow Employee

In case anyone comes across this issue and is struggling as well in the future, the answer is you have to also return the display value for reference fields as described in the following support article: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1166399

Instructions
This issue can be fixed by using the "Scripted" variable instead of the "Constant" type variable.
We have to use a script includes to return the "Value" and "Display Value" in JSON object, which we want to set as a default value to the reference type input:-

return {
    "Value": SYS_ID_OF_RECORD,
    "DisplayValue": DISPLAY_NAME_OF_RECORD
};
Note: Both values are expected the String type. Please utilise `toString()` to convert the values if needed.

This script includes has to be passed to the Variable using the "Script" attribute.


Here are the steps for the same -

Create a new script include to return the "Value" and "Display Value", as shown in the example above, which we want to set as the reference input
Create a new variable of the "Scripted" type.
Then associated the script includes created in step1 to the variable using the attribute "Script".
After doing the above, the default value for the reference input is set as expected. If you still have any further queries, please post your questions in the community.