
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-09-2023 11:24 PM
Hi All, I have one email script to generate clickable link below code is a part of email script, Sometimes link gives an error ("414 Request - URI Too Large") error, As per checking URL length is too long approx 15k to 17k characters thats why that error came.
To fix this error I used below code, It is working fine, It converts Long url to tiny url by using POST method and create record in sys_tiny_url table and then used that tiny url in our email, It looks good and working as expected, but Hardcoding username / passwd in a script is not a good idea, Can anyone please suggest how can I proceed without hadrcoading of username and password in script
//Maximum Practical length is 2086 charecters
if (url.length < 2086) {
gs.info('No need to convert in Tiny URL is');
template.print('<a href=' + url + '>report link </a');
} else {
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://' + gs.getProperty('instance_name') + '.service-now.com/api/now/tinyurl');
request.setHttpMethod('POST');
//Eg. UserName="admin", Password="admin" for authentication, any basic servicenow user
var user = "admin";
var password = "admin";
var body = {};
body.url = 'https://' + gs.getProperty('instance_name') + '.service-now.com/itam_portal?id=my_it_asset_report&table=u_gap&filter=ccl_sys_class_name%3Dcmdb_ci_appl%5EORcl_sys_class_name%3Dcmdb_ci_computer%5Ecl_install_statusNOT%20IN111,14%5Ecl_u_number%20IN' + hwname + '%5Ecl_assignment_group%2Emanager=' + manager + '&view=my_it_asset_report'; //url that need to be shortened
request.setBasicAuth(user, password);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestBody(JSON.stringify(body));
var response = request.execute();
var jsonString = response.getBody().toString(); // get JSON of url
var cleanedString = jsonString.replace(/["}]+$/g, '');
var startPos = cleanedString.indexOf('"result":"');
if (startPos !== -1) {
var shorturl = cleanedString.substring(startPos + '"result":"'.length);
}
gs.info('Tiny URL is: ' + shorturl);
template.print('<a href=' + shorturl + '>report link </a');
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-10-2023 03:36 AM
@Community Alums
setup the profile in REST Message then inside your Rest message function set the authentication type as Inherit from parent or you can again select the same basic auth profile
add the profile name which you create as 2nd parameter
//Maximum Practical length is 2086 charecters
if (url.length < 2086) {
gs.info('No need to convert in Tiny URL is');
template.print('<a href=' + url + '>report link </a');
} else {
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://' + gs.getProperty('instance_name') + '.service-now.com/api/now/tinyurl');
request.setHttpMethod('POST');
//Eg. UserName="admin", Password="admin" for authentication, any basic servicenow user
// var user = "admin"; // not required
//var password = "admin"; // not required
var body = {};
body.url = 'https://' + gs.getProperty('instance_name') + '.service-now.com/itam_portal?id=my_it_asset_report&table=u_gap&filter=ccl_sys_class_name%3Dcmdb_ci_appl%5EORcl_sys_class_name%3Dcmdb_ci_computer%5Ecl_install_statusNOT%20IN111,14%5Ecl_u_number%20IN' + hwname + '%5Ecl_assignment_group%2Emanager=' + manager + '&view=my_it_asset_report'; //url that need to be shortened
//request.setBasicAuth(user, password); // this is not required
request.setAuthenticationProfile('basic', profile name); // give the basic auth profile name here
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestBody(JSON.stringify(body));
var response = request.execute();
var jsonString = response.getBody().toString(); // get JSON of url
var cleanedString = jsonString.replace(/["}]+$/g, '');
var startPos = cleanedString.indexOf('"result":"');
if (startPos !== -1) {
var shorturl = cleanedString.substring(startPos + '"result":"'.length);
}
gs.info('Tiny URL is: ' + shorturl);
template.print('<a href=' + shorturl + '>report link </a');
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-10-2023 04:05 AM
@Community Alums
rest message and rest message function (post) both will have same endpoint.
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-10-2023 04:07 AM
Thanks @Ankur Bawiskar , It is working fine now, Thanks for your assistance!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā11-10-2023 03:50 AM
@Community Alums
I believe I have provided enough assistance on this thread.
Thank you for marking my response as helpful.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
⨠Certified Technical Architect || ⨠9x ServiceNow MVP || ⨠ServiceNow Community Leader