4

The following script is executed after clicking a button on sales order. It creates work orders for items on the sales order if some conditions are fulfilled. It works fine, but sometimes it stops working and I need to undeploy and deploy the script again for it to work. Do you know what can cause this behaviour?

/**
 * @NApiVersion 2.x
 * @NScriptType workflowactionscript
 */
define(['N/record', 'N/search'],        
        
    function(record, search) {
        var scriptName = "createWO";
        "use strict";           

        function onAction (context) {
            var funcName = scriptName + "onAction " + context.newRecord.type + " " + context.newRecord.id;
            log.debug(funcName, "Starting");
            
        try {
            
        /**Getting values from SO.
         */
                    var currentRecord = context.newRecord;
                    
                    var so = currentRecord.getValue({
                    fieldId: 'id'
                    });
                        

        
        var s = search.create({
            type : search.Type.TRANSACTION,
               columns:
               [
                  search.createColumn({name: "item"}),
                  search.createColumn({name: "formulanumeric", formula: "({quantity}-NVL({quantitycommitted},0))-nvl({quantityshiprecv},0)"})
               ],
            filters:
            [
                ["type","anyof","SalesOrd"], 
                "AND", 
                ["mainline","is","F"], 
                "AND", 
                ["internalidnumber","equalto",so], 
                "AND", 
                ["taxline","is","F"], 
                "AND", 
                ["shipping","is","F"], 
                "AND", 
                ["item.type","anyof","Assembly"], 
                "AND", 
                ["formulanumeric: ({quantity}-NVL({quantitycommitted},0))-nvl({quantityshiprecv},0)","greaterthan","0"]
            ]
        });


        var searchResultCount = s.runPaged().count;
        log.debug("s result count",searchResultCount);
        s.run().each(function(result){
           // .run().each has a limit of 4,000 results
           return true;
        });
        
        var searchResult = s.run().getRange({start:0,end:999});
        
    for(var j=0;j<searchResultCount;j++) {
        
            var item = searchResult[j].getValue({name:'item'});
            var qtytobuild = searchResult[j].getValue({name:'formulanumeric'});
            
    
                            
                            var TO = record.create({
                            type: 'workorder',
                            isDynamic:true
                            });                     
                            
                            TO.setValue({
                            fieldId: 'customform', 
                            value: '50'
                            });
                            
                            TO.setValue({
                            fieldId:'subsidiary', 
                            value: 1
                            });                         
                            
                            TO.setValue({
                            fieldId: 'location', 
                            value: 2
                            });
                            
                            
                            TO.setValue({
                            fieldId: 'assemblyitem', 
                            value: item
                            });
                            
                            TO.setValue({
                            fieldId: 'quantity', 
                            value: qtytobuild
                            });
                            
                            var newWO = TO.save({                  
                            ignoreMandatoryFields: true    
                            });
                            
                            
                
    }
                
                                
                                
            } catch (e) {
                log.error(funcName + "Unable to create WO", e)
            }           
        }

    return {
        onAction : onAction
    };
    
});
bknights
  • 14,408
  • 2
  • 18
  • 31
JeppePeppe
  • 51
  • 4
  • What's the error or behaviour that occurs when "it stops working" ? – Simon Delicata Nov 08 '21 at 22:44
  • Hi, Thanks for replying! Nothing happens, no log on the script, the order just reloads and no work order is created. – JeppePeppe Nov 09 '21 at 09:43
  • Have you turned in logging on the workflow? It should show an attempt to run the script. Are you sure the workflow is transitioning to the correct state? – Simon Delicata Nov 09 '21 at 09:46
  • Yes, it doesn't transitioning to the correct state. I then redeploy the script and the workflow is transaitioning to the correct state again. Can you see anything strange in my script? – JeppePeppe Nov 09 '21 at 09:50
  • If it doesn't log the "Starting" message then it's a fault with the workflow. Are there any preconditions on the script execution? Is it set to run on "Entry" or "After Submit".? An action set to run "After Submit" wont run if your button is pressable in view mode. – Simon Delicata Nov 09 '21 at 09:57
  • My $0.02, it sounds like you know what you're doing and this may be a weird race condition in the platform. Next time this happens, try making a video to clearly demonstrate the symptom and the resolution. Then open a NetSuite Support case with the video. – Marty C. May 20 '23 at 23:57

0 Answers0