how to get display value and sys id from server script to client controller in service portal

Priya75
Tera Contributor

Hi All,

 

I am sending data through server.get and trying to fetch the data from server side,  i want to display the data filled in a catalog in tabular form.

 

 

 

 

 

//Client Controller
c.getloc= function()
{
var loc=c.loc.displayValue;

if(loc==''){
c.cloudser='';
}
this.server.get({
action :"hostloc",
name: loc
}).then(function(response){
c.cloudser=response.data.cloudProvideList;
});

//Server script

if(input.action="hostloc"){
var location=input.name;
data.cloudProvideList=[];
var cloudService-{};
if(location='Public')
{
var gr=new GlideRecord("sn_capi_provider");
gr.addQuery("u_type" ,location );
gr.query();
while(gr.next())
{
cloudService.name=gr.getValue("name");
cloudService.value=gr.sys_id.toString();
data.cloudProvideList.push(cloudService);

}

 

 

 

 

 

 

 

 

 

i want to use the name in the tabular form but i am getting sys_id . how can i get the name and set it in the tabular form( HTML form) 

 

7 REPLIES 7

what response you getting exactly?

 

TBH you shouldn't have gotten the values since there were typo's in the script that break it, so if you got something it would have been from something else.

So for example 

cloudService.name=gr.getValue("name");

This wouldn't return a sys_id unless a sys_id was on the name field. If a name field doesn't exist you'd get undefined or something similar. Oh, and I noticed another typo I failed to fix. Your second if should have two = as well, so

//Client Controller
c.getloc = function() {
        var loc = c.loc.displayValue;

        if (loc == '') {
            c.cloudser = '';
        }
        this.server.get({
            action: "hostloc",
            name: loc
        }).then(function(response) {
            c.pList = response.data.serviceProList; //I assume this is supposed to be cloudProvideList
        });

        //Server script

        if (input && input.action == "hostloc") {
            var location = input.name;
            data.cloudProvideList = [];
            var cloudService - {};
            if (location == 'Public') { //Another = here
                var gr = new GlideRecord("sn_capi_provider");
                gr.addQuery("u_type", location);
                gr.query();
                while (gr.next()) {
                    cloudService.name = gr.getValue("name");
                    cloudService.value = gr.sys_id.toString();
                    data.cloudProvideList.push(cloudService);
                }


Basically what the script seems to do is that on on client side you call getloc function.
The function asks server for response and provides parameters action (hostloc) and name (can be empty.

Then in server you check if there's input and the action is hostloc. If so, you check if location is "Public" and check if sn_capi_provider table has records with Public type and then push those to data.cloudProvideList array.
Your c.pList should contain an array of objects.

Now there are a few things to note if your original script is returning values, but mostly
since your response is actually using "serviceProList" it's possible you already have that value populated on server side and you're actually getting data from that instead of "cloudProvideList".
Try updating the response.data.serviceProList to cloudProvideList on your client script and you'll see whether it works or not.

Also, since you're specifically checking for Public you wouldn't need to call server side in your client script if it's not Public (unless your code continues and does additional location checks in other conditions).

Trupti94
Tera Guru
if(input.action="hostloc"){

@Priya75 - try to change this line