Script include to query a record and set the value of specific field

Sri56
Tera Contributor

Hi Team,

We need a script include to query 'sysapproval_approver' table and check if approval type is parallel.

For those records, we need to set the field 'parallel approvals intiated' as true from client script by calling above script include.

below screenshot is for your reference:

find_real_file.png

Please help with the code

Thanks,

Sri

 

34 REPLIES 34

Well, I am not sure about the configuration of your Action/Button. But below is the script include and client script function.

Script Include

var UpdateApprovalRecords = Class.create();
UpdateApprovalRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    updateApprovals: function() {

        var msg = 'No record found to be updated.';
        var grApproval = new GlideRecord('sysapproval_approver');

        grApproval.addQuery('<backend_name_of_field_approval_type>', '<backend_value_of_parallel_choice>');
        grApproval.query();
        if (grApproval.hasNext()) {
			msg = 'Some records have been updated.';
            while (grApproval.next()) {
                grApproval.setValue('<backend_name_of_field_parallel_approvals_initiated', true);
                grApproval.update();
            }
        }
		
		return msg;
    },

    type: 'UpdateApprovalRecords'
});

Your script include must be in your Application Scope and API name of your script include will be prefixed with application scope. See the below image for reference.

find_real_file.png

Client Script Function

function onClick() {
    var ga = new GlideAjax('global.UpdateApprovalRecords'); // Make sure to use API name of your Script Include here.
    ga.addParam('sysparm_name', 'updateApprovals');
	ga.getXML(callBack);
	
	function callBack(response){
		var answer = response.responseXML.documentElement.getAttribute('answer');
		alert(answer);
	}
}

Hi,

Above solution is not working.

Please help!

Please refer my script include and client script

find_real_file.png

find_real_file.png

Please help,

Thanks,

Sri

Try writing client script outside the onClick() function (means remove the onClick() function but keep the script which is inside of it).

Hi,

Still no luck!

Please help on this!

Thanks,

Sri

Hi Muhammad,

Its partially working, if i try below code:

script include:

find_real_file.png

client script:

find_real_file.png

But this is updating all my records which is of parallel type.

I just need to restrict to my sctask, from where i'm intiating this.

Please suggest and help with the modification.

Thanks,

Sri