How to set binary field in SN from Jira SD?

Originally asked by Andrew on 15 March 2021 (original question)


Hello community,

I have following payload from Jira SD:

"MoMe-Flag": {  
   "id": 11816,  
   "name": "MoMe-Flag",  
   "description": "for MoMe",  
   "type": "OPTIONS",  
   "value": [  
      {
        "id": "12399",  
        "sequence": 0,  
        "value": "Yes",  
        "disabled": false,  
        "childOptions": []  
      }  
   ]  
}

I have following incoming script on SN side:

if (replica.customFields."MoMe-Flag"?.value?.value == "Yes") {

incident.u_mome = true

} else {

incident.u_mome = false

}

but it doesn’t set u_mome field = true from above payload. u_mome is boolean type field.

Hopefully, someone can help.


Answer by Francis Martens (Exalate) on 16 March 2021

Are there any business rules around that field?
We have seen this in a number of cases where the business rule consequence is that ServiceNow silently ignores the update.


Comments:

Andrew commented on 16 March 2021

I did not find business rule around the field. incident.u_mome is new custom field.

When I added below two lines to my script, I was able to update description field with sent binary value:

def mome = replica.customFields.“MoMe-Flag”?.value?.value
incident.description = "MoMe: " + mome

and then description is MoMe: [Yes]

So I am able to display boolean value in text field, bt I cannot set with this value boolean field.

Francis Martens (Exalate) commented on 16 March 2021

Ah - replica.customFields.“MoMe-Flag”?.value?.value is an array.
Can it be that it is a multi value field?

Following code takes into account that

  • flag is null → false
  • flag is empty → false
  • flag has a value ‘true’ → true

It is untested … but should work

Untested

def flag = replica.customFields."MoMe-Flag"?.value?.value
incident.u_mome = flag?.size ? (flag[0] == "true") : false


Andrew commented on 17 March 2021

Hello,

Positive payload is above, negative (or rather empty) payload from Jira is below:

"MoMe-Flag": {          
"id": 11816,          
"name": "MoMe-Flag",          
"description": "for MoMe",          
"type": "OPTIONS",          
"value": []      },

I have added to incoming script with some “diagnostic” features as below:

def mome = replica.customFields."MoMe-Flag"?.value?.value.toString()

def flag1 = replica.customFields."MoMe-Flag"?.value?.value

def flag2 = flag1?.size ? (flag1[0] == "true") : false

if (mome == "[Yes]") {

incident.description = "1. mome true" + "\n" + "2. MoMe: " + mome + "\n" + "3. Flag1: " + flag1 + "\n" + "4. Flag2: " + flag2

incident.u_mome = flag1?.size ? (flag1[0] == "true") : false

} else {

incident.description = "a. mome false" + "\n" + "b. MoMe: " + mome + "\n" + "c. Flag1: " + flag1 + "\n" + "d. Flag2: " + flag2

incident.u_mome = flag1?.size ? (flag1[0] == "true") : false

}

So description field for positive payload looks as below:

  1. mome true
  2. MoMe: [Yes]
  3. Flag1: [Yes]
  4. Flag2: false

and for empty payload like here:

a. mome false

b. MoMe:

c. Flag1:

d. Flag2: false

So flag2 is always false and u_mome field is not updated to true.

Francis Martens (Exalate) commented on 18 March 2021

Check the logic.

flag2 will always be false because flag1 has the value ‘Yes’ and not true. So the expression
flag1[0] == “true”

Will always return false …

Andrew commented on 22 March 2021

Yes, you are right Francis.

I’ve corrected script so now is:

def flag2 = flag1?.size ? (flag1[0] == "Yes") : false

and

incident.u_mome = flag1?.size ? (flag1[0] == "Yes") : false

so now flag2 = true

but incident.u_mome still is false

Francis Martens (Exalate) commented on 22 March 2021

Can you try without the expression

incident.u_mome = true

And see if this is accepted by ServiceNow. There might be a business rule ignoring the value.