Best practice for updating/inserting a new key/value pair in the additional_info field in em_alert

Pablo Garbarino
Tera Contributor

Hi All... long time reader, first time poster.

 

I need some guidance in regards to the best approach/practice for updating the value of an existing key/value pair in the Additional Info field of an Alert (em_alert) record and inserting a new key/value pair to the same field.

 

We have a Business Rule running before insert of Alert records which parses the Additional Info key/values and outputs new ones (it enriches the Alert based on another table).

 

So far we have been using JSON.parse() to process the values in the "Additional Info" field, add new keys/values to the object and then use JSON.stringify() to update the field with the new string, but we've noticed that the output of stringify is slightly different to what is produced OOTB (it is basically missing the space before the ":")

 

We would like to keep the value of Additional Info standard.

 

We can .replaceAll('":','" :') but I imagine there must be a function that will produce the standard, OOTB "format" of that field.

 

 

Any ideas appreciated.

 

 

Cheers...

 

Pablo.

2 REPLIES 2

SunilSolanki
Tera Contributor

Hello  @Pablo Garbarino 

I am looking for appending new key value pair to additionalInfo field, facing some issues. Could you please share code, how you modified it.
 
 

Well, I'd probably recommend adding it via Event Rule, or Field Mapping, but if BR is what you need, I have used a couple options in the past, not sure what's best, but let you decide:

 

1.- additional_info is a string field so you can manipulate it as any string.

GR.setValue('additional_info',GR.getValue('additional_info').replace(/\n}$/,',\n "new_key" : "new value"\n}'));

 

2.- the one from the post... parse the JSON string, add the key/value, stringify updating the string to match how alerts

var addinfoObj=JSON.parse(GR.getValue('additional_info'));

addinfoObj['new_key'] = 'new value';

GR.setValue('additional_info',JSON.stringify(addinfoObj,null,1).replaceAll('":','" :'));

 

 

Cheers...

 

Pablo.