SuiteScript 2.0 UserEvent Script to Call Map Reduc

2019-09-16 01:31发布

问题:

Good afternoon.

I am trying to get a User Event script to call or use a Map Reduce script. I am really new to the concept of a Map Reduce script and am not having much luck finding resources. Essentially, what I want to do is call a Map Reduce script that finds open transactions with the same Item Name and sets the Class on that item to the new item set by the User. The Map Reduce script would need to the Item Name and Class from the current record.

Here is my User Event:

/**
* @NApiVersion 2.0
* @NScriptType UserEventScript
*/

define(['N/record', 'N/log'],
    function (record, log) {
        function setFieldInRecord (scriptContext) {
            log.debug({
                'title': 'TESTING',
                'details': 'WE ARE IN THE FUNCTION!'
            });
            if (scriptContext.type === scriptContext.UserEventType.EDIT) {
                var old_Record = scriptContext.oldRecord;
                var cur_Record = scriptContext.newRecord;
                var oldClassId = old_Record.getValue({ fieldId: 'class'});
                var curClassId = cur_Record.getValue({ fieldId: 'class'});
                if ( oldClassId != curClassId ) {
                    // CALL MAP REDUCE HERE
                }
            }
        }
        return {
            beforeSubmit: setFieldInRecord
        };
    }
);

Is the Map Reduce Script a separate file or is it embedded in the User Event script? I think I can get the Map Reduce to work if I know how to call it from the User Event. I appreciate any input with this question. Thank you!

回答1:

Here is how we handled this situation.

We made sure to add 'N/task' to the define statement in the above code for the User Event. Then, in the User Event when the conditions were met in order to call the Map / Reduce Script, we did this:

var scriptTask = task.create({
     taskType: task.TaskType.MAP_REDUCE
});
scriptTask.scriptId = 'customscript_id';
scriptTask.deploymentId = 'customdeploy_id';
var scriptTaskId = scriptTask.submit();

This successfully called the Map Reduce Script from the User Event.

I hope this helps out someone in the future.

Thank you.