Transform Map Script - how to throw error back to client?

dazed
Kilo Contributor

Hi

I have an Import Set and Transform Map working into Change Request.

I want to introduced a validation step into the Transform to check the Change Request can be updated and if not valid return that fact back to the client.

Looking at Transformation script variables Documentation I have included

            error = true;
            error_message = "request is already set and doesn't match supplied value";

This stops the import and logs accordingly, but the response to client returns 201 - Created and a normal "result" data block, which I take to be the import record was created.

Is there a way to issue a different HTTP Response code (e.g. 500) and the error message?

 

Updated: I should indicate that I am currently using REST by POST to https://myinstance/api/now/table/my_app_change_import_rest?sysparm_exclude_reference_link=true

Thanks

 

 

1 ACCEPTED SOLUTION

Tony Cattoi
Mega Guru

I think if you are using the table API it will be difficult to return an error message in the way that you are describing since the POST message to the import table will always be successful.  The transform doesn't occur until after the record is loaded into your import set table so you wouldn't be able to return a response from the transform map through the API.

 

What you could do is look into the scripted REST APIs.  This would allow you to write a script to check the values from the POST message before returning a response.  

https://docs.servicenow.com/bundle/london-application-development/page/integrate/custom-web-services...

View solution in original post

4 REPLIES 4

Tony Cattoi
Mega Guru

I think if you are using the table API it will be difficult to return an error message in the way that you are describing since the POST message to the import table will always be successful.  The transform doesn't occur until after the record is loaded into your import set table so you wouldn't be able to return a response from the transform map through the API.

 

What you could do is look into the scripted REST APIs.  This would allow you to write a script to check the values from the POST message before returning a response.  

https://docs.servicenow.com/bundle/london-application-development/page/integrate/custom-web-services...

dazed
Kilo Contributor

Tony hi

Thanks yes, I that explanation makes sense.

The Scripted REST API looks like a possibility, which I may investigate. 

For my internal application use, for now, I have added an "already_linked" column to the import set and update source.already_linked = true; in the transform script.  This gets returned and I'm able to use that to determine the status of the import from that.

Thanks anyway.

 

Brian Workman
Tera Contributor

I realise that I am late to the game, here. But, I feel like we have a similar issue and I read the accepted answer, but I have a follow-up question to the logic presented by @Tony Cattoi 

 

Hi, Tony, you said


The transform doesn't occur until after the record is loaded into your import set table so you wouldn't be able to return a response from the transform map through the API.

 


In my API results from POSTing to my import set, it does in fact say

"sys_import_state": "error",
"sys_import_state_comment": "",
 
I would think that if it errored, it would also know what the error is? In this case, when I look at the transform for this import set, under Import Set Row Errors, it has ""object_access" is a mandatory field".
 
Is there no way to capture this and return this? Or at least have it describe what the error is in that "sys_import_state_comment" since it also reporting that the state is "error?"

Wow this is a blast from the past. Good question Brian. With the table api referenced in the original post, I still don’t think you would be able to guarantee you get a response back with the error since we would just be posting to the import set table itself and doesn’t wait for the transform.  Maybe if you did a get afterwards it would have that detail but there are other options now.  

It looks like in the import set API that you can get a response with this type of detail in many cases so that would be the place to look if you’re posting and wanting to know the result of the transform. https://docs.servicenow.com/bundle/washingtondc-api-reference/page/integrate/inbound-rest/concept/c_...