How to pass URLs in catalog OnLoad script in showFieldMsg

Shwetha Nair
Giga Guru

hi Developers,

 

I want to pass the URL links from backend table to the catalog form in showFieldMsg.

I am able to get it in array till script include, but unable to show it in catalog script.

Kindly help me with the script, also the links should be in clickable format in new tab.

 

Script include :

var LinksDisplay = Class.create();
LinksDisplay.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getdisplayLinkName : function() {
        var arr = [];
        var grurl = new GlideRecord('u_links');
            grurl.addEncodedQuery("u_link_urlISNOTEMPTY");
            grurl.query();
        while (grurl.next()){
                arr.push(grurl.getValue("u_link_url"));
        }
        for(var i=0 ;i<arr.length;i++){
            gs.log('Test'+ arr[i]);
            }
    return arr[i];
},
    type: 'LinksDisplay'
});
 
Client script :
function onLoad() {
   //Type appropriate comment here, and begin script below
 var getData = new GlideAjax('LinksDisplay');
     getData.addParam('sysparm_name', 'getdisplayLinkName');
     getData.getXML(helloLinkParse);
}
function helloLinkParse(response){
    var answer = response;
    alert(response);
    var tlist = answer.split(",");
 
  g_form.showFieldMsg('u_links', 'Find the links :' + tlist);
}
 
Thanks in advance,
Shwetha
5 REPLIES 5

Aniket Chavan
Tera Sage
Tera Sage

Hello @Shwetha Nair ,

Please try to use below updated scripts and see how it works for you.

Updated Script Include:

var LinksDisplay = Class.create();
LinksDisplay.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getdisplayLinkName: function() {
        var arr = [];
        var grurl = new GlideRecord('u_links');
        grurl.addEncodedQuery("u_link_urlISNOTEMPTY");
        grurl.query();
        while (grurl.next()) {
            arr.push(grurl.getValue("u_link_url"));
        }
        // Return the links as a comma-separated string
        return arr.join(",");
    },
    type: 'LinksDisplay'
});


Updated Client Script:

function onLoad() {
    var getData = new GlideAjax('LinksDisplay');
    getData.addParam('sysparm_name', 'getdisplayLinkName');
    getData.getXML(helloLinkParse);
}

function helloLinkParse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    var tlist = answer.split(",");

    // Create the HTML string for clickable links
    var message = '<div class="fieldmsg-container" aria-live="polite"><div class="fieldmsg notification notification-info">';
    for (var i = 0; i < tlist.length; i++) {
        if (tlist[i]) {
            message += '<a href="' + tlist[i] + '" target="_blank">' + tlist[i] + '</a><br/>';
        }
    }
    message += '</div></div>';

    // Use g_form.showFieldMsg to show a placeholder message
    g_form.showFieldMsg('u_links', 'Loading links...', 'info', false);

    // Update the field message container with HTML content
    var fieldMsgContainer = gel('variable.u_links_fieldmsg'); // Replace 'variable.u_links' with your actual field name if different
    if (fieldMsgContainer) {
        fieldMsgContainer.innerHTML = message;
    }
}

 

Feel free to refer to the link below, which provides additional helpful information on showFieldMsg usage and customizations.

 

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


Regards,
Aniket

 

hi @Aniket Chavan 

 

Thanks for your response. Few questions as I am not aware of HTML much:

1. What is exactly i need to put in variable.u_links_fieldmsg? My field name is 'u_links' and label is Links.

var fieldMsgContainer = gel('variable.u_links_fieldmsg'); 

  2. Do i need to put the HTML part somehwere else or in OnLoad client catalog client script itself?

 3. Also, I am getting this error . Would it be an issue

ShwethaNair_0-1730793151673.png

 

Thanks,

Shwetha

 

Deepak Negi
Mega Sage
Mega Sage

Your code seems correct, try to add .toString() in the return statement in script include.

 

return arr[i].toString();

 

Even better, if you can use JSON as an alternative

hi @Deepak Negi ,

 

I do not want to put tostring() s I am looking to make it in clickable format.

 

Thanks,

Shwetha Nair