Update incident record through api

1_DipikaD
Kilo Sage

Hi All,

 

I have requirement that in Incident table if channel is phone, Category is hardware / software and sub category is Email/IP address then record must be updated successfully  Otherwise it should through error response. Please help me with the script .

 

Thank you

3 REPLIES 3

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @1_DipikaD , 
Can you please what you tried till now? and where you are facing a issue while working with API. Meanwhile you can check below video tutorial which show how to use table API. may be you should be able to find your answer here:

Incident Table API: Learn How To Leverage ServiceNow Integration! 
Table API Integration ServiceNow 

 

Kindly mark the answer ✔️ Correct or Helpful ✔️ If it addresses your concern.


Regards,

Siddhesh

Ankur Bawiskar
Tera Patron
Tera Patron

@1_DipikaD 

you can use data policy for this or before update business rule

Are you using scripted REST API? if yes then you can use scripted rest api script itself

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Selva Arun
Mega Sage
Mega Sage

 

 Hi Dipika,

To meet your requirement of updating an incident record through the API with specific conditions (channel = phone, category = hardware/software, and subcategory = Email/IP address), you can handle this using either a Scripted REST API or a Business Rule. Here’s a quick overview of both approaches:

 

Option 1: Scripted REST API

You can create a Scripted REST API to handle the update and include the validation logic directly in the script. The API will check if the conditions are met (channel, category, and subcategory) before updating the record. If the conditions aren’t satisfied, it will return an error response. This approach is ideal if you want to control updates specifically through the API.

 

Sample code:

(function process(request, response) { 

    var requestBody = request.body.data; 

 

    // Extract fields from the request 

    var sysId = requestBody.sys_id; 

    var channel = requestBody.channel; 

    var category = requestBody.category; 

    var subCategory = requestBody.sub_category; 

 

    // Validate conditions 

    if (channel === 'phone' &&  

        (category === 'hardware' || category === 'software') &&  

        (subCategory === 'Email' || subCategory === 'IP address')) { 

         

        // Update the incident record 

        var incident = new GlideRecord('incident'); 

        if (incident.get(sysId)) { 

            incident.channel = channel; 

            incident.category = category; 

            incident.subcategory = subCategory; 

            incident.update(); 

 

            response.setStatus(200); 

            response.setBody({ status: 'success', message: 'Incident updated successfully.' }); 

        } else { 

            response.setStatus(404); 

            response.setBody({ status: 'error', message: 'Incident not found.' }); 

        } 

    } else { 

        response.setStatus(400); 

        response.setBody({  

            status: 'error',  

            message: 'Invalid data. Ensure channel is "phone", category is "hardware/software", and subcategory is "Email/IP address".'  

        }); 

    } 

})(request, response); 

 

Option 2: Business Rule

If you want to enforce this logic across all updates (not just through the API), you can use a Before Update Business Rule. The rule will validate the conditions and prevent the update if they aren’t met, displaying an error message to the user. This ensures consistency regardless of how the record is updated.

 

Additional Tips

  • If you’re using the Table API, you can also enforce the same logic using a Data Policy or a Business Rule.
  • Always test your implementation in a development environment before deploying it to production.
  • For testing Scripted REST APIs, you can use the REST API Explorer in ServiceNow.

If you believe the solution provided has adequately addressed your query, could you please **mark it as 'Helpful'** and **'Accept it as a Solution'**? This will help other community members who might have the same question find the answer more easily.

Thank you for your consideration.

Selva Arun