would anyone know how to set the default value for...
# suitescript
u
would anyone know how to set the default value for a custom dropdown field in a suitelet via SS2? I asked a similar question earlier, but the context now is that a dropdown field's default value has to be set after the custom form's been rendered. The ClientScript's
pageInit
fires without issues (
alert()
triggers fine), but setting the default value of said field using
Field.defaultValue
doesn't seem to work using
currentRecord.getField().defaultValue()
, nor does it work being set via the SL script the field was made.
b
serverWidget.Field.defaultValue is only usable in server suitescript
u
hmmm, this doesn't seem to work. For further context, I created a custom Suitelet form, and I'm running a client script on that SL form by binding it after it's made with
Form.clientScriptModulePath
. To add, I configured the specified dropdown field to be pre-populated through a saved search. I'm not sure if that's interfering with setting its default value, nor am I even sure if I'm setting it right. As I recall,
serverWidget.FieldType.SELECT
selections are a key-value pair, which makes me concerned if I'm setting the values correctly. Thanks for responding regardless.
b
what does the code look like
u
for the SL that generates the form
Copy code
function onRequest(context) {
    if (context.request.method === 'GET') {

      var form = serverWidget.createForm({
        title: 'Generate Statement'
      });

      form.clientScriptModulePath = "SuiteScripts/CRM_GenerateStatementPDF/sl_cs_statementpdf.js";

//omitting irrelevant fields

      var customerfield = form.addField({
        id: 'custpage_customerlabel',
        type: serverWidget.FieldType.SELECT,
        label: 'Customer',
        container: "tab_email"
      });

      var customerSearch = search.load({
        id:"customsearch718"
      }).run().getRange({
        start: 0,
        end: 1000
      });
      for (var i = 0; i < customerSearch.length; i++) {
          var customerName = customerSearch[i].getValue({
              name: "entityid"
          });
          var customerId = customerSearch[i].getValue({
              name: "internalid"
          });
          var newOp = customerfield.addSelectOption({
              text: customerName,
              value: customerId
          });
      }
}
for the CS that gets loaded after the form
Copy code
function pageInit(context) {
		var currentRecord = context.currentRecord;


      	currentRecord.setValue({
          fieldId: "custpage_customerlabel",
          value: "test"
        });
edit: i forgot to include the block of code for the saved search
b
you need to choose a valid select option
there is no select option with a value matching "test"
u
ah, so you're saying I should instead fetch the value I want to set from the options I pre-populated the field with then? Or should I just generate the values from the CS and set it afterwards? I'm understanding your response such that I should replace the contents of `currentRecord.setValue()`'s value field to a variable with a
text
and
value
property, correct?
b
no
the value used with setValue should be a string matching the value used in addSelectOption
similarly, if you wanted text, the text used with setText should be a string matching the text used in addSelectOption
1
n
Reading all of the above I really don't know why, in your SuiteLet, after adding all the select options you cannot just do customerfield.defaultValue = 10; or whatever the internal id of the default value is. I've done this countless times...
1
u
Thanks @battk. I've finally managed to figure it out.
setValue
won't work for the dropdown field, but
setText
with the matching
text
value from the pre-populated entries not only filled in the field, but also matched it to that entry. That's a long headache cleared. Many thanks again. @NElliott I went to try out what you said, and much to my surprise it actually worked. I'm not sure why I never thought of using the internal id to set the default value (I just processed the dropdown field as a text-only) field. It also seems to be much faster than the found solution, so thank you very much for this as well. I'll be keeping the found solution for another problem I'm working on then.
👍 1
563 Views