Business Rules not working when convert HTML to String in Problem table

Nurulaslah Anua
Tera Contributor

The fix notes and cause notes fields in the Problem table are currently set as HTML format.

To prevent HTML formatting from appearing in generated reports, I created custom fields named 'String Fix Notes' and 'String Cause Notes' (both of which are string data types). These new fields do not overwrite the original HTML fields.

While testing with a sys_id in a script background, the conversion from HTML to string worked as expected.

However, the conversion does not work when I add it to a BR and create a new ticket.
The new fields are blank, indicating that the conversion from HTML to string was not performed, and the values were not updated in the custom fields.


Here are my BR script:

------------------------------------------

(function executeRule(current, previous /*null when async*/ ) {


function decodeHTML(str) {
var a = str.replace(/<\/?[^>]+(>|$)/g, ""); //Remove tags
var b = a.replace(/&amp;/g, '&'); //Retain any ampersands that are just ampersands
return b.replace(/&#(\d+);/g, function(match, dec)
{
return String.fromCharCode(dec); //Returns the special character from the decimal code representation and returns the entire decoded string.
});
}

var grProblem = new GlideRecord('problem');
//grProblem.get('xxxxxxxx'); //replace xxx with sys_id

grProblem.addEncodedQuery('fix_notesISNOTEMPTY^ORcause_notesISNOTEMPTY');
grProblem.query();

if (grProblem.next()) {
// Decode the HTML content in the cause_notes and fix_notes fields
var decodedCauseNotes = decodeHTML(grProblem.cause_notes);
var decodedFixNotes = decodeHTML(grProblem.fix_notes);

// Set the decoded value to the new custom field
grProblem.u_cause_notes_string = decodedCauseNotes;
grProblem.u_string_fix_notes = decodedFixNotes;

// Update the current record
grProblem.update();

} else {
// Handle the case where the problem record doesn't exist
gs.info('Problem record not found.');
}
})(current, previous);

 

-------------------------------------------

Note: I added gs.log on each line and its returned value without any error.

 

When to run:

BR_when to run.png

 

Can you please help why the new fields are still blank?

 

Thank you in advance

1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

If you're modifying data for the current record, you should be doing this in a BEFORE business rule, and not calling current.update();

 

(function executeRule(current, previous /*null when async*/ ) {

    function decodeHTML(str) {
        var a = str.replace(/<\/?[^>]+(>|$)/g, ""); //Remove tags
        var b = a.replace(/&amp;/g, '&'); //Retain any ampersands that are just ampersands
        return b.replace(/&#(\d+);/g, function(match, dec) {
            return String.fromCharCode(dec); //Returns the special character from the decimal code representation and returns the entire decoded string.
        });
    }

    if (current.cause_notes.changes()) {
        current.setValue('u_cause_notes_string', decodeHTML(current.getValue('cause_notes')));
    }

    if (current.fix_notes.changes()) {
        current.setValue('u_string_fix_notes', decodeHTML(current.getValue('fix_notes')));
    }
})(current, previous);

View solution in original post

4 REPLIES 4

Kieran Anson
Kilo Patron

If you're modifying data for the current record, you should be doing this in a BEFORE business rule, and not calling current.update();

 

(function executeRule(current, previous /*null when async*/ ) {

    function decodeHTML(str) {
        var a = str.replace(/<\/?[^>]+(>|$)/g, ""); //Remove tags
        var b = a.replace(/&amp;/g, '&'); //Retain any ampersands that are just ampersands
        return b.replace(/&#(\d+);/g, function(match, dec) {
            return String.fromCharCode(dec); //Returns the special character from the decimal code representation and returns the entire decoded string.
        });
    }

    if (current.cause_notes.changes()) {
        current.setValue('u_cause_notes_string', decodeHTML(current.getValue('cause_notes')));
    }

    if (current.fix_notes.changes()) {
        current.setValue('u_string_fix_notes', decodeHTML(current.getValue('fix_notes')));
    }
})(current, previous);

This code is working perfectly. Thank you so much @Kieran Anson 

 

Aniket Chavan
Tera Sage
Tera Sage

Hello @Nurulaslah Anua ,

Kieran has a valid point. It would be better to use a Before Business Rule instead of calling current.update(). This way, you can change the current record before it gets saved, which helps avoid any issues with conflicts or recursion.

 

Also, remember to check if the cause_notes and fix_notes fields have changed before you process them. This ensures that you only update the new fields when there’s actually new or modified data, which can help keep things efficient and accurate.

 

(function executeRule(current, previous /*null when async*/) {
    
    function decodeHTML(str) {
        // Remove HTML tags
        var a = str.replace(/<\/?[^>]+(>|$)/g, ""); 
        // Decode any ampersands and special characters
        var b = a.replace(/&amp;/g, '&'); 
        return b.replace(/&#(\d+);/g, function(match, dec) {
            return String.fromCharCode(dec);
        });
    }

    // Process cause_notes if it has changed
    if (current.cause_notes.changes()) {
        var decodedCauseNotes = decodeHTML(current.cause_notes.toString());
        current.u_cause_notes_string = decodedCauseNotes;
    }

    // Process fix_notes if it has changed
    if (current.fix_notes.changes()) {
        var decodedFixNotes = decodeHTML(current.fix_notes.toString());
        current.u_string_fix_notes = decodedFixNotes;
    }

})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.


Regards,
Aniket

 

 

Thank you so much @Aniket Chavan for your explanation.