Client script to populate start/end date from CHG to CTASK

jean-pauldehaas
Tera Guru

Hi all,

 

i have a requirement to populate the start/end date from the change to all new(manually) created CTASKs.

 

i created a client script (for testing i only used start date first) for this but i think im missing something..

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   if (g_form.getValue('planned_start_date') == '') {
    g_form.setValue('planned_start_date', 'change_request.start_date');
   }
}
 
 
can anyone point me in the right direction ?
1 ACCEPTED SOLUTION

Hi @jean-pauldehaas ,

 

Sure! See below for the Script Include:

 

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

    getDates: function() {
        var parentChange = this.getParameter('sysparm_parent_change');
        var gr = new GlideRecord('change_request');
        gr.get(parentChange);
        var pstart = new GlideDateTime();
        var pend = new GlideDateTime();
        pstart.setValue(gr.getValue('start_date'));
        pend.setValue(gr.getValue('end_date'));
        var results = {
            "planned_start": gr.start_date.getDisplayValue(),
            "planned_end": gr.end_date.getDisplayValue()
        };
        //gs.log("Results: " + JSON.stringify(results));
        return JSON.stringify(results);
    },

    getAssignmentGroup: function() {
        var parentChange = this.getParameter('sysparm_parent_change');
        var tasktype = this.getParameter('sysparm_task_type');
        var gr = new GlideRecord('change_request');
        gr.get(parentChange);
        if (gr.type == 'standard','normal' && tasktype == 'customer_coordination') {
            var assignmentgroup = gr.getValue('assignment_group');
            return assignmentgroup;
        }
    },

    type: 'ChangeTaskUtils'
});

View solution in original post

20 REPLIES 20

Hi Brian,

 

i was thinking of this solution also, but the thing is that if the planned end/start date are then set on the CTASK and then want to update it , it wont be saved but overwritten again by the values of the Change

That's why I included the condition that the planned start date as empty.  If it already has a value, it should not get updated.

Josh Pirozzi
Kilo Sage

Hi @jean-pauldehaas 

 

We have this set up through an onLoad Client Script and it will auto-populate the Planned State/End Dates from the Parent Change, and allow for these dates to be modified and saved.

 

Script:

 

function onLoad() {
    //Get the planned start/end dates from the parent change
    if (g_form.getValue('planned_start_date') == '' && g_form.getValue('planned_end_date') == '') {
        var ga = new GlideAjax('ChangeTaskUtils');
        ga.addParam('sysparm_name', 'getDates');
        ga.addParam('sysparm_parent_change', g_form.getValue('change_request'));
        ga.getXMLAnswer(updateDates);
    }
}

function updateDates(answer) {
    if (answer) {
        var returneddata = JSON.parse(answer);
        g_form.setValue('planned_start_date', returneddata.planned_start);
        g_form.setValue('planned_end_date', returneddata.planned_end);
    } else {
        g_form.addErrorMessage('No dates found for parent change');
    }
}

hi,

 

i tried your solution but cant get it to work

Hi @jean-pauldehaas , 

 

Is it possible that there is another Business Rule or Script currently active that is impacting what you're trying to achieve?

 

Thanks,

Josh