Munender Singh
Mega Sage

While working on the call record and their management -I came across a requirement to convert call records to service request and copy the short description and description of the call record onto the variables on catalog item.The basic approach available on the community says that pass both short desc and desc content into the url of the service request so,when the form loads,you can extract the both fields data and using catalog client script populate in the variables.

But,the issue happens is that if there are many characters in the desc and short desc field of call record,by default servicenow property was converting the url to tinyURL ,which was stopping the catalog client script to read the URl.

So,here is my solution:

1.Script Include

Create a script include and pass the call-short desc and desc fields data and store there.

find_real_file.png

script:

//Created for call records to requests
//the sysID of call record is processed to fetch the short desc and desc
//create by Munender Singh

var getInfoFromCall = Class.create();

getInfoFromCall.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getDetails: function() {

//Get the sys_ID for the call

//pass the input i.e. sys_id of the call record
var callSysID = this.getParameter('sysparm_callSysID');
var gr = new GlideRecord('new_call');

gr.get(callSysID);

var shortDesc = gr.getDisplayValue('short_description');
var desc = gr.getDisplayValue('u_description_html');

var arr = []; // define the array
arr[0] = shortDesc; //set the short desc in the array
arr[1] = desc; //set the desc in the array

return JSON.stringify(arr);
},

type: 'getInfoFromCall'

});

 

2. Onload catalog client script 

To fetch the data from the script include and use it to populate on the variables of item

find_real_file.png

script:

function onLoad() {
var callSysID = getParmVal('sysparm_call_sysid'); //push the sysID of the call record

var ga = new GlideAjax('getInfoFromCall'); //call the script include

ga.addParam('sysparm_name','getDetails'); // call the function of include

ga.addParam('sysparm_callSysID', callSysID); //pass the fetched call sys_id in the include

ga.getXML(handleResponse);

function handleResponse(response){

var answer = response.responseXML.documentElement.getAttribute("answer"); //fetch the response from script include

var answers = answer.evalJSON(); //evaluate the response and decode it

g_form.setValue('short_description',answers[0]); //set the short description
g_form.setValue('u_description',answers[1]); //set the description

}

//function to fetch the data from the current url
function getParmVal(name){

var url = document.URL.parseQuery();

if(url[name]){

return decodeURI(url[name]);

}

else{

return;

}

}

}

 

3.Business Rule-

make changes in the BR 'CallTypeChanged to Request'

var reqFor = current.caller;
var shortDesc = current.short_description;
var desc = current.u_description_html;
var contactType = current.contact_type;
var location = current.caller.location;
var reqItem = current.request_item;
var sysid = current.sys_id;
var comments = "NEW_CALL_REF:" + current.sys_id + " " + current.description;

getCart();

//URL created to set the call_sysid field so,it can be retrieved in our catalog client scripts

var url = 'com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=' + reqItem
+ "&sysparm_requested_for=" + GlideStringUtil.urlEncode(reqFor)
+ "&sysparm_location=" + GlideStringUtil.urlEncode(location)
+ "&sysparm_special_instructions=" + GlideStringUtil.urlEncode(comments)
+ "&sysparm_stack=" + current.getLink()
+ "&sysparm_contact_type=" + contactType
+ '&sysparm_call_sysid=' + GlideStringUtil.urlEncode(sysid);


// var url = "com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=" + reqItem
// + "&sysparm_requested_for=" + GlideStringUtil.urlEncode(reqFor)
// + "&sysparm_location=" + GlideStringUtil.urlEncode(location)
// + "&sysparm_special_instructions=" + GlideStringUtil.urlEncode(comments)
// + "&sysparm_stack=" + current.getLink()
// + "&sysparm_contact_type=" + contactType
// + "&sysparm_short_description=" + GlideStringUtil.urlEncode(shortDesc)
// + "&sysparm_description=" + GlideStringUtil.urlEncode(desc);
action.setRedirectURL(url);
current.isNavigateToCatalog = true;
function getCart() {
var cart = new GlideRecord('sc_cart');
var userid = gs.getUserID();
cart.addQuery('user', userid);
cart.query();
if (cart.next()) {
// We already have a cart so override the requested for value and empty it
cart.requested_for = reqFor;
cart.contact_type = contactType;
cart.special_instructions = comments;
cart.delivery_address = getDeliveryAddress();
cart.update();
var cartItems = new GlideRecord('sc_cart_item');
cartItems.addQuery('cart', cart.sys_id);
cartItems.deleteMultiple();
} else {
cart.initialize();
cart.user = userid;
cart.requested_for = reqFor;
cart.contact_type = contactType;
cart.special_instructions = comments;
cart.delivery_address = getDeliveryAddress();
cart.insert();
}
return cart;
}

function getDeliveryAddress() {
var gr = new GlideRecord('cmn_location');
if (!gr.get(location))
return '';

var text = '';
if (!gr.street.nil())
text = gr.street + '\n';
if (!gr.city.nil() && !gr.state.nil() && !gr.zip.nil())
text += gr.city + ", " + gr.state + ", " + gr.zip;
return text;
}

***Hope this solution would work and no need to alter the property of setting the URL to TImyURL

Regards,

Munender Singh

 

Comments
Paul Porter
Tera Expert

This may be helpful to us.  Thank you for providing your solution.

Munender Singh
Mega Sage

I'm glad to help you.

marcbowen
Tera Contributor

Munender,

I'm trying to use your method and it doesn't bring up the catalog item selected.  Does this only work when a cart is used, or maybe I did something wrong.

 

Thanks

 

Munender Singh
Mega Sage

Hi Marc,

This use case is when call record is converted to a catalog item.If you want to use this method for other URL related scenario,then the code would a bit different.I would be gla dot help you out.

 

Regards,

Munender

marcbowen
Tera Contributor

Yes, if you could provide some assistance that would be greatly appreciated

Munender Singh
Mega Sage

Sure,please share your requirement and I will try to build the solution as per your requirement.

Version history
Last update:
‎02-07-2019 05:24 AM
Updated by: