Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Pass data object from server side to client controller in a portal widget

Jalen Dixon
Tera Contributor

Hello, 

 

I am trying to pass the sys_id of an attachment from the server side code into the client and having issues. What am I doing wrong with this code? Please help. 

 

Server Side 

// need make the attachment download dynamic 
	var kbArticle = {};
	data.kbArticle = '';
	var findAttachment = new GlideRecord("sys_attachment");
	findAttachment.addEncodedQuery("table_sys_idSTARTSWITH" + kbSysID + "^ORtable_sys_idSTARTSWITH" + kbSysID2);
	findAttachment.query();
	if (findAttachment.next() && action){
		data.kbArticle = kbArticle;
			if (action == "NDA"){
				data.kbArticle.sys_id = findAttachment.sys_id.toString();
			}
	}

 

Client Code

function($location, $window) {
    /* widget controller */
    var c = this;
	
	c.uiAction = function(action) {
		c.data.action = action;
			c.server.update().then(function() {
			// do something window location
				if (c.data.action == 'NDA') {
				var url = 'sys_attachment.do?sys_id=' + c.kbArticle.sys_id;
				$window.open(url);
		}
		});
														 
};
}
1 ACCEPTED SOLUTION

Sohithanjan G
Kilo Sage

Hi @Jalen Dixon 

In the client-side code, you're attempting to access c.kbArticle.sys_id, but kbArticle is not directly accessible within the client script because it's stored in c.data.kbArticle. You need to access it as c.data.kbArticle.sys_id. Here's the corrected client-side code:

 

function($location, $window) {
    /* widget controller */
    var c = this;
    
    c.uiAction = function(action) {
        c.data.action = action;
        c.server.update().then(function() {
            // do something window location
            if (c.data.action == 'NDA') {
                var url = 'sys_attachment.do?sys_id=' + c.data.kbArticle.sys_id; // Access kbArticle as c.data.kbArticle.sys_id
                $window.open(url);
            }
        });
    };
}

 

HIT Helpful & Accept the solution!! 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

View solution in original post

1 REPLY 1

Sohithanjan G
Kilo Sage

Hi @Jalen Dixon 

In the client-side code, you're attempting to access c.kbArticle.sys_id, but kbArticle is not directly accessible within the client script because it's stored in c.data.kbArticle. You need to access it as c.data.kbArticle.sys_id. Here's the corrected client-side code:

 

function($location, $window) {
    /* widget controller */
    var c = this;
    
    c.uiAction = function(action) {
        c.data.action = action;
        c.server.update().then(function() {
            // do something window location
            if (c.data.action == 'NDA') {
                var url = 'sys_attachment.do?sys_id=' + c.data.kbArticle.sys_id; // Access kbArticle as c.data.kbArticle.sys_id
                $window.open(url);
            }
        });
    };
}

 

HIT Helpful & Accept the solution!! 

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)