How to use g_form.submit() in a widget for Service Portal

patricklatella
Mega Sage

I've got a record producer that has a widget that opens up a popup that gathers information from the fields on the form.  Then inside the popup is a button to "Print/Save".  Pressing this button triggers the printer window on the users computer, and also needs to then submit the form to create the record on the destination table.  I've got everything working except for the submit() part.  Anyone see what I'm missing?  Thanks!

Here's the client script in the widget.

function($uibModal, $scope) {
var c = this;

var g_form = $scope.page.g_form;

c.print = function() {

elem = "printMe";
var mywindow = window.open('', 'PRINT', 'height=400,width=600');
mywindow.document.write('<html><head><title>' + document.title + '</title>');
mywindow.document.write('</head><body >');
mywindow.document.write('<h1>' + document.title + '</h1>');
mywindow.document.write(document.getElementById(elem).innerHTML);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
mywindow.print();
mywindow.close();

console.log('hello, record should save');//this shows in the console, but the next line is not submitting the form
g_form.submit();//this should save the record, but it's not

return true;
}
c.openModal = function() {

//pass field values to the modal
$scope.data.reqName = g_form.getDisplayValue('u_requester_name');
$scope.data.dep = g_form.getDisplayValue('u_department');
$scope.data.phone = g_form.getDisplayValue('u_phone_number');
$scope.data.hpc = g_form.getDisplayValue('u_hpc');
$scope.data.date = g_form.getDisplayValue('u_date_of_notary');
$scope.data.delDoc = g_form.getDisplayValue('u_delivery_of_documents');

c.modalInstance = $uibModal.open({
templateUrl: 'notary-print.html',
scope: $scope
});
};

c.closeModal = function() {
c.modalInstance.close();
};

// hide submit button
$("[name='submit']").hide();

}

and here's the code in the HTML script that triggers the "c.print"

<div class="panel-footer text-right">
<button class="btn btn-primary" ng-click="c.print()">${Print &amp; Save}</button>
</div>

1 ACCEPTED SOLUTION

Hi 

 

This worked for Patrick perfectly.

 

$timeout(function() {
$("[name='submit']").trigger('click');
});

 

Thanks.

View solution in original post

5 REPLIES 5

Omkar Mone
Mega Sage

Hi 

Maybe you can make use of spUtil which contains utility methods to perform common functions in a Service Portal widget client script.

 

You need to send the values to server script in widget and then insert records from there.

Go through this :- 

https://community.servicenow.com/community?id=community_question&sys_id=d08fb2e9db58dbc01dcaf3231f96...

 

https://community.servicenow.com/community?id=community_question&sys_id=f3fbf740db399b400e3dfb651f96...

 

Mark correct if it helps.

 

Regards, 

Omkar Mone

www.dxsherpa.com

patricklatella
Mega Sage

Hi Omkar,

thanks for the response...I appreciate the link.

Ended up getting it to work with the following script in the client controller for the widget.

 

$timeout(function() {
$("[name='submit']").trigger('click');
});

 

 

Hi Patrick, 

I hope my answer helped you solve the issue.

Please mark correct answer to close this thread.

Thanks.

Hi Omkar,

yes of course.  If you'd like to reply one more time with the script I used that worked I'll mark your answer correct.   Here again is the script that worked.  Thanks again!

$timeout(function() {
$("[name='submit']").trigger('click');
});