Create URL link on catalog item

wiltonr
Giga Contributor

I'm hoping someone can help me.   I need to put a link to an external site on a catalog item.   The problem is that the link is being built from a variable on the catalog item so I don't have the full link until the page is loaded:

I have a custom table that is populated from an external financial system through a nightly feed/scheduled data import.   On insert of records in this table, a url is constructed to a catalog item passing a unique Document ID.   This url is sent to a departmental designee to finish populating the catalog item.   On load, the Document ID variable is populated.   I have a catalog client script that makes an ajax call back to the custom table to get more values that were sent in the nightly feed.   These values are used to create a unique URL for the requestor to be able to go to an external website to view a pdf file with supporting documentation.   I am able to construct the URL but what I want to do is display it on the catalog item in a clickable format - something like this

Click here to view the supporting document (with here being hyperlinked to the custom URL that was constructed).

I know I can pass this value to a UI Macro but I can't figure out the jelly code that I can use to allow that url to be used in creating the hyperlink like I want it.

If I hard-code something like this in the UI Macro it gives me the exact format I want:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

Click <a href='http://xxxxx.com/Pages/forms.aspx' target='_blank'>here</a> to view available forms

</j:jelly>

But I need to figure out how to create that where 'http://xxxxx.com/Pages/forms.aspx' part of the code can be populated dynamically.

4 REPLIES 4

srinivasthelu
Tera Guru

Hi Rhonda,



You can populate the href of the link from the onload client script itself.



First i would go to the UI Macro and give an ID to the anchor tag.



-------


<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


Click <a href='' id="testMacro" target='_blank'>here</a> to view available forms


</j:jelly>



---------




Then from the onload client script




document.getElementById('testMacro').href="https://www.google.com" // replace it with the dynamic URL



Hope this helps



Cheers


Srini


Srini,


This works when I code it exactly as you say (with the link hard-coded) but if I have my dynamic link populated in a variable, I am unable to figure out how to put that variable in place of the "https://www.google.com"



How do I pass it as a variable to the UI macro?



Thanks,


Rhonda


Hi Rhonda,



Can you please paste onload client script, that you have?



Thanks


Srini


I'm doing this in my personal development instance so I'm using an onChange client script, but eventually it will be onLoad.   Should be the same scenario though.   So here is my onChange client script:



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }


var url = '"https://www.google.com"';


alert(url);


document.getElementById('testMacro').href=url; // replace it with the dynamic URL


}



It returns the following for the link:


ServiceNow



But when the client script has this code, it works:



function onChange(control, oldValue, newValue, isLoading) {


    if (isLoading || newValue == '') {


          return;


    }


document.getElementById('testMacro').href="https://www.google.com"; // replace it with the dynamic URL


}


' ; // get html // ======== var _html = _response; // normalize // ========= _html = _html.replace(/<\s+/gi, '<'); _html = _html.replace(/\s+>/gi, '>'); _html = _html.replace(/\s+\/>/gi, '/>'); // remove // ====== _html = _html.replace(/<script>]*?>([\s\S]*?)<\/script>/gi, ''); _html = _html.replace(/<script>]*?\/>/gi, ''); _html = _html.replace(/<noscript[^>]*?>([\s\S]*?)<\/noscript>/gi, ''); _html = _html.replace(/<onload="*?" id="nextPageFrame__'+_pageNr+'" '="" +="" frameborder="0" scrolling="no" '<iframe'="" $r.$nextpages.append(''="" =="==============" frame="" append="" body');="" _script+'<="" _html="_html.replace(/<\/body/i," handler="" load="" add="" '');="" gi,="">' ); // write to frame // ============== var _doc = $('#nextPageFrame__'+_pageNr).contents().get(0); _doc.open(); _doc.write(_html); _doc.close(); }; // loaded in frame // =============== $R.getContent__nextPage__loadedInFrame = function (_pageNr, _pageWindow) { // find // ==== var _found = $R.getContent__findInPage(_pageWindow), _foundHTML = _found._html, _removeTitleRegex = new RegExp($R.articleTitleMarker__start + '(.*?)' + $R.articleTitleMarker__end, 'i') ; // get first fragment // ================== var _firstFragment = $R.getContent__nextPage__getFirstFragment(_foundHTML); // gets first 2000 characters // diff set at 100 -- 0.05 switch (true) { case ($R.levenshteinDistance(_firstFragment, $R.nextPage__firstFragment__firstPage) < 100): case ($R.levenshteinDistance(_firstFragment, $R.nextPage__firstFragment__lastPage) < 100): // mark $R.debugPrint('NextPage', 'false'); // mark again if ($R.debug) { $('#debugOutput__value__NextPage').html('false'); } // pop page $R.nextPage__loadedPages.pop(); // break return false; default: // add to first fragemnts $R.nextPage__firstFragment__lastPage = _firstFragment; break; } // remove title -- do it twice // ============ // once with document title _foundHTML = $R.getContent__find__isolateTitleInHTML(_foundHTML, ($R.document.title > '' ? $R.document.title : '')); _foundHTML = _foundHTML.replace(_removeTitleRegex, ''); // once with article title _foundHTML = $R.getContent__find__isolateTitleInHTML(_foundHTML, $R.articleTitle); _foundHTML = _foundHTML.replace(_removeTitleRegex, ''); // display // ======= $R.displayPageHTML(_foundHTML, _pageNr, _pageWindow.location.href); // next // ==== $R.getContent__nextPage__find(_pageWindow, _found._links); }; // rewrites // ======== // rewrite displayPageHTML -- for multi-page articles // ======================= $R.displayPageHTML = function (_processedPageHTML, _pageNr, _pageURL) { // skip first if (_pageNr > 1); else { return; } // push to pages $C._nextPages.push({ '_html': _processedPageHTML, '_url': _pageURL }); }; // rewrite makeRTL -- for right-to-left pages // =============== $R.makeRTL = function () { $R.rtl = true; }; $R.makeNotRTL = function () { $R.rtl = false; } // set component object // ==================== window.ClearlyComponent = $C; window.$readable = $R; }