4

I'm trying to figure out the code behind looking at a new Sales Order that has an item called "Repair" and add a second item called "Repair Cost" before User submit. I'm a bit lost and I welcome any help that can be given. I would like this script to be in Javascript and I will attach it to the Sales Order form in Netsuite to run.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Randomlord
  • 51
  • 1
  • 1
  • 3

2 Answers2

3

Here is one sample solution:

We will still assume that the items internal ids are Repair = 100 and Repair Cost = 200

function recalc(type)
{
    if(type == 'item')
    {
        var itemId = nlapiGetCurrentLineItemValue('item', 'item'); //Get the Item ID
        if(itemId == 100) //Repair Cost
        {
                //Insert item
                nlapiSelectNewLineItem('item');
                nlapiSetCurrentLineItemValue('item', 'item', 200); //Repair Cost
                nlapiSetCurrentLineItemValue('item', 'quantity', 1);
                nlapiSetCurrentLineItemValue('item', 'amount', '0.00');
                nlapiCommitLineItem('item');
        }
    }
    return true;
}

Deploy this as client-side code and make sure that the function is Recalc.

To learn more about client side script: https://system.na1.netsuite.com/help/helpcenter/en_US/Output/Help/SuiteFlex/SuiteScript/SSScriptTypes_ClientScripts.html#1016773

eliseobeltran
  • 568
  • 3
  • 11
  • I really appreciate your work on this but I get this error: An unexpected error occurred in a script running on this page. recalc (recalc) customform UNEXPECTED_ERROR (id=he7iqv95g6hfigxhujc7) An unexpected error has occurred. Please click here to notify support and provide your contact information. I have this listed in the recalc function on the form. – Randomlord Mar 12 '13 at 20:36
  • Nevermind. That error is because of the extra LineItemValue code I added. All good. Thank you! – Randomlord Mar 12 '13 at 20:52
1

First thing you need to do is to get the internal id of the item "Repair" and "Repair Cost".

In this example, let's assumed that the internal id of Repair = 100 and Repair Cost = 200

Here is th code:

function afterSubmit(type)
{
    if(type == 'create' || type == 'edit')
    {
        var record = nlapiLoadRecord(nlapiGetRecordType(), nlapiGetRecordId()); //Load the record

        //Loop to all sublist item
        var count = record.getLineItemCount('item');
        for(var i = 1; i <= count; i++)
        {
            var item = record.getLineItemValue('item', 'item', i); //This will return the internal id of the item
            if(item == 100) //Item is equal to 100; insert one item
            {
                record.insertLineItem('item', i);
                record.setLineItemValue('item', 'item', i, 200); //Repair Cost internal id
                record.setLineItemValue('item', 'quantity', i, 1); //You should put some quantity; depending on your account setup all required fields should be set here.
            }
        }

        //Submit the changes
        nlapiSubmitRecord(record, true);
    }
}

To understand the suitescript API and the fields exposed to sales order check on this Netsuite helpguide:

https://system.netsuite.com/help/helpcenter/en_US/RecordsBrowser/2012_2/Records/salesorder.html

eliseobeltran
  • 568
  • 3
  • 11
  • So I put this script in my Netsuite account and added it to my Sales Order form and I got the error: An unexpected error occurred in a script running on this page. additemtorepairSO.js(fieldChange) customform. JS_EXCEPTION ReferenceError additemtorepairSO is not defined. Any thoughts? – Randomlord Mar 12 '13 at 14:22
  • I'm wondering if this has to do with any required fields. I'll mess with the required fields and reply back. – Randomlord Mar 12 '13 at 14:24
  • How did you deploy it? This should be deployed as user-event script in sales order record. – eliseobeltran Mar 12 '13 at 14:24
  • I see so you wanted to do it in client-side. Add a new line item with specified sku whenever a specific sku is added. The code that I showed is for user-event script. I'll get back to you with a client-code. – eliseobeltran Mar 12 '13 at 14:25
  • "additemtorepairSO.js(fieldChange) customform. JS_EXCEPTION ReferenceError additemtorepairSO" - With regards to this error, it means that your function is not defined in the deployment. – eliseobeltran Mar 12 '13 at 14:27
  • Thanks, I appreciate it. I forgot to mention this would run client side. :) – Randomlord Mar 12 '13 at 16:13