Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Service Portal Widget checks if there is already an existing record

JuliaHowells
Tera Expert

On the Portal, we added a widget on Major Incidents that allows the user to create a child incident.

 

We would like to add functionality that will only allow a user to create one child Incident on a Major Incident, so if they already have a child incident on the open Major Incident, they cannot create another one. 

If they try to click the button again, we want to have a message pop up that tells them "Thank you for reporting, you have previously been added to the incident" and provide a link to take them to the My Requests on the Service Portal so they can see their individual ticket: 

 

/esc?id=my_requests

 

 

<button type="button" class="btn btn-primary btn-block" ng-click="sysverb_child_new()">Add me to this issue</button>
// Server Script
(function() {

    // Get table & sys_id
    data.sys_id = input.sys_id || $sp.getParameter("sys_id");
    //input
    if (input && input.action == 'sysverb_child_new') {
        var sr = new GlideRecord('incident');
        if (sr.get(data.sys_id)) {

            var jr = new GlideRecord('incident');
            jr.initialize();

            jr.setValue('incident_state', 'Assigned');
            jr.setValue('state', 9);
            jr.setValue('assigned_to', sr.assigned_to);
			jr.setValue('assignment_group', sr.assignment_group);
            jr.setValue('short_description', sr.short_description);
            jr.setValue('description', sr.description);
            jr.setValue('caller_id', gs.getUserID());
            jr.setValue('parent_incident', data.sys_id);
            jr.setValue('u_on_behalf_of', gs.getUserID());
            jr.setValue('u_callback_number', gs.getUser().getRecord().getValue('phone'));
            jr.setValue('location', gs.getUser().getRecord().getValue('location'));
            jr.setValue('contact_type', 'self-service');
            jr.setValue('category', sr.category);
            jr.setValue('subcategory', sr.subcategory);
            jr.setValue('impact', sr.impact);
            jr.setValue('urgency', sr.urgency);
            var sysIdTemp = jr.insert();
        }
    }
})();
// client controller
api.controller = function($scope,spUtil) {
    var c = this;
    $scope.sysverb_child_new = function() {
        var clientObj = {action:'sysverb_child_new'};
        c.server.get(clientObj).then(function(response) {
            alert("You've been added to this Incident"); // This will give you an alert with ok button
			$sp.getWidget();
            c.data = response.data;
        })
    }

};

 

 

Any ideas on how to do this? 

1 ACCEPTED SOLUTION

James Chun
Kilo Patron

Hi @JuliaHowells,

 

Haven't tested this but try the following:

 

Server script:

// Server Script
(function() {

    // Get table & sys_id
    data.sys_id = input.sys_id || $sp.getParameter("sys_id");
    //input
    if (input && input.action == 'sysverb_child_new') {
        var sr = new GlideRecord('incident');
        if (sr.get(data.sys_id)) {

            var incCreated = false;
            //Check if the user already created a child Incident
            var incGr = new GlideRecord('incident');
            inGr.addQuery('caller_id', gs.getUserID());
            inGr.addQuery('parent_incident', data.sys_id);
			// you may want to add more query such as the state
            incGr.query();

            if (!incGr.next()) {
                var jr = new GlideRecord('incident');
                jr.initialize();

                jr.setValue('incident_state', 'Assigned');
                jr.setValue('state', 9);
                jr.setValue('assigned_to', sr.assigned_to);
                jr.setValue('assignment_group', sr.assignment_group);
                jr.setValue('short_description', sr.short_description);
                jr.setValue('description', sr.description);
                jr.setValue('caller_id', gs.getUserID());
                jr.setValue('parent_incident', data.sys_id);
                jr.setValue('u_on_behalf_of', gs.getUserID());
                jr.setValue('u_callback_number', gs.getUser().getRecord().getValue('phone'));
                jr.setValue('location', gs.getUser().getRecord().getValue('location'));
                jr.setValue('contact_type', 'self-service');
                jr.setValue('category', sr.category);
                jr.setValue('subcategory', sr.subcategory);
                jr.setValue('impact', sr.impact);
                jr.setValue('urgency', sr.urgency);
                var sysIdTemp = jr.insert();
                if (sysIdTemp != -1) {
                    incCreated = true;
                }
            }
            data.incResult = incCreated;
        } else {
            gs.addErrorMessage("No valid Incident record was found");
        }

    }
})();

 

Client controller:

 

