Pass additional parameters to embedded default form widget

brendanhamman
Giga Contributor

I have a portal page with a self-made selection list, that calls the system default form widget after page load, and based on what you click.

//works as expected
var taskOptions = {
        table: input.table,
        sys_id: input.selected,
        view: 'spview',
      };
data.form = $sp.getWidget('widget-form', taskOptions);

I can call the form correctly, but I need to be able to set default values based on the page that is loaded (such as assigned_to, caller, etc). If it was elsewhere on the platform, I could use URL sys_parm parameters, but widget-form doesn't look for URL params if called with $sp. I could also use onload client scripts on the form table to look for URL params, but as this is in a scoped application, and on service portal - I cannot use document, window, $sp, jquery, or angular in client scripts on service portal. 

I have been messing around with the form widget, and cannot get it to accept additional parameters to set fields on load.

//does not work
var taskOptions = {
        table: input.table,
        sys_id: input.selected,
        view: 'spview',
        sys_parm: 'encoded_query'
      };
data.form = $sp.getWidget('widget-form', taskOptions);

Anyone have this problem and solve it?

4 REPLIES 4

brendanhamman
Giga Contributor

Welp, after messing around with this for a couple hours, I came back to it after a 10 minute break and figured it out. 

 

In the form widget, it looks for:

data.query = $sp.getParameter("query") || options.query;

and it accepts the encoded query method above. 

I'm trying to do the same thing. Could you tell me how you wrote your encoded query, because mine's not working. What I've got:

 

{
table: '[table]',
sys_id: '-1',
query: '[fieldName]=[value for field]'
}

 

I've even tried 'sysparm_query=[fieldName]=[value]', but that doesn't work either. It does work when I try to use the encoded query in a URL.

Hi

I also couldn't get the OOB form widget to accept this query parameter when embedding it via client script.

I think that when you embed the widget from client script the object you pass with parameters is accepted as the input object rather than options object in the resulting embedded widget.

I cloned the OOB widget and made the following changes and now the embedded widget accepts the query input value:

Updated widget-form

Starting from line 23 of the original OOB widget server script

Changes commented #1 #2 #3

if (input) {
		data.table = input.table;
		data.sys_id = input.sys_id;
		data.view = input.view;
		//#1 set data.query to input.query if input
		data.query = input.query
		var result = {};
		if (input._fields) {
			result = $sp.saveRecord(input.table, input.sys_id, input._fields);
			data.sys_id = result.sys_id;
		}

		if (input.sys_id == '-1')
			data.isNewRecord = true;
    		if (input.isPopup === true)
			isPopup = true;
	} else {
		data.table = options.table || $sp.getParameter("t") || $sp.getParameter("table") || $sp.getParameter("sl_table");
		data.sys_id = options.sys_id || $sp.getParameter("sys_id") || $sp.getParameter("sl_sys_id");
 		if (!data.sys_id && options.sys_id_required != "true")
			data.sys_id = "-1";
		data.view = options.view || $sp.getParameter("view") || $sp.getParameter("v"); // no default
	        //#2 set data.query from parameter or options if no input
                data.query = $sp.getParameter("query") || options.query || "";
}
        //#3 comment out the original line setting data.query regardless of input
        //data.query = $sp.getParameter("query") || options.query || "";

 

I don't know if there is another way to get the oob widget to accept the query parameter when you are embedding the form widget. It does work when the query is in the URL, but if you are using the form in a modal window for example, I think you'll have to clone the widget.

Thanks

Rob

Hi !

Just to let you know, even if your modifications work effectively, there is a way to use the OOB widget (if you have access to the HTML).

 

Server script method

You're using in your Server script

var taskOptions = {
    table: input.table,
    sys_id: input.selected,
    view: 'spview',
    query: 'your_encoded_query'
};

data.form = $sp.getWidget('widget-form', taskOptions);

Then I assume your HTML look like 

<sp-widget widget="data.form" />

the method getWidget use 2 parameters : the widget, and an object which will be the INPUT of the embedded widget.

 

Client script method

If you use spUtil.get() in Client script to embedded a widget, the object will be the INPUT too, like you said.

If you use spModal() it will also be the INPUT (but you open the widget in a Modal, so it's not embedded).

e.g. :

spModal.open({
    title: 'My embedded widget',
    widget: 'widget-form',
    widgetInput: c.data.taskOptions,
    size: 'lg',
}).then(function(name) {
})

 

HTML method

Actually, there is another way to embed a widget, since you built the page yourself, you can edit the HTML.

HTML :

<widget id="widget-form" options=data.taskOptions></widget>

The server-side object :

data.taskOptions = {
    table: input.table,
    sys_id: input.selected,
    view: 'spview',
    query: 'your_encoded_query'
};

This way, the taskOptions will be the OPTIONS of the Embedded widget. So you don't have to create a custom one.

 

NB for duplicate method : I didn't try, but you should be able to do something like this at #3, if you still want to copy the widget :

data.query = $sp.getParameter("query") || input.query || options.query || "";

Surprising enough they didn't think about adding the INPUT entry for the query ...

 

Hope this will help !