This topic contains 6 replies, has 0 voices, and was last updated by karenn 8 years, 2 months ago.

  • Author
    Posts
  • #1780

    karenn

    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
    This is a cached copy. Click here to see the original post.

  • #1781

    JCirocco

    Similar post from yesterday may help:

    https://usergroup.netsuite.com/users…-https-promise

  • #1782

    karenn

    Thanks John! Mine is strictly a Scheduled SuiteScript so I don;t think so but who knows. I am definitely missing something using promise

  • #1783

    david.smith

    I don’t think you can use promise with a scheduled script. I could be wrong though.

  • #1784

    chanarbon

    Hi @karenn,

    Please note that Promise API and Promise object is currently limited to client side. As of the moment, server side scripts are limited to the synchronous processes unlike client script which is user facing. Please check out SuiteAnswers ID 43804 (https://netsuite.custhelp.com/app/an…il/a_id/43804/) for more information.

  • #1785

    david.smith

    karenn You might want to consider using a map/reduce script. Much more efficient at traversing search results and doing something with them.

  • #1786

    karenn

    Client side…aha!! Missed that little tidbit. Thanks.

    Map/Reduce I guess is my next try to figure out how to use that.

    Thanks all!!

You must be logged in to reply to this topic.