How can I change sys_created_by for incident(task) comment added through API

aaronstanley
Kilo Explorer

I have seen this question asked before but I cannot fond an definitive answer. here's the scenario:

We have an API user that performs all gets, posts and puts, we'll call it APIMan.

When a user looks up an existing incident we capture their sys_id and userid through an API call to the user table.

We can add a comment via the API but I'd like to change the sys_created_by field to reflect the user and not APIMan.

When adding the comment the JSON object is {"comments":"This is the comment"}. The comments string is added to the sys_journal_field table as the "value" field so there is a conversion from "comments" to "value".

Is there some conversion from a JSON string to the "sys_created_by" field in the Journal Entry table? {"comments":"This is a comment","sys_created_by":"userid or sys_id"} does not change the sys_created_by field value in the table. Should "sys_created_by" be something different?

I'd prefer to not force a log-in to SN for the user if I can help it.

1 ACCEPTED SOLUTION

ark6
Mega Guru

okay, i believe you can do that.



The concept is that sys_created_by is a system field and cannot be changed directly.



For this you need to use <object>.autoSysFields(false) to forcefully update the field;



I believe the API provided to you is not a direct API from SNOW, but a scripted one.



If it is a scripted one, you need to include autosysFields(false) in the script and if its a direct one, just include a before insert businessrule and use autoSysFields(false) there



View solution in original post

3 REPLIES 3

ark6
Mega Guru

okay, i believe you can do that.



The concept is that sys_created_by is a system field and cannot be changed directly.



For this you need to use <object>.autoSysFields(false) to forcefully update the field;



I believe the API provided to you is not a direct API from SNOW, but a scripted one.



If it is a scripted one, you need to include autosysFields(false) in the script and if its a direct one, just include a before insert businessrule and use autoSysFields(false) there



This is a frustrating task. If I provide Admin rights to the REST user I can make this work. I guess I just don't feel the Journal entry table should be a system table.



I'm throwing in the towel on this one. We're going to just prepend the user's Name or username to each comment and leave comments added through the API as comments added by our REST user. It's not pretty but in our case it's just to provide some integration with a homegrown lobby check-in system.



Thanks for the help. Your answer is correct provided the user has the correct rights.


TPyne
Giga Contributor

 

I tried this and found one unexpected behavior. 

My script is also adding comments. When I add autosysFields(false) the comments are no longer added.

 

If I remove the autosysFields(false) line comments are added.

 

var gr = new GlideRecord("incident");
gr.initialize();

gr.autoSysFields(false); // allows us to override created by

gr.setValue("short_description", "Test 123 Security");
gr.getElement("sys_created_by").setDisplayValue("Secureworks Integration User"); // Solves scoping issues


gr.getElement("comments").setDisplayValue("Generated by Script Test."); 
gr.getElement("comments").setDisplayValue("Generated by Script Test2.");
gr.insert();
gr.autoSysFields(true);
gs.info("Incident Created " + gr.number);

 

 

// If I comment out the autoSysField lines the comments are added as normal, weird