Janiece
11/10/2021, 3:47 PM/**
*
* @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;
}
});
Ryan J
11/10/2021, 3:58 PM/**
* Example
* @NApiVersion 2.0
* @NScriptType Suitelet
*/
You're just missing the asterisks before the @NApi tagsJaniece
11/10/2021, 4:05 PMRyan J
11/10/2021, 4:06 PMfunction postSlack(newRecord);
creece
11/10/2021, 4:49 PM