Convert Work Order to Incident

kal123
Tera Contributor

Hi All,

I am searching for a way that when I convert a Work Order to an Incident, the Work Order is set to Closed Incomplete/Cancelled. As of right now I have the issue that when clicking the UI action "Create Incident" the Incident is created but the Work Order is still active and set to true. What would be the best way to automatically close this Work Order once the Incident is created? Any help would be appreciated. 

 

Thank you,

2 ACCEPTED SOLUTIONS

Here's a quick answer that might fit your current need. You can improve it according to the evolution of your organization.

 

var canCreateIncident = false;
if ((current.isNewRecord() && current.canCreate()) || (!current.isNewRecord() && current.canWrite()))
    canCreateIncident = current.update();
else
    canCreateIncident = true;

if (canCreateIncident) {
    var inc = new GlideRecord("incident");
    inc.initialize();
    inc.caller_id = current.opened_for;
    inc.short_description = current.short_description;
    if (gs.getProperty("com.snc.incident.create_from_interaction.save") === 'true') {
        inc.work_notes = gs.getMessage('Incident created from Interaction {0}', current.number);
        var incSysId = inc.insert();
        if (incSysId) {
            var interactionRelatedGR = new GlideRecord("interaction_related_record");
            interactionRelatedGR.initialize();
            interactionRelatedGR.interaction = current.sys_id;
            interactionRelatedGR.document_table = 'incident';
            interactionRelatedGR.document_id = incSysId;
            interactionRelatedGR.insert();
        }
    }

    action.openGlideRecord(inc);
    new InteractionRelationshipUtil().copyAttachments(current, inc);

    /* Put the information 'work notes' corresponding to your organization here */
    current.work_notes = "Question from Kal123";
    current.state = 4; //closed incomplete or 3 for closed complete
    current.update();
    /* quick fix */
}

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,

Hope it helps!

Donald DJA

View solution in original post

Donald  DJA
Kilo Sage

Hello @kal123 ,

I checked the difference UI actions that you have shared here, and i appreciate that.

For UI action from Work order [wm_order] table, can you try this script below (You can change any informations for meeting your organisation needs) :

 

 

var canCreateIncident = false;
if ((current.isNewRecord() && current.canCreate()) || (!current.isNewRecord() && current.canWrite()))
    canCreateIncident = current.update();
else
    canCreateIncident = true;

if (canCreateIncident) {
    var inc = new GlideRecord("incident");
    inc.initialize();
    inc.caller_id = current.opened_for;
    inc.short_description = current.short_description;
    if (gs.getProperty("com.snc.incident.create_from_interaction.save") === 'true') {
        inc.work_notes = gs.getMessage('Incident created from Interaction {0}', current.number);
        var incSysId = inc.insert();
        if (incSysId) {
            var interactionRelatedGR = new GlideRecord("interaction_related_record");
            interactionRelatedGR.initialize();
            interactionRelatedGR.interaction = current.sys_id;
            interactionRelatedGR.document_table = 'incident';
            interactionRelatedGR.document_id = incSysId;
            interactionRelatedGR.insert();
        }
    }

    action.openGlideRecord(inc);
    new InteractionRelationshipUtil().copyAttachments(current, inc);
	/* Put the information corresponding to your organization here */
	current.work_notes = "Question from Kal123"; // change this information for organisation needs
	current.state = 4;
	current.update();
}

 

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,

Hope it helps!

Donald DJA

View solution in original post

11 REPLIES 11

kal123
Tera Contributor

@Donald DJA Thank your the help! If you can help with the script for the incident to automatically close that would be great!! Below is the current contents of the UI Action: "Create Incident" :

var canCreateIncident = false;
if ((current.isNewRecord() && current.canCreate()) || (!current.isNewRecord() && current.canWrite()))
    canCreateIncident = current.update();
else
    canCreateIncident = true;

