Friday, July 5, 2019

Accessing the Most Recent Child Record on the Parent Record via SuiteScript

Sometimes there are multiple records attached to a single record. For some instances, we would intend to get the most recently created record.

To get  the most recent custom child record on a parent record via SuiteScript, below is a sample snippet of code.

//gets the number of child records
var lineCount = nlapiGetLineItemCount('recmachcustrecord_issue_child_title');
var recent = 0;
for (i = 1; i <= lineCount; i++)
    //gets the id # of the current selected child record
    temp = nlapiGetLineItemValue('recmachcustrecord_issue_child_title', 'id', i);

    //comparison of values for the id #'s which determines the most recent record
    if (recent < temp)
        recent = temp;
        recent = recent;

//loads the most recent child record in view mode
nlapiSetRedirectURL('RECORD', 'customrecord_issue_record_history', recent);

The above code was used on a user event script deployed on the Issue record.

The parameter 'recmachcustrecord_issue_child_title' is the id of the Title field of the custom child record linked to the parent record.

The parameter 'customrecord_issue_record_history' is the id of the custom child record type.

The above snippet of code gets the most recent child record based on the id # of the child records. The higher the number, the most recent the record is. I then used the most recent id # to load the custom child record in view mode.

DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Netsuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.

Netsuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Netsuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.

Netsuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.

No comments:

Post a Comment