The CreatorCon Call for Content is officially open! Get started here.

How to convert HTML field value to String value for Description field from catalog item

SM123
Tera Expert

Hi,

URGENT : I have a business rule for to populate description field from catalog item to service request form. but the problem is the description field from catalog item is html type so when i populate it in service request form it is showing value in html form. could anyone help me how to resolve this? what script should i add on to this? 

provided the BR i used below

 

 

19 REPLIES 19

Hi @Karunakaran ,

I tried below script but still not working. it is populating html field value with all tags. please check attached ss.

(function executeRule(current, previous /*null when async*/ ) {
 
    // Add your code here
    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('request', current.sys_id);
    gr.query();
 
    if (gr.next()) {
        var description = gr.cat_item.description;
        current.description = gr.cat_item.description.trim();
       current.update();
    }
 
})(current, previous);

Hi @SM123 ,

 

You need to add this trim after the replace statement.

 

it is like this

 

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

// Add your code here
var gr = new GlideRecord('sc_req_item');
gr.addQuery('request', current.sys_id);
gr.query();

if (gr.next()) {
var descriptionString = gr.cat_item.description.replace(/<\/?[^>]+(>|$)/g, "");
current.description = descriptionString.trim();

}

})(current, previous);

 

Thank you.

Amit Pandey
Kilo Sage

Hi @SM123 

 

Just use this function to pass your html body and it will return you content in text:

 

function getFinalText(text) {
    // Replace all <br /> tags with a CRLF
    var regX1 = /<br\s\/>/ig;
    var text2 = text.replace(regX1, String.fromCharCode(13));
    // Replace all remainging HTML tags with ""
    var regX2 = /(<([^>]+)>)/ig;
    var finalText = text2.replace(regX2, "");
    return finalText;
}

 

Please mark my answer helpful and correct.

 

Regards,

Amit

Hi @Amit Pandey ,

Thanks for your ans. Actually i'm new to scripting. Could you add the above script to my BR. so i can use it and try. 

(function executeRule(current, previous /*null when async*/ ) {
 
    // Add your code here
    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('request', current.sys_id);
    gr.query();
 
    if (gr.next()) {
       current.description = gr.cat_item.description;
       current.update();
    }
 
})(current, previous);

 

Hi @SM123 

 

You should avoid using current.update() in BRs.

 

Please check the following script, though I have not tested it-

 

(function executeRule(current, previous /*null when async*/ ) {
 
    // Add your code here
    var gr = new GlideRecord('sc_req_item');
    gr.addQuery('request', current.sys_id);
    gr.query();
 
    if (gr.next()) {
        var description = gr.cat_item.description;
        current.description = getFinalText(description);
    }
 
})(current, previous);

function getFinalText(text) {
    // Replace all <br /> tags with a CRLF
    var regX1 = /<br\s\/>/ig;
    var text2 = text.replace(regX1, String.fromCharCode(13));
    // Replace all remaining HTML tags with ""
    var regX2 = /(<([^>]+)>)/ig;
    var finalText = text2.replace(regX2, "");
    return finalText;
}

 Regards,

Amit