The Zurich release has arrived! Interested in new features and functionalities? Click here for more

A way to attach a file to the attachment field on the form

dmitry1234354
Kilo Explorer

Hello All,

I work on a framework that tests our instance of ServiceNow using REST API and Selenium framework. In my case I have a form in Catalog Items with a mandatory attachment field. To test that form I have to attach a file and than proceed the form further. I know that there is REST API for making attachments on the records in the table, but is there a way to make an attachment on the form using the REST API?

Thank you.

6 REPLIES 6

srinivasthelu
Tera Guru

Create a New Step configuration. 

 

Step Environment : UI (There is a soft limitation) from the UI but.. 

 

 

(function executeStep(step, stepResult) {

var MESSAGE_KEY_UNABLE_TO_ADD_ATTACHMENTS = "FAILURE: Unable to add attachments";
var MESSAGE_KEY_FAILED_NO_G_FORM = "FAILURE: Unable to set values, g_form is not defined. A valid form must be open before setting values";
var MESSAGE_KEY_SUCCESSFULL = "SUCCESS:Successfully added attachments";

function passStep() {
stepResult.message = MESSAGE_KEY_SUCCESSFULL;
stepResult.success = true;
step.defer.resolve();
}

function failStep(reason) {
stepResult.message = reason;
stepResult.success = false;
step.defer.reject();
}


var testFrameWindow = g_ui_testing_util.getTestIFrameWindow();
if (!testFrameWindow.g_form) {
failStep(MESSAGE_KEY_FAILED_NO_G_FORM);
return;
}


var scope = testFrameWindow.angular.element("#sc_cat_item").scope();

var ga = new GlideAjax('Util');
ga.addParam('sysparm_name', 'copyAttachments');
ga.addParam('step_id', step.sys_id);
ga.addParam('item_id', scope.data._generatedItemGUID);
ga.getXMLWait();

var answer = ga.getAnswer();
testFrameWindow.alert(answer);
scope.attachments= JSON.parse(answer);

scope.setFocusToAttachment();
scope.$apply();


passStep();

}(step, stepResult));

 

Client Callabale Script Include : 

 

var Util = Class.create();
Util.prototype = Object.extendsObject(AbstractAjaxProcessor, {

copyAttachments: function (){
var step_id = this.getParameter('step_id');
var payroll_id = this.getParameter('item_id');
var answer = [];
GlideSysAttachment.copy('x_uxck3_payroll_request', payroll_id, 'sys_atf_step', step_id);

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sys_atf_step');
gr.addQuery('table_sys_id', step_id);
gr.query();
while(gr.next()){
answer.push({
'file_name' : gr.file_name+'',
'sys_id' : gr.sys_id+''

});
}
return JSON.stringify(answer);

},

type: 'Util'
});

 

 

 

 

joel_fischer
ServiceNow Employee
ServiceNow Employee

There are new steps to support the following cases in the New York release:

- Form - Add Attachments to Form

- Server - Add Attachments to Existing Record

https://docs.servicenow.com/bundle/newyork-application-development/page/administer/auto-test-framewo...