UI Action already runs when loading form

Christophe
Tera Contributor

Dear experts,

 

I created a UI Action to expire a contract manually. The Form Button works fine, but the problem is that the UI Action is triggered when the form loads. 

 

UI Action

 

Name: Expire Contract
Table: ast_contract
Action name: expire_contract
Show update: True
Client: True
Form button: True
Onclick: setContractToExpired()
Condition: current.state == 'overdue'
Script: 

function setContractToExpired() {
var comment = prompt("Please enter a comment before expiring the contract:");

if (comment === null || comment.trim() === "") {
alert("You must provide a comment.");
return;
}

var ga = new GlideAjax('ExpireContractClientHelper');
ga.addParam('sysparm_name', 'expireContract');
ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
ga.addParam('sysparm_comment', comment);

ga.getXMLAnswer(function(response) {
if (response === 'success') {
alert("Contract status has been set to Expired.");
g_form.reload();
} else {
alert("Error: " + response);
}
});
}

setContractToExpired();

 

Script Include

 

Name: ExpireContractClientHelper
API Name: global.ExpireContractClientHelper
Client callable: True

Script:

var ExpireContractClientHelper = Class.create();
ExpireContractClientHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    expireContract: function() {
        var sysId = this.getParameter('sysparm_sys_id');
        var comment = this.getParameter('sysparm_comment');

        if (!sysId || !comment) {
            return 'Missing parameters';
        }

        var contractGR = new GlideRecord('ast_contract');
        if (contractGR.get(sysId)) {
            contractGR.setValue('state', 'expired');
            contractGR.setValue('description', comment);
            contractGR.update();
            return 'success';
        } else {
            return 'Contract not found';
        }
    }
});

 

 

I'm clearly doing something wrong, but I haven't got the foggiest what it is.

Thank you in advance for your help.

 

Best regards,

Chris.

 

 

2 ACCEPTED SOLUTIONS

Chaitanya ILCR
Kilo Patron

Hi @Christophe ,

replace UI Action script 

 

function setContractToExpired() {
var comment = prompt("Please enter a comment before expiring the contract:");

if (comment === null || comment.trim() === "") {
alert("You must provide a comment.");
return;
}

var ga = new GlideAjax('ExpireContractClientHelper');
ga.addParam('sysparm_name', 'expireContract');
ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
ga.addParam('sysparm_comment', comment);

ga.getXMLAnswer(function(response) {
if (response === 'success') {
alert("Contract status has been set to Expired.");
g_form.reload();
} else {
alert("Error: " + response);
}
});
}

 

just removed last line which is calling the function 

 

the button click calls the script there is no need to explicitly call it again

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

View solution in original post

Hi @Christophe ,

 

you want to append the prompted value to the description ?

 

you can update the script include as this

var ExpireContractClientHelper = Class.create();
ExpireContractClientHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    expireContract: function() {
        var sysId = this.getParameter('sysparm_sys_id');
        var comment = this.getParameter('sysparm_comment');

        if (!sysId || !comment) {
            return 'Missing parameters';
        }

        var contractGR = new GlideRecord('ast_contract');
        if (contractGR.get(sysId)) {
            contractGR.setValue('state', 'expired');
            contractGR.setValue('description', contractGR.getValue('description') + ' ' + comment);
            contractGR.update();
            return 'success';
        } else {
            return 'Contract not found';
        }
    }
});

 

if my understanding is wrong please share more details with some screenshots 

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

View solution in original post

6 REPLIES 6

Hi @Christophe ,

 

you want to append the prompted value to the description ?

 

you can update the script include as this

var ExpireContractClientHelper = Class.create();
ExpireContractClientHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    expireContract: function() {
        var sysId = this.getParameter('sysparm_sys_id');
        var comment = this.getParameter('sysparm_comment');

        if (!sysId || !comment) {
            return 'Missing parameters';
        }

        var contractGR = new GlideRecord('ast_contract');
        if (contractGR.get(sysId)) {
            contractGR.setValue('state', 'expired');
            contractGR.setValue('description', contractGR.getValue('description') + ' ' + comment);
            contractGR.update();
            return 'success';
        } else {
            return 'Contract not found';
        }
    }
});

 

if my understanding is wrong please share more details with some screenshots 

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

tejas1111
Tera Contributor

Hi @Christophe ,

you did everything right . just you did one mistake.

Just remove this line from the bottom of your UI Action script:

setContractToExpired();

Your updated UI Action script should be:

function setContractToExpired() {
    var comment = prompt("Please enter a comment before expiring the contract:");

    if (comment === null || comment.trim() === "") {
        alert("You must provide a comment.");
        return;
    }

    var ga = new GlideAjax('ExpireContractClientHelper');
    ga.addParam('sysparm_name', 'expireContract');
    ga.addParam('sysparm_sys_id', g_form.getUniqueValue());
    ga.addParam('sysparm_comment', comment);

    ga.getXMLAnswer(function(response) {
        if (response === 'success') {
            alert("Contract status has been set to Expired.");
            g_form.reload();
        } else {
            alert("Error: " + response);
        }
    });
}

And leave the OnClick field as

setContractToExpired()

 

PFA screenshots

tejas1111_0-1753080738468.png