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.

Update field value using UI action script, when user click Close Task

Zaman Sm
Tera Expert

Hello,

I have a field called "software_renew_reference" which pulls the software name from alm_license table and users have to select one software. When the fulfiller type in a new start date "new_start" in the sc task, and click the close task UI button, the start date "start_date" field of the software need to get updated with the new start date.

I tired the following script in the Ui action script , but no result yet.

 

Any help, idea will be appreciated.

 

var newStartDate = current.new_start; //
var selectedSoftware = current.software_renew_reference; //

// Check if newStartDate is empty
function checkStartDate(newStartDate) {
    if (!newStartDate) {
        gs.addInfoMessage('No new start date provided. No changes made.');
        return;
    }}

// Ensure selected software is provided before proceeding
if (selectedSoftware) {
    // Query the alm_license table
    var licenseGr = new GlideRecord('alm_license');
    licenseGr.addQuery('display_name', selectedSoftware); // check the display_name in the alm-license table
    licenseGr.query();

    if (licenseGr.next()) {
        // Update the start date field in the alm_license table
        licenseGr.setValue('start_date', newStartDate); //
        var updateResult = licenseGr.update();

        if (updateResult) {
            gs.addInfoMessage('License start date updated successfully.');
        } else {
            gs.addErrorMessage('Failed to update the license start date.');
        }
    } else {
        gs.addErrorMessage('No matching software record found in the license table.');
    }
} else {
    gs.addErrorMessage('Selected software is not specified.');
}


//-------------------------------------------------------------------------------------------
current.state = 3;
current.update();
2 REPLIES 2

HrishabhKumar
Kilo Sage

Hi @Zaman Sm ,

After taking a look at your code, I've done some slight modification in it. Try the below code:

(function executeRule(current, previous /*null when async*/) {
    var newStartDate = current.new_start;
    var selectedSoftware = current.software_renew_reference;

    // Check if newStartDate is empty
    if (!newStartDate) {
        gs.addInfoMessage('No new start date provided. No changes made.');
        return;
    }

    // Ensure selected software is provided before proceeding
    if (selectedSoftware) {
        // Query the alm_license table
        var licenseGr = new GlideRecord('alm_license');
        licenseGr.addQuery('sys_id', selectedSoftware); // Assuming 'software_renew_reference' holds sys_id of the alm_license record
        licenseGr.query();

        if (licenseGr.next()) {
            // Update the start date field in the alm_license table
            licenseGr.setValue('start_date', newStartDate);
            var updateResult = licenseGr.update();

            if (updateResult) {
                gs.addInfoMessage('License start date updated successfully.');
            } else {
                gs.addErrorMessage('Failed to update the license start date.');
            }
        } else {
            gs.addErrorMessage('No matching software record found in the license table.');
        }
    } else {
        gs.addErrorMessage('Selected software is not specified.');
    }

    // Close the current task
    current.state = 3;
    current.update();
})(current, previous);

 

Thanks,

Hope this helps.

If my response proves helpful please mark it helpful and accept it as solution to close this thread.

I really appreciate you for taking time. I tried the modified code, and I am receiving this 

'No new start date provided. No changes made.' msg when closing the task. even when the new start date field is filled up with  date.