Copy Field from incident record when new problem is created

Bintes
Giga Contributor

I am new to ServiceNow. I have created a field u_inc_reason on the incident form, and also created a field u_prb_reason on the problem form. I want the value from the incident (u_inc_reason) to be copied to the problem record when the create proble ui action is used on the incident form. I have tried writing a business rule and also tried updating the create problem ui action but have not been successful. Any script available to achieve that?

1 ACCEPTED SOLUTION

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Bintes 

 

Please find the updated script of 'Create Problem' UI Action:

 

var prob = new IncidentUtils().getProblemFromIncident(current);
if (prob != undefined) {
    current.problem_id = prob.insert();
	
    if (current.state != IncidentState.RESOLVED) {
        current.state = IncidentState.ON_HOLD;
        if (current.isValidField("hold_reason"))
            current.hold_reason = IncidentState.AWAITING_PROBLEM;
    }
    if (GlidePluginManager.isActive("com.snc.best_practice.problem.madrid")) {
        var problemV2Util = new ProblemV2Util();
        problemV2Util.copyAttachments(current, prob);
        problemV2Util.copyAttachedKnowledge(current, prob);
    }
    current.update();
    gs.addInfoMessage(gs.getMessage("Problem {0} created", prob.number));
    action.setRedirectURL(prob);
    action.setReturnURL(current);
	
	var prb = new GlideRecord('problem');
	prb.addQuery('sys_id',prob.sys_id);
	prb.query();
	if(prb.next())
		{
			prb.u_prb_reason = current.u_inc_reason;
			prb.update();
		}
} else {
    action.setRedirectURL(current);
}

 

Added some script in Line 20 to 27.

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

3 REPLIES 3

Sagar Pagar
Tera Patron

Hi @Bintes,

 

I hope you have gone through the Create Problem UI action on incident table. You have to just add the below line to update your INC field to PRB field.

 

prob.u_prb_reason = current.u_inc_reason;
current.problem_id = prob.insert(); // OOTB scritps line

 

Thanks,

Sagar Pagar

The world works with ServiceNow

OlaN
Giga Sage
Giga Sage

Hi,

This is doable, but only if the two custom fields you've created can contain the same values, and they are of the same data type (String, Integer, Reference and so on)

You should be able to modify the OOB UI action that copies the incident record data to a new problem record.

Try adding a line that looks something like below:

problemGR.u_prb_reason = current.u_inc_reason;

 

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Bintes 

 

Please find the updated script of 'Create Problem' UI Action:

 

var prob = new IncidentUtils().getProblemFromIncident(current);
if (prob != undefined) {
    current.problem_id = prob.insert();
	
    if (current.state != IncidentState.RESOLVED) {
        current.state = IncidentState.ON_HOLD;
        if (current.isValidField("hold_reason"))
            current.hold_reason = IncidentState.AWAITING_PROBLEM;
    }
    if (GlidePluginManager.isActive("com.snc.best_practice.problem.madrid")) {
        var problemV2Util = new ProblemV2Util();
        problemV2Util.copyAttachments(current, prob);
        problemV2Util.copyAttachedKnowledge(current, prob);
    }
    current.update();
    gs.addInfoMessage(gs.getMessage("Problem {0} created", prob.number));
    action.setRedirectURL(prob);
    action.setReturnURL(current);
	
	var prb = new GlideRecord('problem');
	prb.addQuery('sys_id',prob.sys_id);
	prb.query();
	if(prb.next())
		{
			prb.u_prb_reason = current.u_inc_reason;
			prb.update();
		}
} else {
    action.setRedirectURL(current);
}

 

Added some script in Line 20 to 27.

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023