User Requirement Could you plz provide code

mahesh_03
Tera Expert
  1. Create a new UI Action on Change Request - Create Task
  2. This button should be visible only in New/Assess state and to the Assignee of the ticket.
  3. When user clicks on Create Task, user should be redirected to new Change Task form and below fields should be copied from Change Request to Change Task:
    - Configuration Item
    - Short Description
    - Description
    - Assignment Group 
    - Assigned To
  4. If there is already an active Change Task for the same Configuration Item, stay on the Change Request form and display an Error Message - "Change Task is already active for the same Configuration Item".
8 REPLIES 8

maheshchokkara
Tera Contributor

From the Below code what changes do I need to make to fulfill my requirement? Actually, I planned to do by server-side script only. from the above script I struck at URL Part I'm trying to use action.setRedirectURL(); instead of window href  can you use that redirect URL method. to complete the requirement.Screenshot 2024-02-04 205402.png 

Hi @maheshchokkara window.location.href is a client side method and it will not work on server side, you can use the below to redirect

put this after line 39

 gs.addInfoMessage(gs.getMessage("Record{0} created", ct.number));
    action.setRedirectURL(ct);
Regards
Harish

Thanks its working but one was not working in my code if you see in my requirement 4th point! 

  1. If there is already an active Change Task for the same Configuration Item, stay on the Change Request form and display an Error Message - "Change Task is already active for the same Configuration Item".   already I did some changes but it's not working can you please guide what changes do I need to do.

Hi @maheshchokkara here is the example, you can adjust your code accordingly

var chgTask = new GlideRecord('change_task');
    chgTask.addQuery('change_request', current.sys_id);
    chgTask.addQuery('cmdb_ci', current.cmdb_ci);
    chgTask.query();
    while (chgTask.next()) {
// check if CI already exist in changeTask
        if (chgTask.cmdb_ci == current.cmdb_ci) {
            gs.addErrorMessage("chg task already created for CI " + current.cmdb_ci.getDisplayValue());
            current.setAbortAction(true);
        }
// create chgtask if there is no ci exist for same ci
else {
            var chgTask1 = new GlideRecord('change_task');
            chgTask1.initialize();
            chgTask1.change_request = current.sys_id;
            chgTask1.short_description = current.short_description;
            chgTask1.description = current.description;
            chgTask1.cmdb_ci = current.cmdb_ci;
            chgTask1.insert();
            gs.addInfoMessage(gs.getMessage("Record{0} created", chgTask1.number));
action.setRedirectURL(chgTask1);

        }
    }
Regards
Harish