Quick question regarding Client Scripts: I have a ...
# suitescript
a
Quick question regarding Client Scripts: I have a CS with a FIELD CHANGED FUNCTION. It does not trigger when I am creating a record. What am I missing? It triggers fine when editing an existing record.
b
make sure that the script deployment has a correct audience and especially event type
a
@battk Thank you. I had left it blank hoping it will cover everything. Do I need to have 2 deployments? 1 for Edit and 1 for Create?
b
blank means everything
a
That's what I thought. So audience is set to ALL and Event Type is blank. Still have this issue.
b
what does the script look like
a
Let me post it.
I apologize in advance for my code. . .
Copy code
function setLineDate(type,name)
{
  var userId = nlapiGetUser();
 
  nlapiLogExecution('DEBUG','USER',userId);
  	
  var recId = nlapiGetRecordId();
  nlapiLogExecution('DEBUG','RECORD ID',recId);
	
  var recType = nlapiGetRecordType();
  nlapiLogExecution('DEBUG','RECORD Type',recType);
   
  var context = nlapiGetContext();
  nlapiLogExecution('DEBUG','CONTEXT',context);
   
  var custReqDate = nlapiGetFieldValue('custbody_mpad_txn_cust_req_date_af');//Customer Requested Date (Auto-Fill)
  nlapiLogExecution('DEBUG','Customer Requested Date',custReqDate);
   
  var revExpShipDate = nlapiGetFieldValue('custbody_mpad_txn_rev_exp_shipdate_af'); //Revised Expected Ship Date (Auto-Fill)
  nlapiLogExecution('DEBUG','Revised Expected Ship Date',revExpShipDate);
   
  var actShipDate = nlapiGetFieldValue('custbody_mpad_txn_act_shipdate_af'); //Actual Ship Date (Auto-fill)
  nlapiLogExecution('DEBUG','Requested Delivery Date Header',actShipDate);
   
  var promDelDate = nlapiGetFieldValue('custbody_promised_delivery_date'); //Promised Delivery Date
  nlapiLogExecution('DEBUG','Promised Delivery Date',promDelDate);
   
  var reqRecDate = nlapiGetFieldValue('custbody_requested_receipt_date'); //PO - Requested Receipt Date (Auto-Fill)
  
  nlapiLogExecution('DEBUG','PO-Requested Receipt Date',reqRecDate);
   
  var promRecDate = nlapiGetFieldValue('custbody_promised_receipt_date'); //PO - Promised Receipt Date (Auto-Fill)
  nlapiLogExecution('DEBUG','PO-Requested Receipt Date',promRecDate);
   
  var revRecDate = nlapiGetFieldValue('custbody_revised_receipt_date'); //PO - Revised Receipt Date (Auto-Fill)
  nlapiLogExecution('DEBUG','PO-Requested Receipt Date',revRecDate);
   
  var leadTime = nlapiGetFieldValue('custbody_mpad_txn_lead_time_weeks_af'); //PO - Revised Receipt Date (Auto-Fill)
  nlapiLogExecution('DEBUG','PO-Requested Receipt Date',leadTime);
   

if(type == 'create' || type == 'view' || type == 'edit'){
  var i;
  var itemLines = nlapiGetLineItemCount('item');
     nlapiLogExecution('DEBUG','Line Count',itemLines);
   

 //if (recType == 'Sales Order'){
  if (name === 'custbody_mpad_txn_cust_req_date_af')
  {
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
          	//var itemRec = nlapiLoadRecord('item',itemId);
			//var itemType = nlapiGetFieldValue('itemtype');
          	//var itemType = nlapiLookupField('item',itemId,'type');
			//var lc = nlapiGetLineItemValue('item', i, 'custcol_pm_expected_ship_date');
		  var itemId = nlapiGetLineItemValue('item','item',i);
         
          nlapiLogExecution('DEBUG','Item ID',itemId);
          
          var itemType = nlapiGetLineItemValue('item','itemtype',i);
          
          nlapiLogExecution('DEBUG','Item Type',itemType);
          
			if	(itemType != 'Description' && itemType != 'Subtotal'){
			nlapiSetLineItemValue('item', 'custcol_pm_customer_requested_date', i, custReqDate);
            }
  			//var setFocus = document.getElementsByName('custbody_mpad_txn_cust_req_date_af');
			//setFocus[0].focus();
		}
  }
  if (name === 'custbody_mpad_txn_rev_exp_shipdate_af')
    //nlapiLogExecution('DEBUG','Name2',name);
  {
		for (i = 1; i <= itemLines; i++)
        {
            var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);
          
           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);
          
			if	(itemType != 'Description' && itemType != 'Subtotal'){
			nlapiSetLineItemValue('item', 'custcol_pm_expected_ship_date', i, revExpShipDate);
            }
  			//var setFocus = document.getElementsByName('custbody_mpad_txn_rev_exp_shipdate_af');
			//setFocus[0].focus();
		}
  }
  if (name === 'custbody_mpad_txn_act_shipdate_af')
    //nlapiLogExecution('DEBUG','Name3',name);
  {
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);
          
           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);
          
			if	(itemType != 'Description' && itemType != 'Subtotal'){
			nlapiSetLineItemValue('item', 'custcol_pm_actual_ship_date', i, actShipDate);
            }
  			//var setFocus = document.getElementsByName('custbody_mpad_txn_act_shipdate_af');
			//setFocus[0].focus();
		}
  }
  if (name === 'custbody_promised_delivery_date')
  {
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);
          
           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          
          nlapiLogExecution('DEBUG','Item Type',itemType);
          
			if	(itemType != 'Description' && itemType != 'Subtotal'){
			//var lc = nlapiGetLineItemValue('item', i, 'custcol_promised_delivery_date');
			nlapiSetLineItemValue('item', 'custcol_promised_delivery_date', i, promDelDate);
            }
  			//var setFocus = document.getElementsByName('custbody_promised_delivery_date');
			//setFocus[0].focus();
		}
  }
  //}


 // if (recType == 'Purchase Order'){ //Purchase Order Fields
   if (name === 'custbody_requested_receipt_date') //Requested Receipt Date (Auto-Fill)
  {
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);
          
           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);
        
			if	(itemType != 'Description' && itemType != 'Subtotal'){
			//var lc = nlapiGetLineItemValue('item', i, 'custcol_requested_receipt_date');
			nlapiSetLineItemValue('item', 'custcol_requested_receipt_date', i, reqRecDate);
            }
  			//var setFocus = document.getElementsByName('custbody_requested_receipt_date');
			//setFocus[0].focus();
		}
  }
    if (name === 'custbody_promised_receipt_date') //Promised Receipt Date (Auto-Fill)
  {
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);
          
           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);
          
			if	(itemType != 'Description' && itemType != 'Subtotal'){
			//var lc = nlapiGetLineItemValue('item', i, 'custcol_promised_receipt_date');
			nlapiSetLineItemValue('item', 'custcol_promised_receipt_date', i, promRecDate);
            }
  			//var setFocus = document.getElementsByName('custbody_promised_receipt_date');
			//setFocus[0].focus();
		}
  }
    if (name === 'custbody_revised_receipt_date') //Revised Ship Date (Auto-Fill)
  {
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);
          
           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          
          nlapiLogExecution('DEBUG','Item Type',itemType);
          
			if	(itemType != 'Description' && itemType != 'Subtotal'){
			//var lc = nlapiGetLineItemValue('item', i, 'custcol_pm_revised_ship_date');
			nlapiSetLineItemValue('item', 'custcol_pm_revised_ship_date', i, revRecDate);
            }
  			//var setFocus = document.getElementsByName('custbody_revised_receipt_date');
			//setFocus[0].focus();
		}
  }
 // }

  // if (recType == 'Estimate'){ //Estimate Fields Fields
   if (name === 'custbody_mpad_txn_lead_time_weeks_af') //Requested Receipt Date (Auto-Fill)
  {
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);
          
           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          
          nlapiLogExecution('DEBUG','Item Type',itemType);
          
			if	(itemType != 'Description' && itemType != 'Subtotal'){
			//var lc = nlapiGetLineItemValue('item', i, 'custcol_requested_receipt_date');
			nlapiSetLineItemValue('item', 'custcol_pm_lead_time', i, leadTime);
            }
  			//var setFocus = document.getElementsByName('custbody_mpad_txn_lead_time_weeks_af');
			//setFocus[0].focus();
		}
  }
}
}
b
Copy code
var userId = nlapiGetUser();
console.debug('hello there');
nlapiLogExecution('DEBUG','USER',userId);
add a console debug and check the console after a field changes
a
It does not log anything
b
what does the script record look like
a
message has been deleted
message has been deleted
message has been deleted
b
everything looks correct to me
a
I was so afraid of you saying that
I tried a pageinit as well and that did nothing either
b
you have a weird looking script file name
but i dont think that would cause your issue
a
I hope not, haha. I will keep working on this.Will circle back with you if I get anything.
b
otherwise its working fine in my account
a
So it works for you both in create and edit type?
b
yes
a
Damn it!
Okay, let me run some more tests. Do you mind sharing with me what your console log looks like when you run it?
b
its what you had before, log statements appear in the script logs
a
Thank you
b
you can always try deleting everything and recreate all the records
a
๐Ÿ˜ž I'd rather not have to do that, haha.
Thanks for the last resort option though.
@battk So I have this deployed on 3 different transaction subrecords: 1. Purchase Order 2. Quote 3. Sales Order I tested and sales order is the only one that does not trigger when type == create.
b
outside of the deleting everything and recreating it
disable other client scripts
a
I'll give that a try
Hello again. So I started receiving A LOT of complaints about NetSuite being slow. I thought it to be internet issues until a user sent me a screen shot with unexpected error for this script. I inactivated it and everyone said their NetSuite was back to normal. How do I optimize this script so that it does not cause latency?
n
Do you know why you were getting an unexpected error? Maybe start with figuring that out, maybe you're getting in to a loop and it's only relinquishing control to the user when it finally fails.
a
Ah,very smart. I just added a try & catch. Any other recommendations for tools I can use to see what the issue is?
b
what does the code look like now?
a
Copy code
function setLineDate(type,name)
{
    try {
  //if( type == 'create' || type == 'edit'){
  var uiType = type;
  nlapiLogExecution('DEBUG','TYPE',uiType);

  var userId = nlapiGetUser();
  nlapiLogExecution('DEBUG','USER',userId);
//if(userId == '30784'){
  var recId = nlapiGetRecordId();
  nlapiLogExecution('DEBUG','RECORD ID',recId);

  var recType = nlapiGetRecordType();
  nlapiLogExecution('DEBUG','RECORD Type',recType);

  var context = nlapiGetContext();
  nlapiLogExecution('DEBUG','CONTEXT',context);
  var step = context.getSessionObject('stage');
  nlapiLogExecution('DEBUG','CONTEXT2',step);

  var custReqDate = nlapiGetFieldValue('custbody_mpad_txn_cust_req_date_af');//Customer Requested Date (Auto-Fill)
  nlapiLogExecution('DEBUG','Customer Requested Date',custReqDate);

  var revExpShipDate = nlapiGetFieldValue('custbody_mpad_txn_rev_exp_shipdate_af'); //Revised Expected Ship Date (Auto-Fill)
  nlapiLogExecution('DEBUG','Revised Expected Ship Date',revExpShipDate);

  var actShipDate = nlapiGetFieldValue('custbody_mpad_txn_act_shipdate_af'); //Actual Ship Date (Auto-fill)
  nlapiLogExecution('DEBUG','ActualSHip Date',actShipDate);

  var promDelDate = nlapiGetFieldValue('custbody_promised_delivery_date'); //Promised Delivery Date
  nlapiLogExecution('DEBUG','Promised Delivery Date',promDelDate);

  var reqRecDate = nlapiGetFieldValue('custbody_requested_receipt_date'); //PO - Requested Receipt Date (Auto-Fill)
  nlapiLogExecution('DEBUG','PO-Requested Receipt Date',reqRecDate);

  var promRecDate = nlapiGetFieldValue('custbody_promised_receipt_date'); //PO - Promised Receipt Date (Auto-Fill)
  nlapiLogExecution('DEBUG','PO-Promised Receipt Date',promRecDate);

  var revRecDate = nlapiGetFieldValue('custbody_revised_receipt_date'); //PO - Revised Receipt Date (Auto-Fill)
  nlapiLogExecution('DEBUG','PO-Revised Receipt Date',revRecDate);

  var leadTime = nlapiGetFieldValue('custbody_mpad_txn_lead_time_weeks_af'); //QO - Lead Time (Auto-Fill)
  nlapiLogExecution('DEBUG','QO-Lead Time',leadTime);

  var i;
  var itemLines = nlapiGetLineItemCount('item');
     nlapiLogExecution('DEBUG','Line Count',itemLines);

if(itemLines > 0){
  if (name === 'custbody_mpad_txn_cust_req_date_af')
  {
    console.log('HELLO')
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
		  var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);

          var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);

			if	(itemType != 'Description' && itemType != 'Subtotal'){
			nlapiSetLineItemValue('item', 'custcol_pm_customer_requested_date', i, custReqDate);
            }
  			//var setFocus = document.getElementsByName('custbody_mpad_txn_cust_req_date_af');
			//setFocus[0].focus();
		}
  }
  if (name === 'custbody_mpad_txn_rev_exp_shipdate_af')
    //nlapiLogExecution('DEBUG','Name2',name);
  {
		for (i = 1; i <= itemLines; i++)
        {
            var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);

           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);

			if	(itemType != 'Description' && itemType != 'Subtotal'){
			nlapiSetLineItemValue('item', 'custcol_pm_expected_ship_date', i, revExpShipDate);
            }
  			//var setFocus = document.getElementsByName('custbody_mpad_txn_rev_exp_shipdate_af');
			//setFocus[0].focus();
		}
  }
  if (name === 'custbody_mpad_txn_act_shipdate_af')
    //nlapiLogExecution('DEBUG','Name3',name);
  {
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);

           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);

			if	(itemType != 'Description' && itemType != 'Subtotal'){
			nlapiSetLineItemValue('item', 'custcol_pm_actual_ship_date', i, actShipDate);
            }
  			//var setFocus = document.getElementsByName('custbody_mpad_txn_act_shipdate_af');
			//setFocus[0].focus();
		}
  }
  if (name === 'custbody_promised_delivery_date')
  {
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);

           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);

			if	(itemType != 'Description' && itemType != 'Subtotal'){
			//var lc = nlapiGetLineItemValue('item', i, 'custcol_promised_delivery_date');
			nlapiSetLineItemValue('item', 'custcol_promised_delivery_date', i, promDelDate);
            }
  			//var setFocus = document.getElementsByName('custbody_promised_delivery_date');
			//setFocus[0].focus();
		}
  }
  //}


 // if (recType == 'Purchase Order'){ //Purchase Order Fields
   if (name === 'custbody_requested_receipt_date') //Requested Receipt Date (Auto-Fill)
  {
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);

           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);

			if	(itemType != 'Description' && itemType != 'Subtotal'){
			//var lc = nlapiGetLineItemValue('item', i, 'custcol_requested_receipt_date');
			nlapiSetLineItemValue('item', 'custcol_requested_receipt_date', i, reqRecDate);
            }
  			//var setFocus = document.getElementsByName('custbody_requested_receipt_date');
			//setFocus[0].focus();
		}
  }
    if (name === 'custbody_promised_receipt_date') //Promised Receipt Date (Auto-Fill)
  {
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);

           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);

			if	(itemType != 'Description' && itemType != 'Subtotal'){
			//var lc = nlapiGetLineItemValue('item', i, 'custcol_promised_receipt_date');
			nlapiSetLineItemValue('item', 'custcol_promised_receipt_date', i, promRecDate);
            }
  			//var setFocus = document.getElementsByName('custbody_promised_receipt_date');
			//setFocus[0].focus();
		}
  }
    if (name === 'custbody_revised_receipt_date') //Revised Ship Date (Auto-Fill)
  {
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);

           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);

			if	(itemType != 'Description' && itemType != 'Subtotal'){
			//var lc = nlapiGetLineItemValue('item', i, 'custcol_pm_revised_ship_date');
			nlapiSetLineItemValue('item', 'custcol_pm_revised_ship_date', i, revRecDate);
            }
  			//var setFocus = document.getElementsByName('custbody_revised_receipt_date');
			//setFocus[0].focus();
		}
  }


  // if (recType == 'Estimate'){ //Estimate Fields Fields
   if (name === 'custbody_mpad_txn_lead_time_weeks_af') //Requested Receipt Date (Auto-Fill)
  {
    //nlapiLogExecution('DEBUG','Name1',name);
		for (i = 1; i <= itemLines; i++)
        {
          var itemId = nlapiGetLineItemValue('item','item',i);
          nlapiLogExecution('DEBUG','Item ID',itemId);

           var itemType = nlapiGetLineItemValue('item','itemtype',i);
          nlapiLogExecution('DEBUG','Item Type',itemType);

			if	(itemType != 'Description' && itemType != 'Subtotal'){
			//var lc = nlapiGetLineItemValue('item', i, 'custcol_requested_receipt_date');
			nlapiSetLineItemValue('item', 'custcol_pm_lead_time', i, leadTime);
            }
  			//var setFocus = document.getElementsByName('custbody_mpad_txn_lead_time_weeks_af');
			//setFocus[0].focus();
		}
  }
}
  }
  catch (e){
    nlapiLogExecution('DEBUG','CATCH',e);
  }
 // }
}
have not released since adding try catch
b
toss all the logs
a
They are all DEBUG and the log level is set to ERROR on all deployments. You think thats the issue?
Or its just pissing you off to see shit codelike this? haha
b
each log is a server request
a
Oh damn!
Wow, okay. I will remove them. Maybe I should also log the usage as well. What was the limit for CS? 1000 units?
u
Yes. You have 1000 usage units for a client script function. And I would suggest that you remove your log execution cause ,more or less adds around 50 ms and up per log. Just use the console.log() or just any only when error
a
Thank you all for the technical information. Was not aware of the log issues. Thank you.
n
just remember to remove your console.log/table once you've resolved this, nobody wants the console spamming ๐Ÿ˜‰
๐Ÿ‘ 1
(yes NS PS I'm looking at you and your damn rebate module / tax module / drag and drop)
a
Another great suggestion. Thank you all. This is an awesome community to be a part of.
Soooooo. NS support got back to me about why the script was not triggering when CREATING Sales Orders. We have 15 CS deployed and only the first 10 are getting triggered.