ServiceNow, How to populate value to a list variable - servicenow

I am using ServiceNow develop instance to create a table, in which one field is type of list. The values of the list is populated by Outgoing Rest message.
In business rule, I make a hard coded to see how list values shall be what kind of form. The codes lists as below/1/: but when I update the field computer room, the computerlist's value is not populated. It looks like in the image/2/. How to populate values to a list type of field?
/3/ is the json string returned in pretty format. I try to populate hostName to computerlist, but failed.
/1/ scripts in business rule:
(function executeRule(current, previous /*null when async*/ ) {
try {
var r = new sn_ws.RESTMessageV2('x_383581_aiademo2.AIACmdbReq', 'Default GET');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
// var responseObj = JSON.parse(responseBody);
current.computerlist = new Array;
current.computerlist[0] = "c1";
current.computerlist[1] = "c2";
current.computerlist[2] = "c3";
current.assignto = "How to make it as list";
current.update();
} catch (ex) {
var message = ex.message;
}
})(current, previous);
/2/
/3/
{
"code": 200,
"data": {
"dataList": [
{
"hostName": "MysqlServer",
"deviceIp": "192.168.1.40",
"site": "SH",
"hostId": "00000000",
"location": "Room01",
"id": 9381947
},
{
"hostName": "192.168.1.32",
"deviceIp": "192.168.1.32",
"site": "SH",
"hostId": "a8c02001",
"location": "66666",
"id": 9381950
},
{
"hostName": "back-server",
"deviceIp": "192.168.1.42",
"site": "SH",
"hostId": "00000000",
"location": "Room01",
"id": 9381996
},
{
"hostName": "192.168.1.32",
"deviceIp": "192.168.1.32",
"site": "SH",
"hostId": "00-0C-29-E0-31-32",
"location": "Room01",
"id": 9382011
},
{
"hostName": "core-server1",
"deviceIp": "192.168.1.30",
"site": "SH",
"hostId": "00000000",
"location": "Room01",
"id": 9382014
}
]
},
"msg": "success"
}
/4/ business rule script is updated to:
(function executeRule(current, previous /*null when async*/ ) {
try {
var r = new sn_ws.RESTMessageV2('x_383581_aiademo2.AIACmdbReq', 'Default GET');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var responseObj = JSON.parse(responseBody);
current.computerlist = responseObj.data.dataList.hostName;
current.assignto = responseObj.code;
current.update();
} catch (ex) {
var message = ex.message;
}
})(current, previous);

The answer is as follows:
(function executeRule(current, previous /*null when async*/ ) {
try {
var r = new sn_ws.RESTMessageV2('x_383581_aiademo2.AIACmdbReq', 'Default GET');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var responseObj = JSON.parse(responseBody);
var listLength = responseObj.data.dataList.length;
current.responsetest = responseObj.data.dataList[0].hostName;
var temp = new Array(listLength);
for(var j = 0; j<listLength; j++){
temp[j] = responseObj.data.dataList[j].hostName;
}
current.computerlist.setValue(temp);
current.assignto = temp[0];
current.vers = "0.0.1a";
current.description = responseObj.msg;
current.update();
////////////////////////good way///////////////////////////////
///////////current.computerlist.setValue(["1","2","3","4","5"]);
///////////////////////////////////////////////////////////////
} catch (ex) {
var message = ex.message;
}
})(current, previous);

Related

"Key" on a BucketAggregate is missing when the response is serialized

