Create a change management from UI Action

AlejandroS53470
Tera Contributor

Hi beautiful community, i have a problem
I need to create a record in the change request table with a UI action.
The button that creates it is located in a task of a catalog item, and it should only be displayed for a specific service, when the state is "work in progress", only for the third task, and when a date-type variable that I need to display is not empty.

I have the following code:

function onClick() {
    var reqItemGr = current.request_item.cat_item;

    var typeReq = reqItemGr.variables.request_type.getValue();
    var change = new GlideRecord('change_request');
    change.initialize();
    change.type = 'normal';
    change.category = 'infraestructure';
    change.u_subcategory = 'software_installation';
    change.u_requirement_id = reqItemGr.number;
    change.u_requested_item = reqItemGr.sys_id;
    change.requested_by = reqItemGr.requested_by;
    change.u_requested_for = reqItemGr.requested_for;
    change.u_organizational_unit = reqItemGr.requested_for.u_org_unit.getValue();

    if (typeReq == 'installation' || typeReq == 'replacement') {

        var dateAvailability = reqItemGr.variables.date_of_availability.getValue();
        var dateAvailabilityGDT = new GlideDateTime(dateAvailability);
        dateAvailabilityGDT.addHours(8);

        change.short_description = 'Software Deployment - ' + reqItemGr.variables.software_name.getValue();
        change.start_date = dateAvailability;
        change.end_date = dateAvailabilityGDT;

        if (typeReq == 'replacement') {
            change.description = 'The installation of the software ' + reqItemGr.variables.software_name.getValue() + ' ' + reqItemGr.variables.software_version.getValue() + ' - is considered on the following devices included: ' + reqItemGr.variables.scope.getValue();
        } else if (typeReq == 'installation') {
            change.description = 'The replacement of the software ' + reqItemGr.variables.software_name.getValue() + ' ' + reqItemGr.variables.software_version.getValue() + ' - is considered on the following devices included: ' + reqItemGr.variables.scope.getValue();
        }

    } else if (typeReq == 'uninstallation') {

        var retirementDate = reqItemGr.variables.retirement_date.getValue();
        var retirementDateGDT = new GlideDateTime(retirementDate);
        retirementDateGDT.addHours(8);

        change.short_description = 'Software Uninstallation - ' + reqItemGr.variables.software_name.getValue();
        change.description = 'The uninstallation of the software ' + reqItemGr.variables.software_name.getValue() + ' ' + reqItemGr.variables.software_version.getValue() + ' - is considered on the following devices included: ' + reqItemGr.variables.scope.getValue();
        change.start_date = retirementDate;
        change.end_date = retirementDateGDT;
    }

    change.insert();

}

But in the logs, I get the error "Unparseable date: '0'".
I am not sure what is causing this issue, and I would appreciate any help with this.
1 REPLY 1

Runjay Patel
Giga Sage

Hi @AlejandroS53470 ,

 

Your code look okay to me but if dates will not have proper value then you will get error.

check all date fields one by one.

 

Also, do not make client callable UI action, write code without function so by default it will be consider as server side code.

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------