Widget Buttons to insert the record

Kusuma2
Kilo Guru

Hi All,

I am new to Service Portal Development so I need all your help.

My requirement is, I need to create two buttons on Catalog item form.find_real_file.png

 

As a part of my requirement I have cloned the OOB  widget(Sc- catalog Item) and added the two buttons in the html part as shown in above screen shot.

So when user clicks on the Save as a Template button, a new record should get inserted in the custom table(u

-My templates) and the Current Catalog name should get copied to custom table reference field.

How can I insert the new record in custom table when user clicks on Save as Template Button.

<p style="float: left; width: 130px"><button type="button" class="btn btn-primary btn-block" ng-click="c.uiAction('resolve')">Save as a Template</button>

<p style="float: right; width: 130px"><button type="button" class="btn btn-primary btn-block" ng-click="c.uiAction('cancel')">Saved for later</button>

1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

There's probably going to be a lot more to this that will need to be asked in other forum posts, but here's how you can create a button to insert a record.  You can create this as its own standalone widget if you want and then just add that widget to the existing 'sc_cat_item' portal page.  The only things you'll need to change below are in the 'Server script' section.  You'll need to change 'u_custom_table' to the table name where you want to insert the record and 'u_catalog_item' to the Catalog item reference field on that table.  Here are the settings...

Body HTML template:

<div class="panel panel-default">
 <div class="panel-heading">Templates</div>
 <div class="panel-body">
 <button type="button" class="btn btn-primary btn-block" ng-click="c.uiAction('saveAsTemplate')">Save as Template</button>
 </div>
</div>

Server script:

(function() {
	
	// Get table & sys_id
	data.sys_id = input.sys_id || $sp.getParameter("sys_id");
	
	// Initialize GlideRecord
	var gr = new GlideRecord('u_custom_table');
	gr.initialize();
	if (input && input.action) {
		var action = input.action;
		
		if (action == 'saveAsTemplate') {
			// Create template record
			gr.u_catalog_item = data.sys_id;
			gr.insert();
		}
	}
	
})();

Client controller:

function() {
  var c = this;
	c.uiAction = function(action) {
		c.data.action = action;
		c.server.update().then(function() {
			c.data.action = undefined;
		})
	}
}

 

View solution in original post

3 REPLIES 3

Mark Stanger
Giga Sage

There's probably going to be a lot more to this that will need to be asked in other forum posts, but here's how you can create a button to insert a record.  You can create this as its own standalone widget if you want and then just add that widget to the existing 'sc_cat_item' portal page.  The only things you'll need to change below are in the 'Server script' section.  You'll need to change 'u_custom_table' to the table name where you want to insert the record and 'u_catalog_item' to the Catalog item reference field on that table.  Here are the settings...

Body HTML template:

<div class="panel panel-default">
 <div class="panel-heading">Templates</div>
 <div class="panel-body">
 <button type="button" class="btn btn-primary btn-block" ng-click="c.uiAction('saveAsTemplate')">Save as Template</button>
 </div>
</div>

Server script:

(function() {
	
	// Get table & sys_id
	data.sys_id = input.sys_id || $sp.getParameter("sys_id");
	
	// Initialize GlideRecord
	var gr = new GlideRecord('u_custom_table');
	gr.initialize();
	if (input && input.action) {
		var action = input.action;
		
		if (action == 'saveAsTemplate') {
			// Create template record
			gr.u_catalog_item = data.sys_id;
			gr.insert();
		}
	}
	
})();

Client controller:

function() {
  var c = this;
	c.uiAction = function(action) {
		c.data.action = action;
		c.server.update().then(function() {
			c.data.action = undefined;
		})
	}
}

 

Thanks for the Reply it was really Helpful.

There is additional thing to work. I need to insert the variable values as well into the custom table as well.

My question is how to get the Variable values on the widget when Click on the button. 

find_real_file.png

 

Thanks,

Ajay

 

 

 

Ted19
Tera Expert

HeyAjay, 

 

did you happen to find the answer? to get the variable from the form ?