- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2020 06:02 AM
I have a custom widget button for my Catalog Item, which is perfectly able to create incidents when clicked,
only thing it lacks at this point is retrieving attachments from the catalog item to my incident record after the click.
All I'm looking for is to call attachments from client controller to Server script & attach to my incident record.
Please advise if there are any other better approach in getting these attachments. Thanks!!
This is my current widget code -
HTML -
<button name="submit" ng-click="c.uiAction('submit')" class="btn btn-success">Report Issue</button>
Client Controller - Inside function()
//---------------------------------------------------------
var c = this;
c.uiAction = function(action) {
c.data.action = action;
c.data.description = g_form.getValue("description");
c.data.short_description = g_form.getValue("short_description");
c.data.sub_category = g_form.getDisplayValue("sub_category");
c.data.category = g_form.getDisplayValue("category");
c.server.update().then(function() {
c.data.action = undefined;
setFieldsReadonly();
$scope.submitting = false;
top.window.onbeforeunload = null;
top.window.onunload = null;
top.window.location = c.data.url;
})
}
Server script:
//------------------------------------------------------------------------------------
(function() {
if (localInput && localInput.action) {
var action = localInput.action;
data.url = "";
data.sys_id = "";
if (action == 'submit') {
var gr = new GlideRecord('incident');
gr.initialize();
gr.short_description = input.short_description;
gr.description = input.description;
gr.caller_id = gs.getUserID();
gr.comments = gr.short_description + "\n" + gr.description;
gr.urgency = 2;
gr.contact_type = 'self-service';
gr.assignment_group = "93d598011b7f581093c962c7bd4bcb32";
gr.setValue("subcategory", input.sub_category);
gr.setValue("category", input.category);
gr.business_service = "sys_id";
gr.insert();
gs.addInfoMessage("Submitting.....");
var url = "https://xxxx.service-now.com/sp?sys_id=${" + gr.sys_id + "}&view=sp&id=ticket&table=incident";
data.url = url;
data.sys_id = gr.sys_id;
GlideSysAttachment.copy('sc_cart_item',gr.sys_id,'incident', gr.sys_id);//not sure if this is right?
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2020 11:07 AM
Thanks initially I did thought of include script, but my testing team has some weird reservation on API usage 🙂
I referred to this workaround by jamesmcwhinney & did some changes to my code in cloned widget
https://community.servicenow.com/community?id=community_question&sys_id=46318be5db98dbc01dcaf3231f9619bf
I called below g_form.generatedItemGUID in to my client script to the server at c.data.sys_id_cart & retrieved as input to the server & used GlideSysAttachment.copy() to copy the attachments -
YAY!!!!
Server Script -----------------
var id_cart = input.sys_id_cart;
//gs.addInfoMessage(id_cart );
GlideSysAttachment.copy('sc_cart_item', id_cart, 'incident', gr.sys_id);
Client Controller --------------------------------------------
c.uiAction = function(action) {
c.data.sys_id_cart = g_form.generatedItemGUID;
}
var g_form;
$scope.$on('spModel.gForm.initialized', function(e, gFormInstance){
if (gFormInstance.getSysId() != -1 && gFormInstance.getSysId() != c.getItemId())
return;
g_form = gFormInstance;
$timeout(function() {
$rootScope.$emit('spModel.gForm.rendered', g_form);
}, 175);
g_form.$private.events.on('submitted', function(){
if ($scope.data.sc_cat_item.item_action === "order")
getOne();
else if ($scope.data.sc_cat_item.item_action === "add_to_cart")
addToCart();
else if ($scope.data.sc_cat_item.item_action == "update_cart")
updateCart();
});
g_form.generatedItemGUID = $scope.data._generatedItemGUID;
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2020 07:55 AM
Okay, the Package thing would be a tedious way. So, an easy way is to make a REST API Call.
- Create a script include and include functions as below,
getAttachment: function(thirdPartyRecord,recordSysID, username, password,endpoint) {
try {
var sa = new sn_ws.RESTMessageV2();
sa.setEndpoint('${base_instance_url}' + '/api/now/attachment?sysparm_query=table_sys_id=' + '${id}');
sa.setHttpMethod("GET");
sa.setStringParameter("base_instance_url",endpoint);
sa.setStringParameter("id",thirdPartyRecord);
sa.setBasicAuth(username, password);
var res = sa.execute();
var att_obj = JSON.parse(res.getBody());
for(var i=0; i<att_obj.result.length; i++){
gs.info(att_obj.result[i].sys_id);
return this.getAttachmentBinary(att_obj.result[i].sys_id,recordSysID, att_obj.result[i].file_name, username,password,endpoint);
}
} catch (ex) {
gs.info("IntegrationUtils Attachment Error: " + ex.message);
}
},
getAttachmentBinary: function(attachment_sys_id, recordSysId, filename, username, password,endpoint) {
try {
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
var response,
tablename = this.integration_data,
httpResponseStatus;
request.setEndpoint('${base_instance_url}' + '/api/now/attachment/' + attachment_sys_id + '/file');
request.setStringParameter("base_instance_url",endpoint);
request.setBasicAuth(username, password);
request.setRequestHeader("Content-Type","*/*");
request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);
response = request.execute();
httpResponseStatus = response.getStatusCode();
gs.info("Result http response status_code: " + httpResponseStatus);
gs.info("Result : "+response.haveError() + " " + response.getErrorMessage());
} catch (ex) {
var message = ex.message;
gs.info("IntegrationUtils Attachment Binary " +message);
}
},
- Call the Script Include function getAttachment() with all the parameters, just after incident insertion.
- DO OPTIMIZE THE CODE AS NEEDED.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2020 11:07 AM
Thanks initially I did thought of include script, but my testing team has some weird reservation on API usage 🙂
I referred to this workaround by jamesmcwhinney & did some changes to my code in cloned widget
https://community.servicenow.com/community?id=community_question&sys_id=46318be5db98dbc01dcaf3231f9619bf
I called below g_form.generatedItemGUID in to my client script to the server at c.data.sys_id_cart & retrieved as input to the server & used GlideSysAttachment.copy() to copy the attachments -
YAY!!!!
Server Script -----------------
var id_cart = input.sys_id_cart;
//gs.addInfoMessage(id_cart );
GlideSysAttachment.copy('sc_cart_item', id_cart, 'incident', gr.sys_id);
Client Controller --------------------------------------------
c.uiAction = function(action) {
c.data.sys_id_cart = g_form.generatedItemGUID;
}
var g_form;
$scope.$on('spModel.gForm.initialized', function(e, gFormInstance){
if (gFormInstance.getSysId() != -1 && gFormInstance.getSysId() != c.getItemId())
return;
g_form = gFormInstance;
$timeout(function() {
$rootScope.$emit('spModel.gForm.rendered', g_form);
}, 175);
g_form.$private.events.on('submitted', function(){
if ($scope.data.sc_cat_item.item_action === "order")
getOne();
else if ($scope.data.sc_cat_item.item_action === "add_to_cart")
addToCart();
else if ($scope.data.sc_cat_item.item_action == "update_cart")
updateCart();
});
g_form.generatedItemGUID = $scope.data._generatedItemGUID;
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2020 03:06 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2020 01:10 PM
Nice one.