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

Question about upload the photo to user table

JackieZhang
Tera Contributor

Hi all,

I want to write a scripted api to upload the photo to user table. Here is my code but failed. Please help to correct the code.

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var requestBody = request.body.dataString;
    var parser = new global.JSON();
    var parsedData = parser.decode(requestBody);
    var correlation_id = parsedData.userID;
    //var fileName = 'photo';
    var fileContentType = parsedData.contentType;
    var fileData = parsedData.fileData;
    var rec = new GlideRecord('sys_user');
    rec.addQuery('u_correlation_id', correlation_id);
    rec.query();
    if (rec.next()) {
        var attachment_request = new sn_ws.RESTMessageV2();
        attachment_request.setEndpoint(endPoint);
        attachment_request.setHttpMethod('POST');
        var user = 'xxx';
        var password = 'xxxxx';
        attachment_request.setBasicAuth(user, password);
        attachment_request.setRequestHeader("Accept", "application/json");
        attachment_request.setRequestHeader("Content-Type", fileContentType);
        attachment_request.setRequestBody(fileData);
        var attachment_response = attachment_request.execute();
        response.setBody(attachment_response.responseBody);
        response.setStatus(attachment_response.getStatusCode());
    } else {
        var error = "The User ID " + correlation_id + " not found in ServiceNow";
        var responseBodyFailure = {
            "error": error
        };
        responseBodyFailure.status = "Failure";
        response.setStatus(404);
        response.setBody(responseBodyFailure);
    }

})(request, response);
10 REPLIES 10

It is base64encoded data and running in global. I try to use 

GlideStringUtil.base64DecodeAsBytes to decode the data but failed

I also try this code in backgound and got the same issue. Sorry, It not allowed to post the Binary.

var imageBinary ="
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/
";
var user_GR = new GlideRecord('sys_user');
 user_GR.get('09dd9517fbefa2103c67f6026eefdc3f');

 //var photo_sys_id = photo_GR.write(user_GR, 'photo', 'image/jpeg', GlideStringUtil.base64DecodeAsBytes(imageBinary));
 //gs.info(photo_sys_id);


 /* constructor of Attachment class */
    var attachment = new Attachment();


    /* writing the attachment to the database */
var photo_sys_id= attachment.write("sys_user", '09dd9517fbefa2103c67f6026eefdc3f', 'photo', 'image/jpeg', GlideStringUtil.base64DecodeAsBytes(imageBinary));

@JackieZhang 

since you in global scope write() method only accepts the actual content and it will work for text, csv files which are plain files.

try this link where I shared solution to attach file via scripted rest api then enhance it based on your requirement. once the image is copied to record, simply query sys_attachment with this user sysId and update the table name as 'ZZ_YYsys_user'

Scripted Rest API to update incident with attachment 

Also share your JSON request body, how it looks

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Sorry, Can you give me more detail for the solution. I aslo try to use attachment API. but Still got the same error.  imageBinary is base64 encode.
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('POST');

//Eg. UserName="admin", Password="admin" for this code sample.
var user = 'xx';
var password = 'xx';

request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader("Content-Type","image/jpeg");
request.setRequestBody(imageBinary);
var response = request.execute();
gs.log(response.getBody());

@JackieZhang 

try this link where I shared solution to attach file via scripted rest api then enhance it based on your requirement. once the image is copied to record, simply query sys_attachment with this user sysId and update the table name as 'ZZ_YYsys_user'

Scripted Rest API to update incident with attachment 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader