Create & Update Incident using Transform map

SN_Learn
Kilo Patron
Kilo Patron

Hi All, 

 

I want to create and update incidents with the help of the transform map. The number field in the incident table is unique.

 

The issue that I am facing is that when I am sending the request body using the Postman the record is getting created but if I want to update it, it is throwing an error message.

 

Sample Request Body:

{
    "u_caller": "Mellissa Sule",
    "u_assignment_group": "Help Desk",
    "u_impact": "1",
    "u_urgency": "1",
    "u_category": "Database",
    "u_short_description": "Postman SD",
    "u_sub_category": "VPN"
}

Response:

{
    "import_set": "ISET0010004",
    "staging_table": "u_test_incident",
    "result": [
        {
            "transform_map": "Incident Transform Map",
            "table": "incident",
            "display_name": "number",
            "display_value": "INC0010006",
            "record_link": "https://devXXXX.service-now.com/api/now/table/incident/70748fb397832110ea187f200153af0a",
            "status": "inserted",
            "sys_id": "70748fb397832110ea187f200153af0a"
        }
    ]
}

 

The record is created successfully but if I want to update the same incident, it is not working.

I have also used the Run Script part of the Transform map but it didn't help, the code is:

 

(function transformEntry(source, target, map, log, isUpdate) {
	if(action == 'update'){
    // Check if the incident already exists based on the unique key
    var existingIncident = new GlideRecord('incident');
    existingIncident.addQuery('number', source.u_number.toString()); 
    existingIncident.query();

    if (existingIncident.next()) {
        // Update existing incident
        gs.log('Updating existing incident: ' + existingIncident.number);

        // Update the mapped fields
        existingIncident.short_description = source.u_short_description;
        existingIncident.sub_category = source.u_sub_category;
        existingIncident.urgency = source.u_urgency;
        existingIncident.impact = source.u_impact;
        existingIncident.category = source.u_category;
        existingIncident.caller = source.u_caller;
        existingIncident.assignment_group = source.u_assignment_group;
        existingIncident.update();
	}
    } 
	if(action == 'insert')
	{
        // Create a new incident
        gs.log('Creating new incident');

        var newIncident = new GlideRecord('incident');
        newIncident.initialize();

        // Set the mapped fields
        existingIncident.short_description = source.u_short_description;
        existingIncident.sub_category = source.u_sub_category;
        existingIncident.urgency = source.u_urgency;
        existingIncident.impact = source.u_impact;
        existingIncident.category = source.u_category;
        existingIncident.caller = source.u_caller;
        existingIncident.assignment_group = source.u_assignment_group;
        existingIncident.update();

        newIncident.insert();
    }
})(source, target, map, log, action === "update");

 

The request body for updating the ticket is:

{
    "u_number": "INC0010006",
    "u_caller": "Jade Erlebach",
    "u_assignment_group": "App Engine Admins",
    "u_impact": "2",
    "u_urgency": "2",
    "u_category": "Database",
    "u_short_description": "Postman SD",
    "u_sub_category": "Oracle"
}

 

The error that is coming up in Postman is:

{
    "import_set": "ISET0010004",
    "staging_table": "u_test_incident",
    "result": [
        {
            "transform_map": "Incident Transform Map",
            "table": "incident",
            "status": "error",
            "error_message": "Error during insert of incident (INC0010006); Target record not found"
        }
    ]
}

 

Moreover, I have also tried to make the number field 'coalesce' and it will work in an update but if I will again go for creating a ticket then it will not work, throwing the error:

{
    "import_set": "ISET0010004",
    "staging_table": "u_test_incident",
    "result": [
        {
            "transform_map": "Incident Transform Map",
            "status": "error",
            "error_message": "Unable to resolve target record, coalesce values not present: u_number; Invalid table ",
            "status_message": "Unable to resolve target record, coalesce values not present: u_number"
        }
    ]
}

 

Could anyone please help with this issue? Thanks.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.
2 REPLIES 2

Brian Lancaster
Tera Sage

I think you always need to send u_number even if you send it an empty string. 

something like "u_number": ""

Hi @Brian Lancaster , Thanks for the response.

SN_Learn_0-1685741872557.png

The number field's 'coalesce' is true. 
I am sending the below request body to create ticket:

{
    "u_number": "",
    "u_caller": "System Administrator",
    "u_assignment_group": "App Engine Admins",
    "u_impact": "2",
    "u_urgency": "2",
    "u_category": "Database",
    "u_short_description": "Postman SD",
    "u_sub_category": "Oracle"
}

The response is the postman is as below:

 

{
    "import_set": "ISET0010004",
    "staging_table": "u_test_incident",
    "result": [
        {
            "transform_map": "Incident Transform Map",
            "status": "error",
            "error_message": "Unable to resolve target record, coalesce values not present: u_number; Invalid table ",
            "status_message": "Unable to resolve target record, coalesce values not present: u_number"
        }
    ]
}
----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.