How to Pass the Value from Widget Controller to On-load Script to set it to Variable field.

Jeck Manalo
Tera Guru

I am trying to create a spModal + Widget.

 

Where if I click Submit, it will pass the value from controller into on-load script to set it to the variable field.

 

HTML code:

<div>
<label>Select Mailbox Type</label>
<select ng-model="c.selected">
<option value="(On-Prem)">CUI User (On-Prem)</option>
<option value="O365">Non-CUI User (O365)</option>
</select>

 

  <br><br>
<button class="btn btn-primary" ng-click="c.submit()">Submit</button>

 

</div>

 

Controller:

api.controller = function() {

  var c = this;

 

  c.selected = '';

 

  c.submit = function() {

    if (!c.selected) {

      alert('Please select a value');

      return;

    }

    alert(c.selected);

    c.close(c.selected); // ✅ pass string back

 

 

  };

};

 

Onload:

 

function onLoad() {

 

var cui_value = g_form.getValue('cui_mailbox_location');

 

spModal.open({

            widget: 'widget_variable',

            backdrop: 'static',

            buttons: [],

        }).then(function(result) {

            alert(result);

            if (result) {

                setTimeout(function() {

                    g_form.setValue('cui_mailbox_location', result);

JeckManalo_0-1781830540497.png

 

6 REPLIES 6

Tejas Adhalrao
Kilo Sage

Hi @Jeck Manalo  ,

Try adding a console.log(result) or alert(result) inside the callback to confirm the value is being returned from the widget:

 

try this 

 

function onLoad() {

spModal.open({
widget: 'widget_variable',
backdrop: 'static',
buttons: []
}).then(function(result) {

if (result) {

g_form.setValue('cui_mailbox_location', result);

console.log('Selected Value: ' + result);
}

});
}

 

 

Catalog Client Script (onLoad)

 

function onLoad() {

spModal.open({
widget: 'widget_variable',
backdrop: 'static',
buttons: []
}).then(function(result) {

if (result) {

g_form.setValue('cui_mailbox_location', result);

console.log('Selected Value: ' + result);
}

});
}

 

 

i just figure out that the result in onload is the OK button in spModal. which cause to {object} {object} value.

Hello @Jeck Manalo ,

 

Try console.log(JSON.stringify(result)) and see whats actual value getting there .

Ankur Bawiskar
Tera Patron

@Jeck Manalo 

what's your actual business requirement here?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader