Frage

I've looked all over but can't seem to find how to modify an existing record using Suitescript. Essentially I want to be able to set a field on the record that initiates the script. For example, when I view a custom record, Potential Resource, and save it - it initiates the script (which does happen); but how do I get the information from the record that initiated the script?

Or am I going about this the wrong way?

-- This is what I have in my .js

function getMaxEmployeeNumber(){
var record = nlapiLoadRecord(nlapiGetRecordType()), nlapiGetRecordId());
record.setFieldValue('Last Name', 'Set from script');
}

I don't know if this helps but in NetSuite I've done the following: 1. Created a new User Event script called "Employee Number Creation" 2. Created a script file that has one function in it (above) and added it to my script under the "Scripts" tab. 3. Created a deployment that applies to a custom record (Potential Resource)with a status of "Testing" and an event type of "Edit".

War es hilfreich?

Lösung

There are several ways to hook a script up to "saving" a record. You can use a client script to attach to the "Save Record" event, which allows you to validate the record and prevent it from saving if necessary.

You can also use a User Event script and attach to either the "Before Submit" event, where you can modify any fields before the record gets to the database, or to the "After Submit" event, where you can take actions on other records after the record is committed to the database.

In either script, you can use the nlapiGetFieldValue and related functions to retrieve information from fields on the record, and nlapiSetFieldValue and related functions will update fields on the record.

Edit: Search the NetSuite Help for "Record API" to see the documentation for all record-related functions you can use.

Andere Tipps

BeforeLoad works well for doing things like adding custom buttons to the form. Surely, you don't want to change the actual record before it gets displayed to the user.

Your setFieldValue function call needs to reference the ID of the custom field on the custom record. The fields are called something like "custrecord_whatever_you_named_it". Best practice is to enter "_whatever" in the ID field when creating the custom field. For example, "_last_name". If you had a field with this ID, your script would look like this:

record.setFieldValue('custrecord_last_name', 'Set from script');

Also, if you are using a User Event script that initializes a value (like setting an ID), you don't need to load the record. Just use this in a BeforeLoad function:

nlapiSetFieldValue('XXXXX', 'YYYYY');

Many ways to set a field.

For client side you can use durectly nlapiSetFieldValue.

Or server, you can load the record by either rec=nlapiGetNewRecord or rec=nlapiLoadRecord, and then set the field with rec.setFieldValue, nlapiSubmit(rec).

Something I've found works best in most cases is nlapiSubmitField, to do a direct edit. Is the operation that tends to fail the least.

You have to submit the record once you have modified it. You can submit the record using the function nlapiSubmitRecord()

Here's the sample code.

function getMaxEmployeeNumber()
{
    var record = nlapiLoadRecord(nlapiGetRecordType()), nlapiGetRecordId());
    record.setFieldValue('Last Name', 'Set from script');
    nlapiSubmitRecord(record, true);
}

You missed submitting the record that's why the values you set did not reflect.

See nlapiSubmitRecord() API

var record = nlapiLoadRecord(nlapiGetRecordType()), nlapiGetRecordId());

This line has an extra parenthesis and won't even execute without throwing an error. Correct it to this as well as submitting the record when you are done.

var record = nlapiLoadRecord(nlapiGetRecordType(), nlapiGetRecordId());

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top