RE: How to add options in sublist select type custom field?

I have created custom select type department field on a journal entry’s sublist through user event before load script.  I am getting context.mode value as blank. Now I  am getting departments using field lookup from account record’s department field which is multiselect type. I am trying to insert those departments into journal entries custom department field using client script validate field event. But its adding same options on all lines of custom department field. Also When I try to add account on new line,then I am getting an error as “”type”:”error.SuiteScriptError”,”name”:”SSS_INVALID_SUBLIST_OPERATION”,”message”:”CurrentRecord.getSublistField”,”id”:null,”stack”:[“SSS_INVALID_SUBLIST_OPERATION: CurrentRecord.getSublistField”,” at Object.createError [as nlapiCreateError]

Please help!!

/**
 *@NApiVersion 2.1
 *@NScriptType ClientScript
 */
 define([‘N/record’, ‘N/search’, ‘N/ui/dialog’],
 function (record, search, dialog) {
function validateField(context)
{
  try{
  var currRec = context.currentRecord;
        var sublistName = context.sublistId;
        log.debug(“sublistName”,sublistName);
        var sublistFieldName = context.fieldId;
        log.debug(“sublistFieldName”,sublistFieldName);
        log.debug(“context–>”,context);
        if (sublistName === ‘line’ && sublistFieldName === ‘account’)
        {
          alert(“validate field”);
            //Get account Value
    var accountid = currRec.getCurrentSublistValue({
      sublistId:’line’,
      fieldId:’account’
    })
    log.debug(“accountid”,accountid);
    var currIndex = currRec.getCurrentSublistIndex({
      sublistId: ‘line’
    });
    log.debug(“currIndex–>”,currIndex);
    var objField = currRec.getSublistField({
        sublistId: ‘line’,
        fieldId: ‘custpage_department’,
        line: currIndex
    });
    log.debug(“objField”,objField);
    //objField.removeSelectOption({ value: null });
    var fieldLookUp = search.lookupFields({
    type: search.Type.ACCOUNT,
    id: accountid,
    columns: [‘custrecord_cp_account_restrict_dept’,’custrecord_cp_dept_selection’]
    });
    var restrict=fieldLookUp.custrecord_cp_account_restrict_dept;
    log.debug(“restrict:”,restrict);
    if(restrict==false)
    {
      objField.removeSelectOption({ value: null });
    var restrictDept=fieldLookUp.custrecord_cp_dept_selection;
    log.debug(“restrictDept:”,JSON.stringify(restrictDept));
    if(restrictDept.length>0)
      {
    for(var j=0;j<restrictDept.length;j++)
    {
    log.debug(“restrictDept value—>”,restrictDept[j].value);
    objField.insertSelectOption({
    value : restrictDept[j].value,
    text : restrictDept[j].text
    });
  }
}
}
        }
        if (sublistName === ‘line’ && sublistFieldName === ‘custpage_department’)
        {
          alert(“validate field2”);
            //Get account Value
            var customDepartment=currRec.getCurrentSublistValue({
              sublistId: ‘line’,
              fieldId: ‘custpage_department’,
                });
                log.debug(“customDepartment validatefield”,customDepartment);
      currRec.setCurrentSublistValue({
        sublistId: ‘line’,
        fieldId: ‘department’,
        value:customDepartment
          });
        }
      }catch(e)
      {
        log.error(“Error occurred: “, e);
      }
      return true;
}
   return {
     validateField:validateField
   };
 });

 

 

 

Maira S Beginner Asked on October 11, 2023 in SuiteScript.
Add Comment
3 Answers

The error message “SSS_INVALID_SUBLIST_OPERATION: CurrentRecord.getSublistField” indicates that you are trying to use the CurrentRecord.getSublistField() method on a sublist that does not exist. This can happen if you are trying to access a sublist on a record that is not a sublist record, or if the sublist name is incorrect.

In your case, you are trying to use the CurrentRecord.getSublistField() method to access the custpage_department field on the line sublist. However, the custpage_department field is not a sublist field. It is a custom field that you have added to the journal entry record.

To fix this error, you need to remove the CurrentRecord.getSublistField() method from your script. You can then access the custpage_department field directly using the CurrentRecord.getField() method.

Here is an updated version of your script that fixes the error:

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 */
define(['N/record', 'N/search', 'N/ui/dialog'],
function (record, search, dialog) {

function validateField(context) {
  try {
    var currRec = context.currentRecord;
    var sublistName = context.sublistId;
    var sublistFieldName = context.fieldId;

    if (sublistName === 'line' && sublistFieldName === 'account') {
      // Get account value.
      var accountid = currRec.getCurrentSublistValue({
        sublistId: 'line',
        fieldId: 'account'
      });

      // Get the account's department restrictions.
      var fieldLookUp = search.lookupFields({
        type: search.Type.ACCOUNT,
        id: accountid,
        columns: ['custrecord_cp_account_restrict_dept', 'custrecord_cp_dept_selection']
      });

      var restrict = fieldLookUp.custrecord_cp_account_restrict_dept;
      var restrictDept = fieldLookUp.custrecord_cp_dept_selection;

      // If the account's department restrictions are not enabled, then populate the
      // custom department field with the restricted departments.
      if (!restrict) {
        // Get the custom department field.
        var customDepartmentField = currRec.getField({
          fieldId: 'custpage_department'
        });

        // Remove all existing options from the custom department field.
        customDepartmentField.removeSelectOption({ value: null });

        // Add the restricted departments to the custom department field.
        for (var j = 0; j < restrictDept.length; j++) {
          customDepartmentField.insertSelectOption({
            value: restrictDept[j].value,
            text: restrictDept[j].text
          });
        }
      }
    } else if (sublistName === 'line' && sublistFieldName === 'custpage_department') {
      // Get the custom department field value.
      var customDepartment = currRec.getFieldValue({
        fieldId: 'custpage_department'
      });

      // Set the department field value to the custom department field value.
      currRec.setFieldValue({
        fieldId: 'department',
        value: customDepartment
      });
    }
  } catch (e) {
    log.error('Error occurred: ', e);
  }

  return true;
}

return {
  validateField: validateField
};
});

This updated script will now populate the custom department field with the restricted departments when the user enters an account on a new line. It will also set the department field value to the custom department field value when the user validates the custom department field.

Beginner Answered on October 11, 2023.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.