Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Scriptes Rest API to create record and add attachment to record

Sneha Anil2
Tera Contributor

Hi,

 

I tried to created scripted rest api to create a record and attach attachment to that record, but attachemnt is added to attachment table and record as undefined.

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var requestBody = request.body.dataString;
    var parser = new global.JSON();
    var parsedData = parser.decode(requestBody);
    //var number = parsedData.incidentNumber;
    var fileName = parsedData.fileName;
    var fileContentType = parsedData.contentType;
    var base64EncodedContent = parsedData.content;
    var regularContent = GlideStringUtil.base64DecodeAsBytes(base64EncodedContent);
    var rec = new GlideRecord('u_new_hire_termination_import');
    rec.initialize();
    rec.u_name = "Workday Request";
    rec.insert();
    var sa = new GlideSysAttachment();
    //var decodedString = GlideStringUtil.base64Decode(fileData);
    sa.write(rec, fileName, fileContentType,regularContent);
    var responseBody = {};
    responseBody.incNumber = rec.number;
    responseBody.status = "Success";
    response.setBody(responseBody);

})(request, response);
 
Thanks in advance,
Sneha
2 REPLIES 2

Akash4
Kilo Sage

Hi Sneha,

There is a statement with syntax error at gs.log line. Remove this line or edit to 

gs.log('File name' +fileName);

Regards, Akash
If my response proves useful, please mark it "Accept as Solution" and "Helpful". This action benefits both the community and me.

Hi Akash,

 

I tried it but still its adding attachment as undefined text file.

 

Thanks,

Sneha