https://netsuiteprofessionals.com logo
b

Brian Brown

05/04/2022, 3:49 PM
But a PO is never created from that SO line, after this code executes.
b

battk

05/04/2022, 3:56 PM
what does the rest of the code look like
b

Brian Brown

05/04/2022, 3:57 PM
Thanks @battk. This is a MR script that loads the SO record and adds a new line that is to be dropshipped....
Let me paste my REDUCE function here:
Copy code
function reduce(context) {
  	try {
/**
		log.debug('context.values is:', JSON.stringify(context.values))
  	  var soId = context.key;
      var uniqueKey = context.values  //this is not stringified, but is an array of objects
	  //var tagItem = context.values.tagitem;
	  log.debug('soId / uniqueKey:', soId +' / '+ uniqueKey)
**/


		      var soIdKey = context.key
                log.debug('soIdKey is:', soIdKey)

                if ( context.values.length > 0 ) {

                    var dataInput = [];

                    for ( var i = 0 ; i < context.values.length ; i++ ) {
                        dataInput.push(JSON.parse(context.values[i]))
                    }

                } else {
                    var dataInput = JSON.parse(context.values[0])
                }
                var objInput = {};
                objInput[soIdKey] = dataInput;
                //The key here is to loop through the array elements and parse them individually.
                log.debug('objInput is:', JSON.stringify(objInput));  //the entire array of all objects
      
	  
	  
////load SO/////	  
	  var soRec = record.load({
        type: record.Type.SALES_ORDER,
        id: soIdKey,
        isDynamic: true
      });
     log.debug('Loaded SO / objInput[soIdKey].length:', soIdKey +' / '+ objInput[soIdKey].length)
	  
	 var headerLocationId = soRec.getValue({fieldId: 'location'})
	 var headerDepartmentId = soRec.getValue({fieldId: 'department'})
	 var headerProductLineId = soRec.getValue({fieldId: 'class'})
	 
      // Get SO record line item count
      var lineCount = soRec.getLineCount({
        sublistId: 'item'
      });
      
 ////for loop////
 for(var i = 0; i < objInput[soIdKey].length; i++) {
		
		log.debug('Searching for objInput[soIdKey].uniquekey:', objInput[soIdKey][i].uniquekey)
		
		var indexFound = soRec.findSublistLineWithValue({
			sublistId: 'item',
			fieldId: 'lineuniquekey',
			value: objInput[soIdKey][i].uniquekey
		});
		log.debug('indexFound is:', indexFound)
		
		if(indexFound == -1) continue

              soRec.insertLine({sublistId: 'item', line: indexFound + 1});
			  		  
              soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'item', value: objInput[soIdKey][i].tagitem});
			  
              soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'description', value: 'Scripted Tag Item '});
			  
              soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'quantity', value: objInput[soIdKey][i].tagquantity});
			  
			  soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'rate', value: 0});
			  
			  soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'location', value: 1});
			  
              soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'createpo', value: 'DropShip'});
			  
			  
			  ///SET MANDATORY LINE values
			  if(headerDepartmentId){
				  
				  soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'department', value: headerDepartmentId});
				   
			  }
			  if(headerLocationId){
				  
				  soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'location', value: headerLocationId});
				    
			  }
			  if(headerProductLineId){
				    
				  soRec.setCurrentSublistValue({sublistId: 'item', fieldId: 'class', value: headerProductLineId});
				  
			  }
			  
  
              soRec.commitLine({
                sublistId: 'item'
              });
			  
		log.debug('Added line:', Number(indexFound + 1))
	
		/////set as processed//////////
			  soRec.selectLine({
                sublistId: 'item',
                line: indexFound,
              });
			 soRec.setCurrentSublistValue({
				sublistId: 'item',
				fieldId: 'custcol_tags_dropship_processed',
				value: true
			  });
			  soRec.commitLine({
                sublistId: 'item'
              });
		log.debug('Set line:', indexFound + ' to processed')
		
		  	  
		  
        }//end sublist for loop


      // Save SO
      var soSaved = soRec.save({
        enableSourcing: false,
        ignoreMandatoryFields: true
      });

      context.write({
        key: soIdKey 
      }); 

	log.debug('Saved SO:', soSaved)

	  
  	} catch (error) {
      log.error('reduce error', error);
    }
  }
b

battk

05/04/2022, 3:58 PM
doesnt sound like a valid operation
edit the sales order in the ui
if you cant set the field in the ui, you are unlikely to be able to in script
b

Brian Brown

05/04/2022, 4:03 PM
I can set the field in the UI appropriately.
b

battk

05/04/2022, 4:04 PM
on edit?
b

Brian Brown

05/04/2022, 4:24 PM
Yes that's correct
This is the automatic DropShip PO creation functionality in NetSuite.
It seems the trigger is the APPROVE button click int he UI
b

battk

05/04/2022, 4:25 PM
same thing in script actually, you need to change the order status
you dont get access to most buttons in suitescript
b

Brian Brown

05/04/2022, 4:26 PM
Just tried that, but not seeing success.
Changed from 'A' to 'B' orderstatus, but the appropriate POs were not created from the SO 😕
b

battk

05/04/2022, 4:26 PM
so you have to do the matching action from the edit page
b

Brian Brown

05/04/2022, 4:26 PM
Hmmm... what is the 'matching action' ?
not sure I'm familiar with that
interested to hear more
b

battk

05/04/2022, 4:28 PM
you cant click buttons in suitescript
thats not entirely true, but its mostly true
so you have to replicate the buttons action by setting fields
b

Brian Brown

05/04/2022, 4:29 PM
I realize that. Only using jQuery or some DOM scripting. But I'm not trying to do that.
Yes yes I understand that.
b

battk

05/04/2022, 4:29 PM
in this case, approving a sales order is equivalent to editing the order status
b

Brian Brown

05/04/2022, 4:29 PM
Yep, I'm setting 'orderstatus' to 'B'
I think I realize what may be the issue.... the SALES ORDER is PENDING FULFILLMENT status....
then I change it to PENDING APPROVAL
set CREATEPO to DROPSHIP...
then I change to PENDING FULFILLMENT again
I bet the initial status has to be PENDING APPROVAL, when the SO is loaded
b

battk

05/04/2022, 4:33 PM
it why i was telling you about the invalid operation
approved sales orders dont allow new drop shipments
the ui wont let you
b

Brian Brown

05/04/2022, 4:34 PM
the ui will let you
but you must change the STATUS back to PENDING APPROVAL!
first
b

battk

05/04/2022, 4:37 PM
you would have to do the same thing in script, which is to change the order status back to pending approval
keep in mind that its basically a one time thing
b

Brian Brown

05/04/2022, 4:41 PM
Ya, I did change the orderstatus from B to A. Then back to B again.
I believe because the order is not in PENDING APPROVAL when I load it!
I believe that is root cause.
b

battk

05/04/2022, 4:55 PM
drop ship orders are created when the sales order is approved (or immedietly if starting as pending fulfillment)
you are able to set some sales orders back to pending approval, but basically any that have related records to it cant be put back into pending approval
if you do manage to get it back to pending approval , then you are back to step one and approving it creates the drop ships
b

Brian Brown

05/04/2022, 4:58 PM
Giving that a try!