
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2025 11:31 PM
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:
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2025 11:43 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2025 03:40 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2025 03:40 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2025 11:53 PM
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