Jira Code: AD 27
This task is to set item record inactive when product life cycle is inactive.When inactive product life cycle is selected in item record, item record inactive fields should be set to true and vice versa.
Workflow action script
/**
* @NApiVersion 2.x
* @NScriptType workflowactionscript
* @ScriptTitle AD 27 WA JJ Set Item actv or inactv
* @ScriptID customscript_ad27_wa_jj_set_item_actv_in
* @RelatedTask AD 27
* @AppliedTo ADV - Product Lifecycle Status
* @Description This workflow is to set item is active or inactive based on the SET ITEM INACTIVE
* on ADV - Product Lifecycle Status.
* DONE BY JJ TEAM
* @CreatedBy AJ 08/01/2019
*/
define(['N/record', 'N/search','N/file','N/url','N/https','N/task'],
/**
* @param {record} record
* @param {search} search
*/
function(record, search,file,url,https,task) {
/**
* Definition of the Suitelet script trigger point.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @Since 2016.1
*/
function onAction(scriptContext) {
try{
// to get the record id
var recID = scriptContext.newRecord.id;
// to get current record
var currentRecord = scriptContext.newRecord;
// to get the set inactive field
var setInactive = currentRecord.getValue({
fieldId:'custrecord_adv_status_setinactive'
});
var scheduleScrptTask = task.create({
taskType: task.TaskType.MAP_REDUCE,
scriptId: "customscript_ad27_mr_jj_set_inactive",
deploymentId: 'customdeploy_ad27_mr_jj_set_inactive',
params: {
custscript1: recID
}
});
scheduleScrptTask.submit();
}catch(e)
{
log.debug("Err@ onAction FN ",e.message);
log.error("Err@ onAction FN ",e.message);
}
}
return {
onAction : onAction
};
});
Map Reduce script
/**
* @NApiVersion 2.x
* @NScriptType MapReduceScript
* @NModuleScope SameAccount
* customscript_ad27_mr_jj_set_inactive';
'customdeploy_ad27_mr_jj_set_inactive
*/
define(['N/record', 'N/search', 'N/runtime'],
/**
* @param {record} record
* @param {search} search
*/
function(record, search ,runtime) {
/**
* Marks the beginning of the Map/Reduce process and generates input data.
*
* @typedef {Object} ObjectRef
* @property {number} id - Internal ID of the record instance
* @property {string} type - Record type id
*
* @return {Array|Object|Search|RecordRef} inputSummary
* @since 2015.1
*/
function getInputData() {
try{
var recId = runtime.getCurrentScript().getParameter("custscript1");
var itemSearchObj = search.create({
type: "item",
filters:
[
["formulanumeric: CASE WHEN({custitem_adv_product_lifecycle_status.custrecord_adv_status_setinactive}!={isinactive}) THEN 1 ELSE 0 END","equalto","1"],
"AND",
["custitem_adv_product_lifecycle_status","anyof",recId]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "type", label: "Type"}),
search.createColumn({name: "isinactive", label: "Inactive"})
]
});
var searchResultCount = itemSearchObj.runPaged().count;
return itemSearchObj;
}catch(e)
{
log.debug("Err@ FN ",e.message);
log.error("Err@ FN ",e.message);
}
}
/**
* Executes when the map entry point is triggered and applies to each key/value pair.
*
* @param {MapSummary} context - Data collection containing the key/value pairs to process through the map stage
* @since 2015.1
*/
function map(context) {
try{
}catch(e)
{
log.debug("Err@ FN map",e.message);
log.debug("Err@ FN map",e.message);
}
}
/**
* Executes when the reduce entry point is triggered and applies to each group.
*
* @param {ReduceSummary} context - Data collection containing the groups to process through the reduce stage
* @since 2015.1
*/
function reduce(context) {
try{
var internalID = context.key;
var Obj =JSON.parse(context.values[0]);
var recordType= Obj.recordType;
var setField= Obj.values.isinactive;
// to submit
var id = record.submitFields({
type:recordType,
id:internalID,
values:{
isinactive:setField=="F"?true:false
}
});
}catch(e)
{
log.debug("Err@ FN reduce",e.message);
log.error("Err@ FN reduce",e.message);
}
}
/**
* Executes when the summarize entry point is triggered and applies to the result set.
*
* @param {Summary} summary - Holds statistics regarding the execution of a map/reduce script
* @since 2015.1
*/
function summarize(summary) {
try{
}catch(e)
{
log.debug("Err@ FN summarize",e.message);
log.error("Err@ FN summarize",e.message);
}
}
return {
getInputData: getInputData,
reduce: reduce,
summarize: summarize
};
});