Prefill form without insert, URL encode and optional use Tiny URL

General Add comments
by:

Sometimes you don’t want to create a record but you need to prefill some fields on the form.
You can do this by creating a “dynamic” URL, like this for example in an UI Action (Button) to copy a change task:

[cc lang=”javascript”]
var link = ‘/change_task.do?sys_id=-1&sysparm_query=cmdb_ci=’ + current.cmdb_ci;
link += ‘^priority=’ + current.priority;
link += ‘^change_request=’ + current.change_request;
link += ‘^company=’ + current.company;
link += ‘^risk=’ + current.risk;
link += ‘^impact=’ + current.impact;
link += ‘^urgency=’ + current.urgency;
link += ‘^assignment_group=’ + current.assignment_group;
link += ‘^short_description=’ + escape(current.short_description);
link += ‘^description=’ + escape(current.description);

gs.addInfoMessage(“New change task created as a copy of change task ” + current.number);
action.setRedirectURL(link);
action.setReturnURL(current);
[/cc]

In the script above we will create a link where we redirect the user to.

We use the Javascript function “escape” to encode the value for use in a URL, this prevents errors in the URL with the use of &, ?, ‘ and = signs
(I first used Packages.java.net.URLEncoder.encode but when it’s not needed don’t use Packages)

Optional we can enable the Tiny URL plugin http://wiki.servicenow.com/index.php?title=Tiny_URL_Support.
This will keep the URL tiny and the URL can be as large as we want. When the plugin is enabled the URL will become:
[cc lang=”text” nowrap=”0″]
https://demo12.service-now.com/change_task.do?sysparm_tiny=2518b1eb745320004108ff966b883c29
[/cc]
instead of:
[cc lang=”text” nowrap=”0″]
https://demo12.service-now.com/change_task.dosys_id=-1&sysparm_query=cmdb_ci=a9c0c8d2c6112276018f7705562f9cb0^priority=3^
change_request=undefined^company=^risk=2^impact=3^urgency=3^assignment_group=^short_description=Rollback
%20Oracle%20Version^description=Performance%20of%20the%20Siebel%20SFA%20software%20has%20been%20severely
%20degraded%20since%20the%20upgrade%20performed%20this%20weekend.%20%0D%0AWe%20moved%20to%20an%20unsupported
%20Oracle%20DB%20version.%20%20%20Need%20to%20rollback%20the%20Oracle%20Instance%20to%20a%20supported%20version.
[/cc]

When we look in the Tiny URL table (sys_tiny_url) we will find our record with the following data:
[cc lang=”text” nowrap=”0″]
sys_id=-1&sysparm_query=cmdb_ci%3Da9c0c8d2c6112276018f7705562f9cb0%5Epriority%3D3%5Echange_request%3D
undefined%5Ecompany%3D%5Erisk%3D2%5Eimpact%3D3%5Eurgency%3D3%5Eassignment_group%3D%5Eshort_description%3D
Rollback+Oracle+Version%5Edescription%3DPerformance+of+the+Siebel+SFA+software+has+been+severely+degraded
+since+the+upgrade+performed+this+weekend.+%0D%0AWe+moved+to+an+unsupported+Oracle+DB+version.+++Need+to
+rollback+the+Oracle+Instance+to+a+supported+version.
[/cc]
The Tiny URL will be used when the length of the property glide.tiny_url_min_length is exceeded. (default 1024)

If you have any questions you can contact me using mail on menno.aret@2e2.nl.

Comments are closed.