Insert attachment for my custom widget button in service portal

Roshan Poduval3
Kilo Expert

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?

}
}

}

 

 

1 ACCEPTED SOLUTION

Roshan Poduval3
Kilo Expert

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;


});

 

 

View solution in original post

8 REPLIES 8

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

So you are not actually submitting a request but you have a custom widget which is invoked by button and you want to transfer the attachments attached to this catalog item to this incident record?

Thanks,
Ashutosh

Correct, I want to transfer the attachments attached to this catalog item to incident record.

Thanks

Community Alums
Not applicable

I ran onto a similar problem.
Does this widget exists on a custom app, or is it on global?

I suspect if GlideSysAttachment works on global SP widgets. I suppose you need the Package call for that one.

Below snip taken from : https://snprotips.com/blog/2016/2/25/understanding-and-using-glideattachment

find_real_file.png

yes my current widget is a global widget,

Also tried this code but did not work,

 


var fileNameSansExtension = 'work.txt';

var grRecordWithAttachment = new GlideRecord(tableName);
grRecordWithAttachment.get(gr.sys_id);//sys_id of the incident

var gsa = new GlideSysAttachment();
//ONLY works in global
var textVal = gsa.get(grRecordWithAttachment, fileNameSansExtension);

gs.print(textVal);

 

 

Thanks