- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2024 08:03 AM - edited 07-18-2024 08:32 AM
Hi,
I'm integrating two ServiceNow instances, where I have a requirement to update knowledge articles through Rest Outbound.
Here I'm using PUT Method and through the BR trying to set the field values. However it's setting the values except 'workflow_state' and 'Version' fields. Here is the Business rule.
Business Rule:
(function executeRule(current, previous /*null when async*/ ) {
try {
var request = new sn_ws.RESTMessageV2('Articles Integration with instance 2', 'Update Article');
request.setStringParameter('sys_id', current.u_co_id);
var request_payload = {
"kb_knowledge_base": current.kb_knowledge_base.toString(),
"short_description": current.short_description.toString(),
"version": current.version.toString(),
"confidence": current.confidence.toString(),
"workflow_state": workflow_state.toString()
};
var request_body = JSON.stringify(request_payload);
request.setStringParameterNoEscape('request_payload', request_body);
var response = request.execute();
var requestBody = request.getRequestBody();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log(responseBody);
gs.log(httpStatus);
responseJSON = responseBody.substring(10, responseBody.length - 1);
parsedJSON = JSON.parse(responseJSON);
var targetArticlesysid = parsedJSON['sys_id'];
var logString = 'Target Article sys id - ' + targetArticlesysid;
current.u_co_id = targetArticlesysid;
current.update();
} catch (ex) {
var message = "Article not updated in instance 2" + current.number + ". Error Message: \n " + err;
gs.log(message);
}
})(current, previous);
Can anyone help please!!
Thanks 🙂
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 02:49 AM
It's sorted. There is an ACL which removes the write access to the field workflow_state to everyone [admin can override].
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-18-2024 08:41 AM
GlideRecord.setWorkflow(false) would be something that could only be put in place on the target instance. Your Knowledge Articles may not be workflow driven, so I wouldn't pay too much attention to the word "workflow" as .setWorkflow() impacts Business Rules as well.
On the target instance you could create a Business Rule on the kb_knowledge table similar to the following:
When: Before
Insert = False
Update = True
Order: -100 ** This may need updating but start there as I don't know where workflow_state is being overridden**
Condition: gs.getUserID() == 'sys_id_of_integration_user'
Script:
current.setWorkflow(false);
current.update();
This should mean Business Rules/Workflows don't run for the KA updated on the target instance if the integration user is performing the update. You should never normally use current.update() on a Before Business Rule but the setWorkflow(false) means it's not recursive. This would bypass any other Business Rules that are overriding the setting of "workflow_state" to set but it would also mean you might not get other updates that should otherwise happen in the background. I don't much like the above solution as Business Rules not running could have undesirable consequences. It would be best to understand why the workflow_state changes are not taking as expected and correct that. You could start by trying to manually set the workflow_state directly in the target instance via background script and go from there.
Can the Knowledge Articles be updated on the target instance in addition to the source instance? Putting the workflow_state issue to one side i seems that this would also be a problem if the target instance is not locked down as the versions could deviate between instances due to updates being performed directly on the target instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 01:44 AM
Thank you for the suggestion!
Here I have few points to highlight.
1. The provided code is not updating the field 'workflow_state', added some infomessages even not getting them on the screen.
2. When I perform update actions on other fields from source to target (through the BR I mentioned), they are getting updated except version and workflow_state.
3. Yes, tried with Background script which is updating the workflow_state without even setting setWorkflow to false.
Here is the code I used in BG Script
var gr = new GlideRecord('kb_knowledge');
gr.addEncodedQuery('sys_class_name!=kb_knowledge_block^number=KB0010012');
gr.query();
if (gr.next()) {
gs.print('current state ' + gr.workflow_state);
gr.workflow_state = 'published';
gr.update();
gs.print('new state ' + gr.workflow_state);
}
OUTPUT:
Was wondering which was blocking the target instance to change the state.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 02:12 AM - edited 07-22-2024 02:21 AM
With your background script example it would be worth including the other fields the integration would usually update to see if this changes things. I was expecting the background script to fail, especially since the Before Business Rule didn't work.
You look to be using a scripted web service, do you know for sure that this is not being blocked on the target instance?
Also noticed something that could be a script error in the above:
workflow_state.toString()
Should this be current.workflow_state.toString()?
You could create a -10000 order Before Business Rule to log out what the values of current are before any other operation occurs, although it's worth checking the error above before expending effort doing that
I don't have Knowledge Management Advanced on my PDI, so will need to wait for that to install to be able to say more about version. If this resolves the issue with workflow_state then there might be something particular about version that means it doesn't map. Is this a text field or a reference field? If its a reference field it might be that it is mapping and it just doesn't appear to be populating on the target instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2024 02:49 AM
It's sorted. There is an ACL which removes the write access to the field workflow_state to everyone [admin can override].