Hello. I am new to scripting and wanted to know if...
# suitescript
a
Hello. I am new to scripting and wanted to know if my code makes sense; also, I have to replicate it for more than 1 field so would appreciate suggestions on how to adjust the code to achieve this. Background: After user enters a date value in the transaction body field {custbody_document_date}, the script should set the item line field {custcol_pm_expected_ship_date} for all lines to the date entered on the mainline field. I have set my function under Field Changed Function. Thank you.
Copy code
function setLineDate()
{
  var recId = nlapiGetRecordId();
  nlapiLogExecution('DEBUG','RECORD ID',recId);

  var context = nlapiGetContext();
  nlapiLogExecution('DEBUG','CONTEXT',context);

  var userId = nlapiGetUser();
  nlapiLogExecution('DEBUG','USER',userId);

  var docDateHead = nlapiGetFieldValue('custbody_document_date');
  nlapiLogExecution('DEBUG','Document Date Header',docDateHead);

  var i;
  var itemLines = nlapiGetLineItemCount('item');
for (i = 1; i <= itemLines; i++) {
nlapiSetLineItemValue('item', 'custcol_pm_expected_ship_date', i, docDateHead);
}
}
s
Copy code
const so = new SalesOrder(context.currentRecord)
_.forEach(so.item, line=> line.custcol_pm_expected_ship_date =  so.custbody_document_date)
// nft
a
Is that 2.0? @stalbert
Also, my concern is that I have several other body fields that need to populate line fields. So this needs to loop for those instances as well but only if the field is edited. Can this be done within this function?
s
that is a snippet of 2.0 with NFT - a thin library to make suitescript simpler. You can expand the same treatment to any number of fields. Others in the forum can surely provide more verbose SS2.0 implementation of same.
a
Okay. Thank you!
After the lines are set, the cursor moves to the next line. Is there a way I can have the page remain where it is? Behavior similar to #fieldname used in URLs.
I still need some help on this if anyone can. Much appreciated. Thank you!
b
not sure what you are describing
a
@battk Once the user sets a date value on field (Document Date - body field), the script populates a column field (Delivery Date) for all line items. But when the script is done, it brings the users down to the last line (as if to allow for entry of a new line item). I would like for the user to remain in the body of the transaction as they have to do this for several other fields (all located on the body.)
b
what code are you using? nlapiSetLineItemValue shouldnt do that
a
The code I listed above. That is what I am using and it does that.
Copy code
function setLineDate()
{
  var recId = nlapiGetRecordId();
  nlapiLogExecution('DEBUG','RECORD ID',recId);
  var context = nlapiGetContext();
  nlapiLogExecution('DEBUG','CONTEXT',context);
  var userId = nlapiGetUser();
  nlapiLogExecution('DEBUG','USER',userId);
  var docDateHead = nlapiGetFieldValue('custbody_document_date');
  nlapiLogExecution('DEBUG','Document Date Header',docDateHead);
  var i;
  var itemLines = nlapiGetLineItemCount('item');
for (i = 1; i <= itemLines; i++) {
nlapiSetLineItemValue('item', 'custcol_pm_expected_ship_date', i, docDateHead);
}
}
b
as that your field changed function?
a
Yes.
b
that shouldnt work very well
it would run on every field change
a
Yea, I realized that so I modified the code. Heres the latest:
Copy code
var i;
  var itemLines = nlapiGetLineItemCount('item');
  if (name === 'custbody_document_date'){
for (i = 1; i <= itemLines; i++) {
//var lc = nlapiGetLineItemValue('item', i, 'custcol_pm_expected_ship_date');
nlapiSetLineItemValue('item', 'custcol_pm_expected_ship_date', i, docDateHead);
}
}
}
So this only takes plcae when that field is cahnged. But I just tested again and as soon as I enter a value for that field, the cursor jumps to "add" a new line item.
b
took a while to set that up, i didnt have a ss1 client script setup
i see what you mean about the focus
i dont think that is controlled by suitescript
you can try keeping the fields in a different subtab
so there is nothing to switch to
otherwise i think you have to use normal javascript to change the focus
a
@battk Thank you! I really appreciate you mirroring my code. . . even though its 1.0 haha. Okay, I was not sure what it was called. I'll see if I can research how to change the focus back to the same field. Thank you for your help on this!
@battk I have added the following code and it does nothing. Did I not use it correctly?
Copy code
var setFocus = document.getElementsByName('custbody_document_date');
setFocus[0].focus();
Alternatively, if I set the id to
Copy code
var setFocus = document.getElementsByName('custbody_document_date_fs_lbl_uir_label');
setFocus[0].focus();
I get an error about the focus.
b
the focus might be changed after setLineDate runs
you can try using setTimeout to set the focus at a later time
a
Okay, I will give it a shot. Thank you.
I am having a difficult time identifying the item type. I do not want the loop to skip any description items. When there is a description item, it spits out blank but for other items it will give me the type (e.g. invtPart) Any ideas what the issue is?
Copy code
for (i = 1; i <= itemLines; i++){
			var itemId = nlapiGetLineItemValue('item','item',i);
               nlapiLogExecution('DEBUG','Item ID',itemId);
          	var itemType = nlapiLookupField('item',itemId,'type');
               nlapiLogExecution('DEBUG','Item Type',itemType);

			if	(itemType != 'Description' || itemType != 'Subtotal'){
			nlapiSetLineItemValue('item', 'custcol_pm_customer_requested_date', i, custReqDate);
            }
}
message has been deleted