
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
This is a repost from my blog(https://blog.jacebenson.com/post/2018-07-15-sp-set-variables-via-url/).
The other day, a post was made, asking how to do this and I had to answer. I knew you could read the URL via $window but that isn’t available in client scripts. So how can this be done?
I came up with the following solution.
Create a Variable type of macro, with a widget that has the following client script;
function($scope, $window) {
// This is the controller, we've included
// $scope in the function above because
// it's easy to work with
var c = this;
// We are going to simplify accessing
// g_form within the client script by
// setting it as a variable named g_form
var g_form = $scope.page.g_form;
//We are going to simplify accessing
// g_form within the HTML by setting
// it as a $scope attribute
$scope.g_form = $scope.page.g_form;
// from here you can just iterate over
// the url params;
var params = $window.location.href.split('?')[1];
console.log(params);
var paramsToString = params.toString();
var paramsArr = paramsToString.split('&');
paramsArr.map(function(keyValue){
var key = keyValue.split('=')[0];
var value = keyValue.split(key + '=').join('');
value = decodeURIComponent(value);
try {
var message = 'Setting ' + key + ' to ';
message += value + ' from url parameter.';
console.log(message);
$scope.g_form.setValue(key,value);
} catch (error) {
console.log('Error setting field', error);
}
});
}
This will try to set all the attributes on the form so in the following url;
https://dev32369.service-now.com/sp?id=sc_cat_item&sys_id=b480811a0f021300fc69cdbce1050ece&description=test
If you found this helpful, please press the "Helpful" link below and/or comment.
P.S.
Laurent Chicoine pointed out another way to do this for an individual variable at a time instead of all of them by setting the default value, you can see his comment in the comments and mark his helpful if you find this shortened version helpful.
If you set the default value of the variable in question to this, then you can default it differently based on weather or not `$sp` exists.
javascript: (function(){
try{
// Service Portal
// if $sp exists do this
return $sp.getParameter('var_short_description') || '';
} catch(e){
// UI16
// if $sp causes an error cause its not defined do this
return RP.getParameterValue('var_short_description');
}
})()
- 4,612 Views
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.