Brijmohan
Brijmohan

Reputation: 13

How to send attachment dynamic in payload?

I am getting below error in logs. test 1 {"error":{"code":"RequestBodyRead","message":"An unexpected 'StartObject' node was found for property named 'name' when reading from the JSON reader. A 'PrimitiveValue' node was expected."}}

Also I am getting empty value in my attachment body. "attachments":[{"@odata.type":"#microsoft.graph.fileAttachment","name":{},"contentType":{},"contentBytes":"SGVsbG8gV29ybGQh"}]},"saveToSentItems":"true"}

I am working with Microsoft graph api. I am trying to send attachment dynamically through business rule on sys_email table. I am having issue with sending the value in payload from attachment table, it is showing empty in response body. I did glide sys_attachment table and get the value of content type and name then pass the value in payload. I think there is issue with my code or approch. Can you please suggest the right approch. Please find my attached code below. `

var body = current.body_text;
var subject = current.subject;

var name;
var type;
var sysid;
var att = new GlideRecord('sys_attachment');
att.addQuery('table_name', 'sys_email');
att.addQuery('table_sys_id', current.sys_id);
att.query();
while(att.next()) {
                gs.info('test attach');
type = att.content_type;
                name = att.file_name;
                sysid = att.sys_id;
                gs.info('test attach'+name);
                gs.info('test attach'+sysid);
                gs.info('test attach'+type);
}

var restMessage = new sn_ws.RESTMessageV2('Email PoC', 'POST Email User');
restMessage.setStringParameter('id', current.u_emailbox_id);


    var ccnulladresses = current.getValue('copied');
    var ccRe = [];
    if (ccnulladresses != null) {
        var ccRemoveSpace = current.getValue('copied').replace(/\s*,\s*/ig, ',');
        var ccaddresses = ccRemoveSpace.split(',');
       for (i = 0; i < ccaddresses.length; i++) {
            ccRe.push({
                "emailAddress": {
                    "address": ccaddresses[i] + ""
                }
            });
        }
    }


    // Set the request body
    var requestBody = {

        "message": {
            "subject": current.getValue('subject'),
            "body": {
                "contentType": "html",
                "content": current.getValue('body')
            },
            "toRecipients": [{
                "emailAddress": {
                    "address": current.getValue('direct')
                }
            }],
            "ccRecipients": ccRe,

            "internetMessageHeaders": [{
                "name": "X-MSIP-APP-classification",
                "value": "Restricted"
            }],
            "attachments": [

                {
                    "@odata.type": "#microsoft.graph.fileAttachment",
                    "name": name,
                    "contentType": type,
                    "contentBytes": "SGVsbG8gV29ybGQh"
                }

            ]
        },
        "saveToSentItems": "true"
    };

    gs.info("test 3 " + requestBody + JSON.stringify(requestBody));
    restMessage.setRequestBody(JSON.stringify(requestBody));
    //restMessage.setRequestBody((requestBody));
    // Send the REST message

    gs.info("test 4 " + restMessage);
    var response = restMessage.execute();

    // Get the response details
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();

    gs.info("test 1 " + responseBody);
    gs.info("test 2 " + httpStatus);


`

Upvotes: 1

Views: 129

Answers (0)

Related Questions