I am working on upgrading a service from ES 5.0 to 6.8. I have a bucket aggregate that in v5 serializes to this:
"items": [
{
"key": "random+topic",
"docCount": 27919,
"aggregations": {
"ParentReference": {
"docCount": 24992,
"aggregations": {
"Popularity": {
"value": 25223
}
}
}
}
},
{
"key": "unknown problem+latency",
"docCount": 24566,
"aggregations": {
"ParentReference": {
"docCount": 23419,
"aggregations": {
"Popularity": {
"value": 23931
}
}
}
}
},
With the v6 of Elasticsearch.Net and Nest, when serialized, I end up with:
"items": [
{
"ParentReference": {
"Popularity": {
"value": 25223
}
}
},
{
"ParentReference": {
"Popularity": {
"value": 23931
}
}
},
I had previously encountered the issue where the "aggregations" property is no longer returned (though I would love a pointer to the breaking changes announcement on that), and have updated my code accordingly. I can't do much without the Key and docCount, however. I figure there must be something related to the Json parsing changes.
I have already tried the steps in:
Custom Serialization | Elasticsearch.Net and NEST: the .NET clients [6.x] | Elastic
I have tried with the default serializer, as well as a custom one using the JsonNetSerializer.Default to no effect.
Can anyone provide a suggestion on what I should be doing?
note that this is how I am getting my BucketAggregate:
var childAgg = response.Aggregations[ss.Type] as SingleBucketAggregate;
var nestedAgg = childAgg.Aggregations[ss.Path] as SingleBucketAggregate;
var countAgg = nestedAgg.Aggregations[ssTermsName] as BucketAggregate;
return new ProviderResult<BucketAggregate>
{
Result = countAgg,
};
Thank!
~john
additional:
ElasticClient elasticClient_BuiltInSerializer = new ElasticClient();
var source = elasticClient_BuiltInSerializer.SourceSerializer.SerializeToString(o);
var response = elasticClient_BuiltInSerializer.RequestResponseSerializer.SerializeToString(o);
ConnectionSettings connectionSettings = new ConnectionSettings(new SingleNodeConnectionPool(new Uri("http://fake")), JsonNetSerializer.Default);
ElasticClient elasticClient_JsonNetSerializer = new ElasticClient(connectionSettings);
var source2 = elasticClient_JsonNetSerializer.SourceSerializer.SerializeToString(o);
var response2 = elasticClient_JsonNetSerializer.RequestResponseSerializer.SerializeToString(o);

How to assign a single object to a list?

I'm working with an API rest and it returns me two types of json, object and array.
Object(When there is only one record in the database):
{ "complain": { "id": "1" , "description": "hello", "date": "2017-01-24 11:46:22", "Lat": "20.5204446", "Long": "-100.8249097" } }
Array(When there is more than one record in the database):
{ "complain": [ { "id": "1" , "description": "hello", "date": "2017-01-24 11:46:22", "Lat": "20.587446", "Long": "-100.8246490" }, { "id": "2" , "description": "hello 2", "date": "2017-01-24 11:50:12", "Lat": "20.529876", "Long": "-100.8249097" } ] }
The code I use to consume the json is as follows:
content = await response.Content.ReadAsStringAsync();
var token = JToken.Parse(content);
if (token["complain"] is JArray)
{
var jsonArray = JsonConvert.DeserializeObject<RootArray>(content);
}
else if (token["complain"] is JObject)
{
var jsonObject = JsonConvert.DeserializeObject<RootObject>(content);
}
When it comes to a json array if I can add it to a listview:
myList.ItemsSource = jsonArray.listArray;
But if it is an object I can not and I get the following error:
Cannot implicitly convert type Object to IEnumerable.
Finally I was able to solve my error, it was just a matter of creating a list and adding the deserialized json object.
var jsonObject = JsonConvert.DeserializeObject<RootObject>(content);
List<Complain> simpleList = new List<Complain>();
simpleList.Add(jsonObject.ComplainObject);
myList.ItemsSource = simpleList;
The class to deserialize the Json object:
public class RootObject
{
[JsonProperty("complain")]
public Complain ComplainObject { get; set; }
}

Handsontable: Updating a cell renderer at runtime

I'm using handsontable, and I want to change the background color of a cell if its value is edited and changed. I can do this easily if my data source is an array of arrays (see fiddle: http://jsfiddle.net/chiman24/3o2c3c7m/).
document.addEventListener("DOMContentLoaded", function() {
// Row Styles
var blank = function(instance, td, row, col, prop, value,
cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.style.backgroundColor = '#ABAAAA'
};
var align = function(instance, td, row, col, prop, value,
cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.style.verticalAlign = 'middle';
td.style.fontWeight = 'bold';
};
var highlight1 = function(instance, td, row, col, prop, value,
cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.style.backgroundColor = '#BDD7EE';
td.style.textAlign = 'right';
};
var changedBackgroundColor = '#cbd9e4';
var defaultBackgroundColor = 'white';
var hasChanged = function(instance, td, row, col, prop, value,
cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.style.backgroundColor = changedBackgroundColor;
};
var noChange = function(instance, td, row, col, prop, value,
cellProperties) {
Handsontable.renderers.TextRenderer.apply(this, arguments);
td.style.backgroundColor = defaultBackgroundColor;
};
var data = [
["1", "Hear us from heaven", "New Life Worship",
"Anderson, Jared", "something"
],
["2", "Spirit Break Out", "Kim Walker", "Walker, Kim",
"Still Believe"
]
],
dataCopy = [
["1", "Hear us from heaven", "New Life Worship",
"Anderson, Jared", "something"
],
["2", "Spirit Break Out", "Kim Walker", "Walker, Kim",
"Still Believe"
]
],
container = document.getElementById('example1'),
hot1;
//Table Row and Col Options
hot1 = new Handsontable(container, {
data: data,
fixedColumnsLeft: 1,
columnSorting: true,
colHeaders: ["id", "title", "artist", "author", "album"],
columns: [{
type: "text"
}, {
type: "text"
}, {
type: "text"
}, {
type: "text"
}, {
type: "text"
}]
});
hot1.addHook('afterChange', afterChange);
function afterChange(changes, source) {
if (source == 'edit' || source == 'autofill') {
$.each(changes, function(index, element) {
var change = element;
var rowIndex = change[0];
var columnIndex = change[1];
var oldValue = change[2];
var newValue = change[3];
var cellChange = {
'rowIndex': rowIndex,
'columnIndex': columnIndex
};
if (oldValue != newValue) {
var cellProperties = hot1.getCellMeta(
rowIndex, columnIndex);
if (newValue != dataCopy[rowIndex][
columnIndex
]) {
cellProperties.renderer = hasChanged;
} else { //data changed back to original value.
cellProperties.renderer = noChange;
}
hot1.render();
}
});
}
}
});
// noSideScroll class added to fix some containers while side scrolling the table
$(window).scroll(function() {
$('.noSideScroll').css({
'left': $(this).scrollLeft()
});
});
However, when using an array of objects, I can't get it to work. (see fiddle: http://jsfiddle.net/chiman24/24mpavga/).
var data = [{
"id": 1,
"title": "First Loved Me",
"artist": "Israel and New Breed",
"author": "Houghton, Israel",
"album": "Covered: Alive In Asia"
}, {
"id": 2,
"title": "One Thing Remains",
"artist": "Israel and New Breed",
"author": "Houghton, Israel",
"album": "Covered: Alive In Asia"
}],
dataCopy = [{
"id": 1,
"title": "First Loved Me",
"artist": "Israel and New Breed",
"author": "Houghton, Israel",
"album": "Covered: Alive In Asia"
}, {
"id": 2,
"title": "One Thing Remains",
"artist": "Israel and New Breed",
"author": "Houghton, Israel",
"album": "Covered: Alive In Asia"
}],
container = document.getElementById('example1'),
hot1;
//Table Row and Col Options
hot1 = new Handsontable(container, {
data: data,
fixedColumnsLeft: 1,
columnSorting: true,
colHeaders: ["id", "title", "artist", "author", "album"],
columns: [{
data: "id"
}, {
data: "title"
}, {
data: "artist"
}, {
data: "author"
}, {
data: "album"
}]
});
hot1.addHook('afterChange', afterChange);
function afterChange(changes, source) {
if (source == 'edit' || source == 'autofill') {
$.each(changes, function(index, element) {
var change = element;
var rowIndex = change[0];
var columnIndex = change[1];
var oldValue = change[2];
var newValue = change[3];
var cellChange = {
'rowIndex': rowIndex,
'columnIndex': columnIndex
};
if (oldValue != newValue) {
var cellProperties = hot1.getCellMeta(
rowIndex, columnIndex);
if (newValue != dataCopy[rowIndex][
columnIndex
]) {
cellProperties.renderer = hasChanged;
} else { //data changed back to original value.
cellProperties.renderer = noChange;
}
hot1.render();
}
});
}
}
Is there a way to accomplish this? I want to get it working using an array of objects because my data coming from the server will be in JSON format. I've scoured the handsontable documentation for a couple of days to no avail. Any help will be much appreciated. Thanks.
I got some help from the handsontable github forum.
Apparently, if the data source is an array of objects, then when calling "getCellMeta", instead of passing in a numerical column index, you have to pass in the column index as a property like this:
hot.getCellMeta(2, hot.propToCol(columnIndex));
Here's the updated demo
Other way to change background color of cell is use the Cell Option
...
if (oldValue != newValue){
aCell.push(
{ row: rowIndex,
col: hot.propToCol(columnIndex),
className: "cssWithBackgroundColor" });
hot.updateSettings({ cell: aCell });
}
If the user undo change you can
if ( source == 'UndoRedo.undo'){
aCell.pop();
hot.updateSettings({ cell: aCell });
}

ElasticSearch NEST library to retrieve certain fields

Trying to achieve following,
Retrieve articles that match given id and genre
Retrieve selected fields for matching records
I have tried this with Sense(chrome plugin),
POST /d3acampaign/article/_search
{
"fields": ["title","genre"] ,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [{
"term": {
"id": "6"
}
},
{
"term": {
"genre": "metal"
}
}]
}
}
}
}
}
For C# code i am trying to build the query using following construct,
FilterContainer fc = null;
TermFilter title = new TermFilter()
{
Field = "id",
Value = "6",
};
TermFilter genre = new TermFilter()
{
Field = "genre",
Value = "metal",
};
fc = title & genre;
QueryContainer qc = new FilteredQuery() { Filter = fc };
var searchRequest = new SearchRequest
{
SearchType = Elasticsearch.Net.SearchType.QueryAndFetch,
Query = qc,
Indices = new IndexNameMarker[] {"journal"},
Types = new TypeNameMarker[] { "article" },
};
var r = client.SearchAsync<Article>(searchRequest);
var l = (List<Article>) r.Result.Documents;
I am able to run this query and get matching records, but i amnt sure how to specify selected fields to retrieve. Let me know what can be changed in C# code to specify necessary fields.
Thanks in advance.
Based on this answer you can modify your request object as follow:
var searchRequest = new SearchRequest
{
...
Fields = new List<PropertyPathMarker>
{
Property.Path<Article>(p => p.YourField)
}
};
or if you will decide to use source filtering:
var searchRequest = new SearchRequest
{
...
Source = new SourceFilter
{
Include = new []
{
Property.Path<Article>(p => p.YourField)
}
}
};
Hope it helps.

How to create *static* property in can.Model

I need somehow to store metadata in the can.Model
I use findAll method and receive such JSON:
{
"metadata": {
"color": "red"
},
"data": [
{ "id": 1, "description": "Do the dishes." },
{ "id": 2, "description": "Mow the lawn." },
{ "id": 3, "description": "Finish the laundry." }
]
}
I can work with data like can.Model.List, but I need metadata like a static property or something.
You can use can.Model.parseModels to adjust your response JSON before it's turned into a can.Model.List.
parseModels: function(response, xhr) {
var data = response.data;
var metadata = response.metadata;
var properties;
if(data && data.length && metadata) {
properties = Object.getOwnPropertyNames(metadata);
can.each(data, function(datum) {
can.each(properties, function(property) {
datum[property] = metadata[property];
});
});
}
return response;
}
Here's a functional example in JS Bin: http://jsbin.com/qoxuju/1/edit?js,console

Resources