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

Passing data from widget server script to html template

Austin27
Tera Contributor

Hello community. I'm having a hard time following various different posts on getting my datetime returned to the html client page. In the Service Portal Widget Editor Page in designer mode, I can show the datetime. But on the html page of the service portal I get undefined. I'm not sure what I'm overlooking or missing. I've tried also using the this.server.get parameter but I don't get a response back only the json string. I don't think I was using the this.server.get parameter correctly and was probably over complicating things that way. So below is what I have that allows me to see the datetime in the service portal widget editor, but need to pass it to the html template too. Thanks for the second set of eyes and any help would be appreciated! Thank you and have a good rest of your day. 

 

// SERVER SCRIPT
(function() {
    /* populate the 'data' object */
    /* e.g., data.table = $sp.getValue('table'); */
    data.sys_id = $sp.getParameter("sys_id");
	
    var widgetGr = new GlideRecord("sp_widget");
    widgetGr.addQuery("sys_id", data.sys_id);
    widgetGr.query();
    while (widgetGr.next()) {
        // Retrieve the sys_updated_on field value as a GlideDateTime object
        var modifiedTime = widgetGr.getValue("sys_updated_on").toString();
			  data.modifiedTime = modifiedTime;
        //var gdt = new GlideDateTime(sysUpdateOn);

        // Format the date using the desired format
        // Note: gdt.getDate() returns a GlideDate object, which has the getByFormat() method
        //var formattedDate = gdt.getDate().getByFormat("MMM dd, yyyy");

        // Store the formatted date in the data object
        //data.time = formattedDate;

    }
})();

 

//CLIENT SCRIPT
function($scope, $sce) {
    var c = this;
    alert(JSON.stringify(c.data.modifiedTime));
    // array of option names
    var optionNames = ['service_intro_text', 'features_text', 'designed_for_text', 'rates_text', 'getting_started_text', 'getting_assistance_text', 'learn_more_text'];

    // Function to update the trusted HTML for a given option
    function updateTrustedHtml(optionName) {
        $scope.$watch('c.options.' + optionName, function(newValue) {
            c[optionName + '_html'] = $sce.trustAsHtml(newValue);
        });
    }

    // Initialize and watch each option
    optionNames.forEach(function(optionName) {
        updateTrustedHtml(optionName);
    });
}

 

<!-- HTML CODE -->
<p ng-bind-html="c.data.modifiedTime"></p>

 

2 REPLIES 2

Satishkumar B
Giga Sage
Giga Sage

@Austin27 please refer this:
1. Server Script

 

(function() {
    // Get sys_id from the widget parameter
    data.sys_id = $sp.getParameter("sys_id");

    // Query the widget table for the given sys_id
    var widgetGr = new GlideRecord("sp_widget");
    widgetGr.addQuery("sys_id", data.sys_id);
    widgetGr.query();

    if (widgetGr.next()) {
        // Retrieve the sys_updated_on field value as a GlideDateTime object
        var modifiedTime = widgetGr.getValue("sys_updated_on");

        // Convert the GlideDateTime to a formatted string
        var gdt = new GlideDateTime(modifiedTime);
        data.modifiedTime = gdt.getDate().getByFormat("MMM dd, yyyy HH:mm:ss");
    }
})();

 

 

2. Client Script

 

function($scope, $sce) {
    var c = this;

    // Watch for changes in the data object and trust the HTML content
    $scope.$watch('c.data.modifiedTime', function(newValue) {
        if (newValue) {
            c.modifiedTime_html = $sce.trustAsHtml(newValue);
        }
    });
}

 

 

 

<p ng-bind-html="c.modifiedTime_html"></p>

 

 

…………………………………………........................................................................................
Please Mark it helpful 👍and Accept Solution !! If this helps you to understand.

…………………………………………........................................................................................

Hi @Satishkumar B 
I updated my code with what you provided and still not getting it to display in the html side. Any other thoughts or ideas? I can still see it in the widget editor but not on the html side. 

 

Thank you.