is there a way to add a custom button to a saved s...
# suitescript
l
is there a way to add a custom button to a saved search ? i need the custom button to trigger a suitelet
n
I don’t think there is. You’re probably better off starting at the suitelet level, displaying your search in a sublist. And adding your button there to trigger some action in the search
l
well that would work but for one saved search. I kinda wanted to make something that works on different saved searches
n
Yeah i think what I would do in that situation is have a 'header' level field on the suitelet that is a list sourced from the savedsearch table. That allows a user to select a search which will dynamically create the sublist with the results. But that's a lot of work. I don't know if you were looking for a quick and easy solution
l
yeah i can do that. its more work but its worth it
n
If it helps, i have this utility function that i wrote to do this. It has some extra stuff in there you probably don't need, but it might help get an idea.
Copy code
const addSublist = ({ form, mySearch, listObj }) => {
  try {
    const searchColumns = mySearch?.columns;
    const searchResults = mySearch?.run().getRange({ start: 0, end: 1000 });
    const { list, customFields, buttons } = listObj;

    // Add sublist
    const sublist = form.addSublist(list);

    // Add custom columns before search columns
    customFields.forEach((field) => {
      if (field.insert === 'before') {
        addSublistColumn({ field, sublist });
      }
    });

    // Add columns to sublist
    for (let i = 0; i < searchColumns?.length; i++) {
      const customDisplayTypes = ['HIDDEN', 'INLINE', 'ENTRY'];
      const field = sublist.addField({
        id: searchColumns[i].name,
        label: searchColumns[i].label,
        type: serverWidget.FieldType.TEXT
      });
      if (customDisplayTypes.includes(searchColumns[i].label)) {
        field.updateDisplayType({
          displayType: serverWidget.FieldDisplayType[searchColumns[i].label]
        });
      }
    }

    // Add custom columns after search columns
    customFields.forEach((field) => {
      if (field.insert === 'after') {
        addSublistColumn({ field, sublist });
      }
    });

    // Add search results to sublist
    let counter = 0;
    for (let i = 0; i < searchResults?.length; i++) {
      if (counter > 100) {
        return;
      }

      for (let column = 0; column < searchColumns?.length; column++) {
        let value = searchResults[i].getText(searchColumns[column]);
        // eslint-disable-next-line no-unused-expressions
        value === null ? (value = searchResults[i].getValue(searchColumns[column])) : null;
        sublist.setSublistValue({
          id: searchColumns[column].name,
          line: i,
          value: value || null
        });
      }

      counter++;
    }

    // add buttons to sublist
    if (buttons.length > 0) {
      buttons.forEach((button) => {
        sublist.addButton(button);
      });
    }
  } catch (e) {
    log.error({ title: 'error in UTIL.addSublist', details: e });
  }
};
💯 1
takes the parameters • form - created with serverWidget.createForm() • mySearch - created with search.create() or search.load() • listObj - A custom object with fields you might want to insert before or after the results of the saved search. This is the stuff you may not need
l
perfect thanks