Sending JSON through Create record resource

Philippe Luickx
Tera Contributor

Hi,

 

I'm trying to create a record through the UI builder. I've followed the steps as in https://www.youtube.com/watch?v=ELScILWb-LA and it works except for this.

I need to send JSON. When defining the templateFields variable, it works if I pass a normal string. But a stringified JSON blocks. I have identified it's the double quotes " that breaks it.

Any way to fix this / workaround?

 

Thanks!

3 REPLIES 3

Brad Tilton
ServiceNow Employee
ServiceNow Employee

I think that property only takes an encoded query rather than JSON. Why do you need to send JSON and how is it currently formatted? (Might be helpful to post at least part of your code)

I'm integrating a map component and need to save a drawn area (geo feature), which is a js object that gets stored in json format. Save it in the backend in a record so I can display these records on another page.

Currently as a workaround I'm replacing double " with single ' and it gets through, do the same conversion when I need to display it. But it's a bit cumbersome.

 

Not working:

    const geo_json = event.payload.geojson;

    const geo_json_stringified = JSON.stringify(geo_json);
    
    const templateFields = `map_json=${geo_json_stringified}`;
    api.data.create_record_1.execute({
        "table": "x_map_area",
        "templateFields": templateFields,
        "useSetDisplayValue": false
    });
}

 

Workaround:

    const geo_json = event.payload.geojson;
    // double quotes are not passing... so change them with singles and we deal with that later
    const geo_json_stringified = JSON.stringify(geo_json).replaceAll('"',"'");
    
    const templateFields = `map_json=${geo_json_stringified}`;
    api.data.create_record_1.execute({
        "table": "x_map_area",
        "templateFields": templateFields,
        "useSetDisplayValue": false
    });
}

blaine2
Tera Contributor

Tilton is correct that the fieldType is condition_string, but that is troublesome and misleading since the purpose is not to set conditions such as ">=" or "CONTAINS", but only assignments.  It's extremely non-intuitive and quirky, but you can use the UI Builder editor in form mode to assign multiple fields by adding multiple 'is' criteria joined with 'and'.  Each "is" actually does an assignment.  As the whole situation of using a condition editor to set assignments is crazy, I recommend changing the fieldType of the 'Update Record' sys_ux_data_broker_graphql to string so you can edit assignments sanely like: field1=val1^field2=val2.

 

Regarding special characters, the reported issue is not JSON-specific.  You will have this issue if you try to assign any string value containing double-quote, equal, or carat characters.  You can use double-quotes by escaping them-- don't need to replace them with something else.  Same applies to carat and equals.

 

Same constraint as JSON encoding, binary characters aren't supported so JavaScript strings like "one\ntwo" won't work (in fact will silently fail).  You must  pass backslash character escapes to NE, so you need to pass string like "one\\ntwo" and if coding the string in JavaScript then it would be "one\\\\ntwo".  To write final field value "one\two" you would need to send string "one\\two" which could be coded like "one\\\\two" in JavaScript.

 

Working example:

({api}) => {
const inStr = `a0\\\\e=ga,\\nde^ga"ep`;
const neCharEscape = s =>
s.replace(/\^/g, "\\^").replace(/"/g, '\\"').replace(/=/g, "\\=");
return {
table: "u_fu",
recordId: "5d95bbcc07dce1100049f9fc7c1ed029",
templateFields: `u_pre=${neCharEscape(inStr)}`,
};
}