UI Builder Encoded Query for Update Record

Velma
Tera Guru

Building the encoded templateFields string for the Global Update Record in a client script (api.data.update_request_record.execute). I'm doing fine with short strings and sysIds. I need to know how I can encode multiline strings (textarea content) and dates--those are erroring. It works fine when I don't add those, but that does not get me to where I need to get. Are there function samples or documentation (or someone's video) for how to encode/prep them somewhere?  Or can you tell me, please.

1 ACCEPTED SOLUTION

Velma
Tera Guru

Processing a textarea for api.data.update (or create, I'll presume). This preserves the line breaks to the data long String field. The backslash and caret were the only special characters I could find that need escaping, YMMV--I tested everything obvious but not the extended character sets. This works well as far as getting to the database. I am annoyed about having to spend a lot of time figuring out how to make a Save work when the functionality should be built into it, but so it goes. To be able to use my text in the email text for a flow without again losing the carriage returns, there is another step. I can apparently only put one code block in a post, so that will be a following reply.

    function fixTextAreaForWrite(inputText) {
        let workingText = inputText;

        // Backslash MUST BE FIRST
        workingText = workingText.replace(/\\/g, "\\\\");
        // Carriage returns
        workingText = workingText.replace(/(\r\n|\r|\n)/g, "\\n");
        // Cannot make caret work, just remove
        workingText = workingText.replace(/\^/g, "");

        return workingText;
    }

View solution in original post

8 REPLIES 8

Velma
Tera Guru

Processing a textarea for api.data.update (or create, I'll presume). This preserves the line breaks to the data long String field. The backslash and caret were the only special characters I could find that need escaping, YMMV--I tested everything obvious but not the extended character sets. This works well as far as getting to the database. I am annoyed about having to spend a lot of time figuring out how to make a Save work when the functionality should be built into it, but so it goes. To be able to use my text in the email text for a flow without again losing the carriage returns, there is another step. I can apparently only put one code block in a post, so that will be a following reply.

    function fixTextAreaForWrite(inputText) {
        let workingText = inputText;

        // Backslash MUST BE FIRST
        workingText = workingText.replace(/\\/g, "\\\\");
        // Carriage returns
        workingText = workingText.replace(/(\r\n|\r|\n)/g, "\\n");
        // Cannot make caret work, just remove
        workingText = workingText.replace(/\^/g, "");

        return workingText;
    }

This works to escape the ^ in the string 

workingText = workingText.replace(/\^/g, "\\^");

gwalton
Tera Contributor

I also processed a textarea with api.data.update (and create) data resource execute for my "comment" field. I used the data resource in Script mode to encode the textarea value with the encodeURIComponent( ) function. Below is example script code for my "Update" data resource execute event:

 

function evaluateEvent({api, event}) {
    let sysID = api.state.your_selected_record.sys_id;
    let field = "comment="+ encodeURIComponent(api.state.comment_from_textarea);
    return {
        table: "{Your DB Table}",
        recordId: sysID,
        templateFields: field,
        useSetDisplayValue: false
    };
}
 
Next, create an "Insert" & "Update" Business Rule for your table to decode the field's input before it's saved to your DB table with the decodeURIComponent( ) function. Make sure your Business Rule "When to Run" tab configuration is similar to:
"When"... (before)
"Filter Condition"... (Your DB field) -> (changes)
"Insert"... (checked)
"Update"... (checked)
 
Below is the (Advanced Tab) example Business Rule script to decode the input for my "comment" field before inserting or updating the table:
 
(function executeRule(current, previous /*null when async*/) {
    var txt = "";
    if(current.comment.changes) {
        txt = decodeURIComponent(current.comment);
        //gs.addInfoMessage(txt);
        current.comment = txt;
    }
})(current, previous);
 
The nice thing is the decodeURIComponent( ) function does NOT alter any other (already) unencode text that's being saved to this DB field from another source. I hope this is helpful...

I'll be doing an update to that application (that contains the update we're talking about) soon. I'll definitely test this out to see if it works for me. Thank you.