Applying logic to button Make Copy - Netsuite - netsuite

I just started with Netsuite and Suitescript 2.1. I am wondering if I can apply any logic to the "Make Copy" button via suitescript.
The make copy button is on the opportunity > sublist Item > Line item.
Examples of what I want to do:
Reset certain fields of the copied line
Fill in certain fields of the copied line (with a certain logic
behind it)

I already found a solution. I use the validateLine function. When making a copy of a lineitem in Netsuite the lineitem field: "line" has an empty string value until the line is created by clicking the "Add" button or clicking outside of the copied lineitem. I will check if that field is empty, if yes it means the line is a copy. Afterwards I can also set a custom field isCopy = true.
const lineIsCopied = () => {
let lineNumber = opportunity.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'line'
});
if (lineNumber) {
//linenumber is existing this means line is not a copy
return false;
} else {
//linenumber is empty this mean line is a copy
return true;
}
};

Try the 'validateInsert' within a Client Script.
Make sure your function return 'true' for a valid insert, otherwise any insert for any sublist won't succeed.

Related

How to move sublist fields created from User Event script

I added a sublist field on a transaction. For instance a Sales Order. When you add the field via user event script, it positions the field at the end of the sublist. Is there a way to position the field? This can't be accomplished in the UI as the field is added via script. It's a select type field, so I am trying to modify the list values with client script and can only do that if the field is created in my user even script. My code works perfectly fine, it's just the field is at the end of the sublist line(far right and have to scroll). I am using SuiteScript 1.0 but am open to using 2.0 if I need to.
This is for suitescript 2.0
This has sort of turned into two questions so I will give an answer for one and then the solution, for clarity. The short answer is you cannot move a sublist field when created in a user event (however there is a solution to that.)
you can add a sublist field by getting the sublist and using the sublist.addField method.
function beforeLoad(context){
var form = context.form;
var sublist = form.getSublist({
id : 'item'
});
sublist.addField({
id: 'fieldid you want to use for script reference ect.',
type: serverWidget.FieldType.CHECKBOX, //any supported TYPE
label: 'Label users will see in sublist'
});
}
If done this way, there will be a new column on the end of the items sublist that cannot be moved.
To be able to move the location of the column do the following.
Navigate to
Customization
Lists, Records, & Fields
Transaction Line Fields
add a new field and apply to the sublist you want it used on.
you can assign and id which will be prefixed with custcol so start your id with an _ and give it a name.
Once this is complete you can access the field by id and change the value in your user event script before load
function beforeLoad(context){
var form = context.form;
var sublist = form.getSublist({
id : 'item'
});
sublist.setSublistValue({
id: 'customcol_id_created_in_ui',
line: 1, //line you want to access if needing to set all you will have to loop through and set each one.
value: "your value"
});
}
If you go to the API reference and look at the ui/serverWidget module and navigate to sublist you can find all of the methods and options for manipulating sublist there.
In SuiteScript 2.x you would use the N/ui/serverWidget module, create the field and then use the Form.insertField(options) method, passing in the field you created as options.field and the existing field which you want to insert your field before as options.nextfield. Note that Form refers to the form object passed to the user event script in the scriptContext.
i don't believe there is a SS1.0 equivalent.

Label gets selected on clicking enter key in the tabulator cell

I have a js fiddle to show the issue that I am facing.
Tabulator 'email' column has freetext set to true to allow the user to set the value of the cell to a free text entry as follows:
editorParams:{
values:{
"steve#boberson.com":"steve",
"bob#jimmerson.com":"bob",
"jim#stevenson.com":"jim",
"harvy#david.com":"Harvy",
"ken#thompson.com":"Ken",
"denny#beckham.com":"Denny"
},
freetext:true,
searchFunc:function(term, values){
console.log("term and val "+term +" \n"+values);
var matchedEntries = {};
var matchFound = false;
for(var key in values) {
if(key.includes(term)){
matchFound = true;
matchedEntries[key] = values[key];
}
}
if(matchFound){
return matchedEntries;
}
else{
return {term: term};
}
}
}}
Select one of the values from the selector list displayed, say steve, value being 'steve#bobberson.com'.
Then after the value is selected, click on the same tabulator cell, the value displayed suddenly changes to the label.When user clicks enter key now, the label is selected instead of the value,.ie. steve will be displayed instead of steve#bobberson.com
If this is not a bug, how do I make sure that the value steve#bobberson.com remains selected after clicking enter. This is because freetext option is set to true in the editorParams of the column
Attached a gif showing the issue
That is not a bug,
With freetext enabled, users are able to enter any text they like in that field and it will be saved which is exactly what is happening in the example
If they select the email address again from the list then it is correctly saved.
It is an either/or scenario, you can either restrict the users to selecting specific values from the list OR you can allow them freetext in which case anything goes.
If you want a different behaviour then you can always build a custom editor that works in the way you want. Checkout the Custom Editor Documentation for more information