api.controller = function($scope, spUtil) {
    var c = this;
    $scope.sysverb_child_new = function() {
        var clientObj = {
            action: 'sysverb_child_new'
        };
        c.server.get(clientObj).then(function(response) {
            if (response.data.incResult == true) {
                alert("You've been added to this Incident"); // This will give you an alert with ok button
            } else {
                if (window.confirm('Thank you for reporting, you have previously been added to the incident. Click "ok" to be redirected to your request)) {
                    window.location.href = '{yoururl}/esc?id=my_requests';
                };
            }
            $sp.getWidget();
            c.data = response.data;
        })
    }

};

 

View solution in original post

2 REPLIES 2

James Chun
Kilo Patron

Hi @JuliaHowells,

 

Haven't tested this but try the following:

 

Server script:

// Server Script
(function() {

    // Get table & sys_id
    data.sys_id = input.sys_id || $sp.getParameter("sys_id");
    //input
    if (input && input.action == 'sysverb_child_new') {
        var sr = new GlideRecord('incident');
        if (sr.get(data.sys_id)) {

            var incCreated = false;
            //Check if the user already created a child Incident
            var incGr = new GlideRecord('incident');
            inGr.addQuery('caller_id', gs.getUserID());
            inGr.addQuery('parent_incident', data.sys_id);
			// you may want to add more query such as the state
            incGr.query();

            if (!incGr.next()) {
                var jr = new GlideRecord('incident');
                jr.initialize();

                jr.setValue('incident_state', 'Assigned');
                jr.setValue('state', 9);
                jr.setValue('assigned_to', sr.assigned_to);
                jr.setValue('assignment_group', sr.assignment_group);
                jr.setValue('short_description', sr.short_description);
                jr.setValue('description', sr.description);
                jr.setValue('caller_id', gs.getUserID());
                jr.setValue('parent_incident', data.sys_id);
                jr.setValue('u_on_behalf_of', gs.getUserID());
                jr.setValue('u_callback_number', gs.getUser().getRecord().getValue('phone'));
                jr.setValue('location', gs.getUser().getRecord().getValue('location'));
                jr.setValue('contact_type', 'self-service');
                jr.setValue('category', sr.category);
                jr.setValue('subcategory', sr.subcategory);
                jr.setValue('impact', sr.impact);
                jr.setValue('urgency', sr.urgency);
                var sysIdTemp = jr.insert();
                if (sysIdTemp != -1) {
                    incCreated = true;
                }
            }
            data.incResult = incCreated;
        } else {
            gs.addErrorMessage("No valid Incident record was found");
        }

    }
})();

 

Client controller:

 

api.controller = function($scope, spUtil) {
    var c = this;
    $scope.sysverb_child_new = function() {
        var clientObj = {
            action: 'sysverb_child_new'
        };
        c.server.get(clientObj).then(function(response) {
            if (response.data.incResult == true) {
                alert("You've been added to this Incident"); // This will give you an alert with ok button
            } else {
                if (window.confirm('Thank you for reporting, you have previously been added to the incident. Click "ok" to be redirected to your request)) {
                    window.location.href = '{yoururl}/esc?id=my_requests';
                };
            }
            $sp.getWidget();
            c.data = response.data;
        })
    }

};

 

Basheer
Mega Sage

Hi @JuliaHowells 

You can try as below

// Server Script
(function() {

    // Get table & sys_id
    data.sys_id = input.sys_id || $sp.getParameter("sys_id");
    //input
    if (input && input.action == 'sysverb_child_new') {
        var sr = new GlideRecord('incident');
        if (sr.get(data.sys_id)) {
 gs.addErrorMessage("hank you for reporting, you have previously been added to the incident");
}
else{

            var jr = new GlideRecord('incident');
            jr.initialize();

            jr.setValue('incident_state', 'Assigned');
            jr.setValue('state', 9);
            jr.setValue('assigned_to', sr.assigned_to);
			jr.setValue('assignment_group', sr.assignment_group);
            jr.setValue('short_description', sr.short_description);
            jr.setValue('description', sr.description);
            jr.setValue('caller_id', gs.getUserID());
            jr.setValue('parent_incident', data.sys_id);
            jr.setValue('u_on_behalf_of', gs.getUserID());
            jr.setValue('u_callback_number', gs.getUser().getRecord().getValue('phone'));
            jr.setValue('location', gs.getUser().getRecord().getValue('location'));
            jr.setValue('contact_type', 'self-service');
            jr.setValue('category', sr.category);
            jr.setValue('subcategory', sr.subcategory);
            jr.setValue('impact', sr.impact);
            jr.setValue('urgency', sr.urgency);
            var sysIdTemp = jr.insert();
        }
    }
})();

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.