How to send the details of all SO created yesterday except closed/cancelled/Exchange to broadly.
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
/********************************************
* * Broadly Software Integrations
*
* ******************************************************
*
* Author: Jobin and Jismi IT Services
*
* Date Created :04 -January-2023
*
* Created By: Jobin and Jismi IT Services
*
* Description : Broadly Software Integrations
*
* REVISION HISTORY
*
**********************************************************/
define(['N/https', 'N/record', 'N/runtime', 'N/search'],
/**
* @param{https} https
* @param{record} record
* @param{runtime} runtime
* @param{search} search
*/
(https, record, runtime, search) => {
const FORM_ID = '197'; // Exchange sales order form ID
/**
* Function to search the sales order data which is not closed and cancelled or an exchange sales order
* @returns {boolean|*[]}
*/
function salesOrderSearchData (){
try{
let salesorderSearchObj = search.create({
type: "salesorder",
filters:
[
["type","anyof","SalesOrd"],
"AND",
["status","noneof","SalesOrd:C","SalesOrd:H"],
"AND",
["datecreated","on","yesterday"],
"AND",
["customform","noneof",FORM_ID],
"AND",
["mainline","is","T"],
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "tranid", label: "Document Number"}),
search.createColumn({name: "custbody_aha_sales_location", label: "Sales Location"}),
search.createColumn({
name: "internalid",
join: "customer",
label: "Internal ID"
}),
search.createColumn({name: "custbody8", label: "Bill to Main Number"}),
search.createColumn({name: "custbody17", label: "Ship To Email (For Thank You)"}),
search.createColumn({name: "custbody45", label: "Sales Associate"}),
search.createColumn({
name: "altname",
join: "customer",
label: "Name"
}),
]
});
let searchResultCount = salesorderSearchObj.runPaged().count;
log.debug("salesorderSearchObj result count",searchResultCount);
let dataArray=[];
if(searchResultCount > 0){
salesorderSearchObj.run().each(function(result){
let dataObj={}
dataObj.internalid=result.getValue({
name: "internalid", label: "Internal ID"
});
dataObj.docNumber=result.getValue({
name: "tranid", label: "Document Number"
});
dataObj.salesLocation = result.getValue({
name: "custbody_aha_sales_location", label: "Sales Location"
});
dataObj.salesLocationName = result.getText({
name: "custbody_aha_sales_location", label: "Sales Location"
});
dataObj.customerId=result.getValue({
name: "internalid",
join: "customer",
label: "Internal ID"
});
dataObj.customerName= result.getValue({
name: "altname",
join: "customer",
label: "Name"
})
dataObj.phone=result.getValue({
name: "custbody8", label: "Bill to Main Number"
});
dataObj.shipEmail = result.getValue({
name: "custbody17", label: "Ship To Email (For Thank You)"
});
dataObj.salesAssociate = result.getValue({
name: "custbody45", label: "Sales Associate"
})
dataObj.salesAssociateName = result.getText({
name: "custbody45", label: "Sales Associate"
})
dataArray.push(dataObj)
return true;
});
return dataArray;
}
else
return false;
}
catch(err)
{
log.debug("error@FunctionsalesOrderSearchData",err)
}
}
/**
* Function to call API
* @param bodyObj is the object which contains the sales order data
* @returns {ClientResponse}
*/
function sendRequestToBroadly(bodyObj){
let headerObj = {
"Content-Type": "application/json",
}
let response = https.post({
url:'www.testurl.com'
body: JSON.stringify(bodyObj),
headers: headerObj
});
return response
}
/**
* Defines the function that is executed at the beginning of the map/reduce process and generates the input data.
* @param {Object} inputContext
* @param {boolean} inputContext.isRestarted - Indicates whether the current invocation of this function is the first
* invocation (if true, the current invocation is not the first invocation and this function has been restarted)
* @param {Object} inputContext.ObjectRef - Object that references the input data
* @typedef {Object} ObjectRef
* @property {string|number} ObjectRef.id - Internal ID of the record instance that contains the input data
* @property {string} ObjectRef.type - Type of the record instance that contains the input data
* @returns {Array|Object|Search|ObjectRef|File|Query} The input data to use in the map/reduce process
* @since 2015.2
*/
const getInputData = (inputContext) => {
try{
let data = salesOrderSearchData();
return data;
}
catch(err)
{
log.debug("error@GetInputData",err)
}
}
/**
* Defines the function that is executed when the reduce entry point is triggered. This entry point is triggered
* automatically when the associated map stage is complete. This function is applied to each group in the provided context.
* @param {Object} reduceContext - Data collection containing the groups to process in the reduce stage. This parameter is
* provided automatically based on the results of the map stage.
* @param {Iterator} reduceContext.errors - Serialized errors that were thrown during previous attempts to execute the
* reduce function on the current group
* @param {number} reduceContext.executionNo - Number of times the reduce function has been executed on the current group
* @param {boolean} reduceContext.isRestarted - Indicates whether the current invocation of this function is the first
* invocation (if true, the current invocation is not the first invocation and this function has been restarted)
* @param {string} reduceContext.key - Key to be processed during the reduce stage
* @param {List<String>} reduceContext.values - All values associated with a unique key that was passed to the reduce stage
* for processing
* @since 2015.2
*/
const reduce = (reduceContext) => {
try{
let dataObj=reduceContext.values.map(JSON.parse);
let recId = dataObj[0].internalid;
let apiObj = {
"Service_Order_Number" : dataObj[0].docNumber,
// "Location_ID" : dataObj[0].salesLocation,
"Location_Name":dataObj[0].salesLocationName,
"Customer_ID":dataObj[0].customerId,
"Customer_Name":dataObj[0].customerName,
"Customer_Email":dataObj[0].shipEmail,
"Phone_Number":dataObj[0].phone,
"Associate":dataObj[0].salesAssociate,
"Associate_Name":dataObj[0].salesAssociateName,
}
log.debug("apiObj*****",apiObj);
if (runtime.EnvType.PRODUCTION){
log.debug("production environment")
let response = sendRequestToBroadly(apiObj);
log.debug("response**",response);
let body=JSON.parse(response.body)
log.debug("Body**",body);
let status = body.status;
if(status === 'success')
{
let value = record.submitFields({
type: record.Type.SALES_ORDER,
id: recId,
values: {
custbody_jj_broadly_integraion: true
},
options: {
enableSourcing: false,
ignoreMandatoryFields : true
}
});
}
}
else if (runtime.EnvType.SANDBOX) {
log.debug('You are using your sandbox!');
}
}
catch(err)
{
log.debug("error@Reduce",err)
}
}
return {getInputData,reduce}
});