I am not getting values in Client controller properly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2024 10:29 PM - edited 07-29-2024 01:38 AM
//server script
else if (input.action == 'impHostingloc') {
var hostingLocation=input.name;
data.cloudServiceProvideList =[]:
var cloudService=(};
if(hostingLocation=="Public"){
var cloudPro = new GlideRecord('sn_capi_provider');
cloudPro.addQuery ("u_cloud_service_provider_type", hostingLocation);
cloudPro.query();
while (cloudPro.next)) {
//gs. info("inside while"+input.name);
cloudService.label=cloudPro.getValue('name');
cloudService.value=cloudPro.sys_id.toStringO;
data.cloudServiceProvideList.push(cloudService);
}
}
else if(hostingLocation=="Private"){
var cloudProdl = new GlideRecord("sn_capi_provider"):
cloudProd1. addQuery ("U_cloud_service_provider_type", hostingLocation)
cloudPro1.query();
while(cloudProdl.next()){
gs. info("inside while"+input, name);
cloudService.label=cloudProd1,getValue('name');
cloudService.value-cloudProd1.sys_id.toString();
data.cloudServiceProvideList.push(cloudService);
}
else if (input.action == 'getServiceName') {
var ids=input.ids;
data. name= [];
var gr1=new GlideRecord("sn_capi_provider") gr1. addQuery('sys_id', 'IN',ids);
gr1. query);
while (grl.next )
date.name. push(gr1.getValue('name '));
gs. info ("data"+data. name) ;
);
//client Controller
c.cloudService=[];
c.getHostLoc = function() {
var hostingLoc=c.hostloc.displayValue;
console. log("hostinglocation"+c.hostloc.displayValue);
if(hostingLoc==""){
c.cloudservprov='';
}
this.server.get({
action: "impHostingloc" ,
name: hostingLoc,
//listservice: selectedcloud
}).then (function(response) {
c.cloudservprovlist= response.data.cloudServiceProvidelist;
console.log("1891"+c.cloudservprovlist);
}
c.updateSelected =function (){
var selectedCloudService=c.cloudservprov;
console.log("response"+selectedCloudService);
if(selectedCloudService && selectedCloudService. length›0)(
this.server.get ({
action: "getServiceName",
ids: selectedCloudService
}).then (function(response){
c.cloudService=response.data.name.join ',');
console.log("response"+c.cloudService);
}
//HTML
<sn-choice-list sn-model="c.cloudservprov" table="sn_capi_provider" display-field="name" value-fied='sys_id'
sn-items=c.cloudservprovList" multiple="true" on-change="c.updateSelected()"</sn-choice-list >
i am trying to get the values selected by the user , but in the console i am not getting all the values for eg:- first value is skipped first time and then it appears second time , so if i select 4 values only 3 are getting updated
var selectedCloudService=c.cloudservprov;
console.log("response"+selectedCloudService);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2024 11:19 PM
You'll have to then clarify the code you're using.
If we look at your client code:
c.cloudService=[];
c.getHostLoc = function() {
var hostingLoc=c.hostloc.displayValue;
console. log("hostinglocation"+c.hostloc.displayValue);
if(hostingLoc==""){
c.cloudservprov='';
}
this.server.get({
action: "impHostingloc" ,
name: hostingLoc,
//listservice: selectedcloud
}).then (function(response) {
c.cloudservprovlist= response.data.cloudServiceProvidelist;
console.log("1891"+c.cloudservprovlist);
}
We see that you're getting "hostingLoc" value and then you're calling the server with action "impHostingLoc" and name with the "hostingLoc" value.
Then in your server script we have this.
else if (input.action == 'impHostingloc') {
var hostingLocation=input.name;
data.cloudServiceProvideList =[]: // : instead of ;
var cloudService=(}; // ( and } mixed. Also you might want to move this inside the while loop.
if(hostingLocation=="Public"){
var cloudPro = new GlideRecord('sn_capi_provider');
cloudPro.addQuery ("u_cloud_service_provider_type", hostingLocation);
cloudPro.query();
while (cloudPro.next)) { //Missing starting ( on next
//gs. info("inside while"+input.name);
cloudService.label=cloudPro.getValue('name');
cloudService.value=cloudPro.sys_id.toStringO;
data.cloudServiceProvideList.push(cloudService);
}
}
else if(hostingLocation=="Private"){
var cloudProdl = new GlideRecord("sn_capi_provider"): //cloudProdl instead of cloudProd1
cloudProd1. addQuery ("U_cloud_service_provider_type", hostingLocation) //Capital U and missing ;
cloudPro1.query();
while(cloudProdl.next()){ // again cloudProd1 should be used here? Also consider moving the var cloudService here as well.
gs. info("inside while"+input, name);
cloudService.label=cloudProd1,getValue('name'); // comma instead of a dot.
cloudService.value-cloudProd1.sys_id.toString(); // - instead of a =
data.cloudServiceProvideList.push(cloudService);
}
else if (input.action == 'getServiceName') {
var ids=input.ids;
data. name= [];
var gr1=new GlideRecord("sn_capi_provider") gr1. addQuery('sys_id', 'IN',ids); //no ; after GlideRecord.
gr1. query); //Missing starting (
while (grl.next ) //grl instead of gr1
date.name. push(gr1.getValue('name ')); //date instead of data
gs. info ("data"+data. name) ;
);
We are missing the original if. There's no indication what values your script might be updating before, so you might end up getting some data. Remember that if you don't use if(!input) and if(input) you might just end up running the whole server script and if you're updating values onLoad you might have some "ghost" values returned in your client script.
Anyway, lets assume your input.action == impHostingloc. Your script breaks at "data.cloudServiceProvideList =[]: // : instead of ;" and will stop running. Some other steps would also stop it from running.
I also noticed that there's a "o" at the end of one toString() operation instead of (). That would at least break the value.
So, lets say your script is like this:
data.cloudServiceProvidelist = "xxx";
if(input.action == "impHostingloc"){
//code that breaks;
}
You still might end up with a value in cloudServiceProvidelist, because nothing is overwritten as the code breaks.
If you're getting values then you're getting the values from some other part of a script.
Try adding console.log() to your server script if the widget is in global scope. Those will work and you can see in the console what parts ran. If it's a custom scope, then you'd need to log with something like gs.info();
By logging you can actually see if your script processes through certain points or not.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2024 11:37 PM
Here's a bit modified script for you:
//server script
if (input.action == 'impHostingloc') {
var hostingLocation = input.name;
data.cloudServiceProvideList = [];
if (hostingLocation == "Public" || hostingLocation == "Private") {
var cloudPro = new GlideRecord('sn_capi_provider');
cloudPro.addQuery("u_cloud_service_provider_type", hostingLocation);
cloudPro.query();
while (cloudPro.next()) {
var cloudService = {};
cloudService.label = cloudPro.getValue('name');
cloudService.value = cloudPro.sys_id.toString();
data.cloudServiceProvideList.push(cloudService);
}
}
} else if (input.action == 'getServiceName') {
var ids = input.ids;
data.name = [];
var gr1 = new GlideRecord("sn_capi_provider");
gr1.addQuery('sys_id', 'IN', ids);
gr1.query();
while (gr1.next()) {
data.name.push(gr1.getValue('name'));
gs.info("data" + data.name);
}
}
//client Controller
c.cloudService = [];
c.getHostLoc = function() {
var hostingLoc = c.hostloc.displayValue;
console.log("hostinglocation" + c.hostloc.displayValue);
if (hostingLoc == "") {
c.cloudservprov = '';
}
this.server.get({
action: "impHostingloc",
name: hostingLoc
}).then(function(response) {
c.cloudservprovlist = response.data.cloudServiceProvideList;
console.log("1891 " + c.cloudservprovlist);
});
}
c.updateSelected = function() {
var selectedCloudService = c.cloudservprov;
console.log("response: " + selectedCloudService);
if (selectedCloudService && selectedCloudService.length > 0) {
this.server.get({
action: "getServiceName",
ids: selectedCloudService
}).then(function(response) {
c.cloudService = response.data.name.join(',');
console.log("response" + c.cloudService);
});
}
}
One thing to note is your variable
If we look at your client script functions you're returning values from server to
In your updateSelected function you're trying to use values from
I feel like that might be your biggest issue with the script.
Basically you have three variables on your client side:
Which one are you expecting to use here? Avoid using similar names for variables.
Also you had an if for Private and Public, but there's no else if, so you wouldn't be returning any values if your hostingLocation is something else.