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

Thanks for reply! but custom field is on sublist. which will not be retrieved through

currRec.getField api.  Because this api is used to get body level field object right? I tried your approach, but its giving blank value in

customDepartmentField log. Also getting same error with line level standard department field. I will appreciate your suggetion of any other approach. 

 

 

Thanks!

 

 

Beginner Answered on October 11, 2023.
Add Comment

Your Answer

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