if (canCreateIncident) {
    var inc = new GlideRecord("incident");
    inc.initialize();
    inc.caller_id = current.opened_for;
    inc.short_description = current.short_description;
    if (gs.getProperty("com.snc.incident.create_from_interaction.save") === 'true') {
        inc.work_notes = gs.getMessage('Incident created from Interaction {0}', current.number);
        var incSysId = inc.insert();
        if (incSysId) {
            var interactionRelatedGR = new GlideRecord("interaction_related_record");
            interactionRelatedGR.initialize();
            interactionRelatedGR.interaction = current.sys_id;
            interactionRelatedGR.document_table = 'incident';
            interactionRelatedGR.document_id = incSysId;
            interactionRelatedGR.insert();
        }
    }
    action.openGlideRecord(inc);
    new InteractionRelationshipUtil().copyAttachments(current, inc);
}

Here's a quick answer that might fit your current need. You can improve it according to the evolution of your organization.

 

var canCreateIncident = false;
if ((current.isNewRecord() && current.canCreate()) || (!current.isNewRecord() && current.canWrite()))
    canCreateIncident = current.update();
else
    canCreateIncident = true;

if (canCreateIncident) {
    var inc = new GlideRecord("incident");
    inc.initialize();
    inc.caller_id = current.opened_for;
    inc.short_description = current.short_description;
    if (gs.getProperty("com.snc.incident.create_from_interaction.save") === 'true') {
        inc.work_notes = gs.getMessage('Incident created from Interaction {0}', current.number);
        var incSysId = inc.insert();
        if (incSysId) {
            var interactionRelatedGR = new GlideRecord("interaction_related_record");
            interactionRelatedGR.initialize();
            interactionRelatedGR.interaction = current.sys_id;
            interactionRelatedGR.document_table = 'incident';
            interactionRelatedGR.document_id = incSysId;
            interactionRelatedGR.insert();
        }
    }

    action.openGlideRecord(inc);
    new InteractionRelationshipUtil().copyAttachments(current, inc);

    /* Put the information 'work notes' corresponding to your organization here */
    current.work_notes = "Question from Kal123";
    current.state = 4; //closed incomplete or 3 for closed complete
    current.update();
    /* quick fix */
}

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,

Hope it helps!

Donald DJA

Hi @Donald DJA ,

I know how to look for this answer too. THANKS. I imagine you could help me a little more if I have specific questions. thanks in advance.

@Donald DJA I have tried this solution and I am still having some issues. There are two UI actions I have, one is on the interaction table, and one is on the wm_order table. I have the script below in both UI actions but when creating the Incident the Work order is still in the Draft state and not moving to closed complete/incomplete. Please let me know If something is incorrect.

 

current.work_notes.setJournalEntry(setwrknotesinci);
    current.state ='8'; // Cancelling the work order after conversion to Incident
    current.setValue('active', 'false'); // work order will be active = false after conversion
    current.update();
 
the pictures attached are both UI actions.

Donald  DJA
Kilo Sage

Hello @kal123 ,

I checked the difference UI actions that you have shared here, and i appreciate that.

For UI action from Work order [wm_order] table, can you try this script below (You can change any informations for meeting your organisation needs) :

 

 

var canCreateIncident = false;
if ((current.isNewRecord() && current.canCreate()) || (!current.isNewRecord() && current.canWrite()))
    canCreateIncident = current.update();
else
    canCreateIncident = true;

if (canCreateIncident) {
    var inc = new GlideRecord("incident");
    inc.initialize();
    inc.caller_id = current.opened_for;
    inc.short_description = current.short_description;
    if (gs.getProperty("com.snc.incident.create_from_interaction.save") === 'true') {
        inc.work_notes = gs.getMessage('Incident created from Interaction {0}', current.number);
        var incSysId = inc.insert();
        if (incSysId) {
            var interactionRelatedGR = new GlideRecord("interaction_related_record");
            interactionRelatedGR.initialize();
            interactionRelatedGR.interaction = current.sys_id;
            interactionRelatedGR.document_table = 'incident';
            interactionRelatedGR.document_id = incSysId;
            interactionRelatedGR.insert();
        }
    }

    action.openGlideRecord(inc);
    new InteractionRelationshipUtil().copyAttachments(current, inc);
	/* Put the information corresponding to your organization here */
	current.work_notes = "Question from Kal123"; // change this information for organisation needs
	current.state = 4;
	current.update();
}

 

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,

Hope it helps!

Donald DJA