Issues with populating REQ fields with INC fields after conversion

joymorales
Giga Guru

Hi Team.

 

I am trying to populate the REQ form fields with INC form fields after conversion to REQ.  I have the following UI Action for the conversion and it works fine.

 

url = 'sc_request.do?sysparm_incident_id=' + current.sys_id;
    action.setRedirectURL(url);

current.state='7';
current.work_notes="Created REQ";
current.close_code='6';
current.close_notes="Created REQ";
current.update();

 

I have the following on-Load Client Script for populating the REQ fields with INC fields info.  The "Requested_for", "Company", and "Short Description" fields populate fine.  The "u_Category" and "u_Subcategory" fields populate with the value (all lower case) instead of the Label info.  The rest of the fields do not populate.

 

function onLoad() {
   //Verify that the sysparm_incident_id parameter is present in the URL
   var gURL = new GlideURL();
   gURL.setFromCurrent();
   var incidentID = gURL.getParam("sysparm_incident_id");

   if(incidentID){
    /*Verified this request is a conversion from an Incident. Use the sys_id to create a new GlideRecord
    object with the incident data*/
    var grINT = new GlideRecord ('incident');
    grINT.get(incidentID);

    //Map each of the Incident fields to the relevant Request fields
    g_form.setValue('requested_for', grINT.caller_id);
    g_form.setValue('company', grINT.company);
    g_form.setValue('u_category', grINT.category);
    g_form.setValue('u_subcategory', grINT.subcategory);
    g_form.setValue('short_description', grINT.short_description);
    g_form.setValue('description', grINT.description);
    g_form.setValue('assignment_group', grINT.assignment.group);
    g_form.setValue('assigned_to', grINT.assigned_to);
    g_form.setValue('state', grINT.state);

    }
   
}
 
Could you please point me in the right direction to resolve this issue?  Also, any suggestion for copying the INC Activities info to the REQ form?  Any suggestion will be greatly appreciated.
3 REPLIES 3

Peter Bodelier
Giga Sage

Hi @joymorales 

 

What exactly is your intention with this? Requests should not be used in this way.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

AshishKM
Kilo Patron
Kilo Patron

Hi @joymorales ,

Seems like you are trying to transfer the incident data to service request and that UI action should create service request with some incident data. 

 

You have to use cart class and catalog item for placing this service request order from Incident UI Action

var cart = new Cart(cartId);

Refer below for code.

https://www.servicenow.com/community/itsm-forum/how-to-populate-a-value-from-incident-to-a-ritm/td-p...

https://www.servicenow.com/community/developer-forum/create-a-request-from-an-quot-ui-action-quot/m-... 

Let us know if it works for you.

-Thanks,

AshishKMishra


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Bert_c1
Kilo Patron

Hi @joymorales,

 

If you go ahead, you can achieve your goal by updating the UI Action with the following script:

 

 

 

action.setRedirectURL(current);
var incidentRequest = (new IncidentUtils()).createRequest(current);
gs.addInfoMessage(gs.getMessage("Request {0} was created", incidentRequest));

 

 

 

And adding the following function to the 'IncidentUtils' script include:

 

 

 

	createRequest: function(currentIncident) {
		// create sc_request record
		var gr = new GlideRecord('sc_request');
		gr.setValue('requested_for', currentIncident.caller_id);
		gr.setValue('company', currentIncident.company);
		gr.setValue('u_category', currentIncident.category);
		gr.setValue('u_subcategory', currentIncident.subcategory);
		gr.setValue('short_description', currentIncident.short_description);
		gr.setValue('description', currentIncident.description);
		gr.setValue('assignment_group', currentIncident.assignment_group);
		gr.setValue('assigned_to', currentIncident.assigned_to);
		gr.setValue('state', currentIncident.state);
		var requestId = gr.insert();
		gs.info('createRequest: created request: ' + requestId);
		gr.get(requestId);
		
		// update incident fields
		current.state='7';
		current.work_notes="Created " + gr.number;
		current.close_code='6';
		current.close_notes="Created " + gr.number;
		var updResult = current.update();
		gs.info('createRequest: incident update: ' + updResult);

		return "<a href='/sc_request.do?sysparm_query=number=" + gr.number +"'>" + gr.number + "</a>";
	},

 

 

Add after " /***************Custom changes****************/ "

 

(Taken from "Create Security Incident" UI action defined on the incident table).