I am trying to write a scheduled script to traverse each of the results from a saved search. If I just use search.load and run it works fine. But when I try to use promise I get an error right away and I am not sure why. The error that is logged is
org.mozilla.javascript.EcmaError: TypeError: Cannot find function promise in object function loadSearch(options) {...}. (/SuiteScripts/PE Scripts 2.0/pe2_ss_costchanges.js#30)
Here is the script
/**
* @NApiVersion 2.0
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/search', 'N/email'],
function(record, search, email) {
/**
* Definition of the Scheduled script trigger point.
*
* @param {Object} scriptContext
* @param {string} scriptContext.type - The context in which the script is executed. It is one of the values from the scriptContext.InvocationType enum.
* @Since 2015.2
*/
function execute(scriptContext)
{
//email.send({
// author : 6,
// recipients : 6,
// subject : 'Cost Changes Start',
// body : 'Cost Changes updates started'
// });
search.load.promise({
type: search.Type.ITEM_RECEIPT,
id: 'customsearch_pe_porecptcostchgs'
}).then(function (searchObj) {
return searchObj.run().each.promise(
function (result, index) {
var itemId = result.getValue({name: 'internalid', join: 'item'});
log.debug('itemId: ' + itemId );
var itemRec = loadItemRec(itemId);
var abcCode = result.getValue({name: 'custitem_abc_code', join: 'item'});
log.debug('abccode: ' + abcCode );
return true;
})
})
//this works!!
//var mySearch = search.load({id: 'customsearch_pe_porecptcostchgs'});
//mySearch.run().each(function(result) {
// var itemId = result.getValue({name: 'internalid', join: 'item'});
// log.debug('itemId: ' + itemId );
// var itemRec = loadItemRec(itemId);
// var abcCode = result.getValue({name: 'custitem_abc_code', join: 'item'});
// log.debug('abccode: ' + abcCode );
// return true;
//});
}
function loadItemRec(itemid) {
var item = null;
itemok = false;
try
{
item = record.load({
type : record.Type.INVENTORY_ITEM,
id : itemid,
isDynamic : false
})
itemok = true;
}
catch(e)
{
try
{
item = record.load({
type : record.Type.ASSEMBLY_ITEM,
id : itemid,
isDynamic : false
})
itemok = true;
}
catch(e)
{
try
{
item = record.load({
type : record.Type.KIT_ITEM,
id : itemid,
isDynamic : false
})
itemok = true;
}
catch(e)
{
itemok = false;
//alert("Unable to find item to determine type");
}
}
}
if (itemok)
{
return item;
}
else
{
return null;
}
}
return {
execute: execute
};
});
I have found very few examples of scripts using promise so I am kind of flying blind here. Any help would be appreciated. Probably something stupid I am just not seeing.
Thanks!
Karen