Hello, I am writing my first script that is suppos...
# suitescript
j
Hello, I am writing my first script that is supposed to post new/cancelled sales order fulfillments to slack channels based on the assigned location, but I am running into an error when uploading to NetSuite. Is anyone able to help please?
Copy code
/**  
 * 
 * @NApiVersion 2.0
 * @NScriptType UserEventScript 
 * @NModuleScope Public
 */

define(['N/log', 'N/record', 'N/runtime', 'N/https', 'N/search'],
    function(log, record, runtime, https, search) {
        function postSlack(newRecord) {
            var attachement = slackAttachement(newRecord);
            var url = newRecord.fields.location.custrecord_slack_webook;
            // var headers = "curl -X POST -H 'Content-type: application/json' --data '"
            try {
                var response = <http://https.post|https.post>({
                    url: url,
                    // headers: headers,
                    headers: {
                        'content-type': 'application/json'
                    },
                    data: attachement
                });
                var body = JSON.parse(response.body);
                log.debug({ title: 'response', details: response });
                return body;
            } catch (e) {
                log.debug({ title: 'fetch error', details: e });
                return null;
            }
        }

        function afterSubmit(context) {
            var newRecord = JSON.parse(JSON.stringify(context.newRecord));
            log.debug({ title: 'status', details: newRecord.fields.status });
            if (newRecord.fields.type == 'Fulfillment Request') {
                if (newRecord.fields.status == 'New' || newRecord.fields.status == 'Cancelled') {
                    postSlack(newRecord);
                }
            }
        }
        function slackAttachement(newRecord) {
          var color;
          var attachments;
          var locationName = newRecord.fields.location;

          if (newRecord.fields.status === "new") {
            color = "##546e7a";
            attachments = [
              {
                "fallback": "New Fulfillment Request",
                "color": color,
                "pretext": "New Fulfillment Request",
                "fields": [
                  {
                    "title": "Sales Order",
                    "value": newRecord.fields.createdfrom.otherrefnum,
                  },

                  {
                    "title": "Date",
                    "value": '${newRecord.fields.createdfrom.datecreated}',
                  },
                  {
                    "title": "Location",
                    "value": locationName,
                  },
                  // {
                  //   "title": "Item(s)",
                  //   "value": itemFulfillment.item,
                  // },
                  // {
                  //   "title": "Netsuite Link",
                  //   "value": linktolineitem,
                  // },
                ]
              }
            ];
          } else if (newRecord.fields.status === "cancelled") {
            color = "#bf9494";
            attachments = [
              {
                "fallback": 'Cancelled Fulfillment Request',
                "color": color,
                "pretext": 'Cancelled Fulfillment Request',
                "fields": [
                  {
                    "title": "Sales Order#",
                    "value": newRecord.fields.createdfrom.otherrefnum,
                  },
                  {
                    "title": "Date/Time Modified",
                    "value": '${newRecord.fields.lastmodifieddate}',
                  },
                  {
                    "title": "Location",
                    "value": locationName,
                  },
                  // {
                  //   "title": "Item(s)",
                  //   "value": itemFulfillment.item,
                  // },
                  // {
                  //   "title": "Netsuite Link",
                  //   "value": linktolineitem,
                  // },
                ]
              }
            ];
          } else {
            throw new Error('Unknown status sent to sendSlackMessage: ${newRecord.fields.status}');
          }

          return attachments;
       }
});
r
Copy code
/**
 * Example
 * @NApiVersion 2.0
 * @NScriptType Suitelet
 */
You're just missing the asterisks before the @NApi tags
j
I still get this error
r
I've not looked at the script in too much detail - header was the first thing that stuck out but you've got a rogue ';'
Copy code
function postSlack(newRecord);
✔️ 1
c
You also need to return the afterSubmit function. Take a look at SuiteAnswers: 43800 & 51660. I'd understand that first then come back and try to write this again. For retrieving values, look at SuiteAnswer: 45372.
👆 1