widget on service portal

suresh101
Tera Contributor

I have created widget on service portal
in the body html template I have added few fields like caller, category, contact type, description and short description along with submit button
if i click the submit  button new tab should be open  incident form with pre filled above details, need to save incident form manually
in the client controller I have added beow script

$scope.submitForm = function () {
        var callData = {
caller_id: $scope.data.caller,
category: $scope.data.category,
contact_type: $scope.data.contact_type,
short_description: $scope.data.short_description,
description: $scope.data.description
        };
 
after this how to write a script for incident open on new tab, prefilled values
12 REPLIES 12

I wrote this in client script , due to that it is not working, how to call this script from server to client

  

 var callerSysId = '';
        var gr = new GlideRecord("sys_user");
        gr.addQuery("name", callData.caller_id);
        gr.query();
        if (gr.next()) {
            callerSysId = gr.getUniqueValue();
        }

@suresh101 

why not make that input as reference type and then no need of GlideRecord?

How to pass the reference value from the HTML to the client controller in the widget 

OR

try to use GlideRecord with callback

Sharing script for 2nd approach

$scope.submitForm = function() {
    var callData = {
        caller_id: $scope.data.caller,
        category: $scope.data.category,
        contact_type: $scope.data.contact_type,
        short_description: $scope.data.short_description,
        description: $scope.data.description
    };

    // Use GlideRecord with a callback
    var gr = new GlideRecord('sys_user');
    gr.addQuery('name', callData.caller_id);
    gr.query(function(response) {
        var callerSysId = '';
        if (response.next()) {
            callerSysId = response.getUniqueValue();
        }

        // Construct the URL with query parameters
        var url = '/incident.do?sys_id=-1' +
            '&sysparm_query=caller_id=' + encodeURIComponent(callerSysId) +
            '^category=' + encodeURIComponent(callData.category) +
            '^contact_type=' + encodeURIComponent(callData.contact_type) +
            '^short_description=' + encodeURIComponent(callData.short_description) +
            '^description=' + encodeURIComponent(callData.description);

        // Open the URL in a new tab
        window.open(url, '_blank');
    });
};

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@suresh101 

Glad to know that my script worked.

Please enhance it further.

If my response helped please mark it correct and close the thread so that it benefits future readers.

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