4

I am working in ServiceNow and am creating a widget that pulls up a modal window with a form embedded in it. I want to pre-populate some of the fields in the modal form, but am unsure how to do this.

here is my HTML of a button that opens up the modal window:

<div>
    <input class="btn btn-support" ng-click="c.onbSupport()" type="button" value="Ask a Question">
</div>

my client script looks like this:

function($scope,spModal) {
  /* widget controller */
  var c = this;

        c.onbSupport = function(){
        spModal.open({
            title: 'Submit Your Question',
            widget: 'form-new',
            widgetInput: {table: 'support_tickets'},
            buttons: []
        }).then(function(){
    })      
    }
}

and finally, here is my server script:

    var usr = gs.getUserID();
    var gr = new GlideRecord('info');
    gr.addQuery('opened_for', usr);
    gr.query();
    if(gr.next()) {
        data.parent = gr.getValue('number');
        data.short_description = gr.getValue('short_description');
    }

In the modal form, I have two fields (parent_case and category) that I would like to be pre-populated with data.parent and data.short_description respectively. To pass the server script value into HTML, I know you can do {{data.parent}}. However, how do I get those values into the client script that generates the modal form?

Dave
  • 1,257
  • 2
  • 27
  • 58

2 Answers2

1

You need to pass the values to client script and "catch".

I am assuming you are trying to pass multiple values, So you need an array to keep values and pass to client script side.

Server script:

var records=[]; //define array first
var usr = gs.getUserID();
var gr = new GlideRecord('info');
gr.addQuery('opened_for', usr);
gr.query();
if(gr.next()) {
    var rec={} //define a record
    rec.parent = gr.getValue('number');
    rec.short_description = gr.getValue('short_description');
    records.push(rec); //populate array with records
}
data.records=records; // you need to assign your array as data

Client Script:

function($scope,spModal) {
  /* widget controller */
  var c = this;
  var infos=c.data.records;//this is the "catch" part
}

Note:I haven't tested this code.

alperzzz
  • 53
  • 5
0

@alperzzz delivered the right approach. To handover your data, you have to use the "shared" attribute to share your record(s) with your embedded widget. Within your widget you can populate your shared data. You can find all attributes in the docs

t3chnico
  • 64
  • 6