Copy previous value in a sublist field

I'm trying to add a field to item sublist on sales order record that copies the original value of another field when ever it is changed. It pretty much preserves the previous value of another field. When ever I do a nlapiGetCurrentLineItemValue at Validate field trigger in my client script, it is giving me the new value(changed by user) not the one before changing it. Is there a way to get the value of a field before it is edited at validatefield event in client script? or by any other ways?
function validateFieldChanged(type, name, linenum) {
if (type == 'item' && name == 'custcol_commit_date') {
nlapiSetCurrentLineItemValue('item', 'custcol_last_commit_date', nlapiGetCurrentLineItemValue('item', 'custcol_commit_date'), true,true);
}
return true;
}
I've previously done something similar.
At the top of your script, declare a variable such as var existingValue;
Then in your lineInit function, get the field value and store it in existingValue.
Then when you have your validateField, you compare the be value with the existingValue.

NetSuite Suitescript 2.0 How can I access custom item options records?

I have a problem that I'm trying to solve in a roundabout way since the straightforward way didn't work.
The task: Retrieve the customizable item options off of a purchase order item (using SuiteScript 2.0).
The partial solution: Currently, I extract the itemId off the PO using
po.getCurrentSublistValue({ sublistId: 'item', fieldId: 'item' });
Then I load the item and extract the item options IDs and labels using
var optionIds = item.getValue({ fieldId: 'itemoptions' });
var optionLabels = item.getText({ fieldId: 'itemoptions' });
For item options that aren't List/Record type, (like freeform text personalization), I can then loop through the item options and extract their text off of the PO item using the following code.
for (var j = 0; j < optionIds.length; j++) {
var option = po.getCurrentSublistText({
sublistId: 'item',
fieldId: optionIds[j].toLowerCase()
});
if (option !== null) {
log.debug({title: 'option found', details: optionLabels[j] + ': ' + option});
}
}
The issue: When the item option is a List/Record type, both getCurrentSublistText and getCurrentSublistValuereturn the internal ID of the list selection. For instance, if I have a custom item option for Shirt Color, and it uses the custom list Color, with Red being internal ID 2, then if my PO has an item with red shirt color on it, the option label will be "Shirt Color", but instead of "Red," the option will be 2, the internal ID from the Color list. I have no idea why getCurrentSublistText doesn't work for this use case, but I've accepted it, and I'm looking for a workaround.
My thought was, since the item option is a record in the UI (my field explorer extension says it has recordType:"itemoptioncustomfield"), I could just load it up using the option ID that I have, and if it has fieldtype:"SELECT", I would get the internal ID of the List/Record selection (fieldId: selectrecordtype) then use search.lookupFields to get the actual value of my item option, instead of the internal ID. The only issue is, I can find no way to load (or search on) an item option. The record type does not seem to exist in SuiteScript (I believe in Suite Talk it does though). Is this a possible task? Even if my workaround can't work, is there another better workaround that I'm not seeing?
Thanks in advance, I'm at a loss here.
Edit: Just to add further attempts, I found the Field.getSelectOptions() function today which looked really promising, since it would list all the select options of the item option and their values I would think. I tested it out on some other more basic fields on the order and it worked, however when I tried using getCurrentSublistField on the item option field (just as I used getCurrentSublistValue and getCurrentSublistText, the field returned was null, so looks like that is another dead end.

Disable line item Fields on Custom Transaction Form

1)Want to disable line item fields on Custom Transaction form.
Below is the snippet I tried & applied to the form's custom code on LINE INIT FUNCTION function but it doesn't work. Does it work or is it a bug or something I am missing here.Please suggest
function lineInit(){
if(type == 'item'){
var itemDisFields = ['description','location'];
for(var i = 0; i < itemDisFields.length; i++){
nlapiDisableLineItemField('item',itemDisFields[i] , true);
}
}
}
2)How to freeze Add button in the line level.
Sublist indices in SuiteScript 1.0 start at 1, not 0, so you'll need to start i at 1.
I am not exactly sure what you mean by "freeze" the Add button, but there is no supported way to do this. You could of course hack it with jQuery. The best supported way to prevent a line from being added is to use a validateLine handler and return false from it when the line should not be added.

Resources