queryformat and Railo - ajax

Apologies if I'm haven't found it in the documentation yet...
Q: How do you change the queryformat for an ajax call in Railo?
Here's my component:
component {
remote function Read() returnformat='json' {
svc = new Query();
svc.setSQL("SELECT * FROM INFORMATION_SCHEMA.TABLES");
obj = svc.execute();
local.result.Prefix = obj.getPrefix();
local.result.qry = obj.getResult();
url.queryFormat = "column";
return local.result;
}
}
and here's my JavaScript:
(function() {
var local = {};
local.type = 'POST';
local.url = 'AJAX.cfc';
local.dataType = 'json';
local.data = {};
local.data.method = 'Read';
local.Promise = $.ajax(local);
local.Promise.done(done);
local.Promise.fail(fail);
function done(response) {
console.log(response);
debugger;
}
function fail(xhr,status,response) {
debugger;
}
})();
What I'm getting back is:
response.qry.DATA[] // 57 arrays, each of length 4
But ColdFusion returns this, which I've grown fond of using (being able to use the column names instead of the array position):
response.qry.DATA.TABLE_CATALOG[] // An array of 57 elements
response.qry.DATA.TABLE_SCHEMA[]
response.qry.DATA.TABLE_NAME[]
response.qry.DATA.TABLE_TYPE[]

Use ReturnFormat="plain" on the function, and pass true for the 2nd argument of serializeJson()
serializeJson(Query, true)
That will give you a JSON object that is serialized by Column, so you can just return it.

Related

How to Access Parameters in New Form

I have a custom button that I have the below JavaScript attached to that opens an entity form and I am trying to pass viewName to it.
function sendContextToQC(selectedControl) {
var entityFormOptions = {};
entityFormOptions["entityName"] = "new_qrecipemasteritem";
entityFormOptions["useQuickCreateForm"] = true;
var currentView = selectedControl.getViewSelector().getCurrentView().name;
var formParameters = {};
formParameters["viewName"] = currentView
Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
function (success) {
console.log(success);
},
function (error) {
console.log(error);
});
}
The form opens fine and I have the "Pass Execution Context as first parameter" checked, but I don't know how to access the formparameters object. Is it part of the executionContext? I even tried adding another parameter (formParameters), but that didn't work either.
2/14/23
You can access the form parameters from within the form by using the getFormContext() method. Here's an updated version of your code that shows how to access the formParameters object from within the form:
function sendContextToQC(selectedControl) {
var entityFormOptions = {};
entityFormOptions["entityName"] = "new_qrecipemasteritem";
entityFormOptions["useQuickCreateForm"] = true;
var currentView = selectedControl.getViewSelector().getCurrentView().name;
var formParameters = {};
formParameters["viewName"] = currentView;
Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
function (success) {
var formContext = success.getFormContext();
var viewName = formContext.getAttribute("viewName").getValue();
console.log("View Name: " + viewName);
},
function (error) {
console.log(error);
});
}
In the success callback of the openForm method, you use getFormContext() to get a reference to the form context, and then use that reference to access the viewName attribute. You can then use the getValue() method on the attribute to retrieve its value.
In function Xrm.Navigation.openForm argument formParameters is an object holding field values. The function passes these values to the fields that are (should be) on the form opened by it.
It is possible to pass custom query string parameters to the form. These parameters can be retrieved accessing the window.location object.
See openForm - MS Learn and Configure a form to accept custom querystring parameters - MS Learn.
The (classic) Form Properties dialog has a Parameters tab where custom parameters can be declared. Custom parameters that have not been configured here can only be passed in a plain url by adding an extraqs parameter.
I figured it out. To send parameter:
function sendContextToQC(selectedControl) {
var entityFormOptions = {};
entityFormOptions["entityName"] = "new_qrecipemasteritem";
entityFormOptions["useQuickCreateForm"] = true;
var currentView = selectedControl.getViewSelector().getCurrentView().name;
var formParameters = {};
if (currentView.includes("Master") == true) {
formParameters["isMasterView"] = 1;
}
else {
formParameters["isMasterView"] = 0;
}
Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
function (success) {
console.log(success);
},
function (error) {
console.log(error);
});
}
Then to pick it up onload:
this.hideUsedIn = function(executionContext) {
let formContext = executionContext.getFormContext();
let usedInAttribute = formContext.getAttribute("new_usedin");
let usedInControl = formContext.getControl("new_usedin");
let buttonData = {};
buttonData = Xrm.Utility.getPageContext().input.data;
let isMasterItem = buttonData.isMasterView;
if (isMasterItem === 1){
usedInAttribute.setRequiredLevel("none");
usedInControl.setVisible(false);
}
}

ngTagsInput Autocomplete does not Open

I am unable to get the AutoComplete list to display. My service returns json model: TagID: 1, text:MyText
but the AutoComplete list never displays. My HTML:
<tags-input ng-model="tags" tag-class="{even: $index % 2 == 0, odd: $index % 2 != 0}" on-tag-added="addTag(tags)"> <auto-complete source="loadTags($query)"></auto-complete> </tags-input>
My Controller code:
$scope.loadTags = function ($query) {
var tags;
contractorService.gettags()
.success(function (data) {
tags = data;
return tags.filter(function(tag) {
return tag.text.toLowerCase().indexOf($query.toLowerCase()) != -1
UPDATE
I have discovered that it just does not like the Json returned from Ajax call to MVC Controller.
public async Task<ActionResult> GetMajorTags()
{
majorId = UserInfo.intMajorID;
var tags = await CompanyClient.GetAvailableTags(majorId);
return Json(tags, JsonRequestBehavior.AllowGet);
}
Even bypassing the service and calling the MVC Controller method directly like below:
$scope.loadTags = function (query) {
return $http.get('/SSQV4/SSQV5/Contractor/GetMajorTags');
};
If I make the source static like below:
var auto = [
{ TagID: 4,text: 'Tag4' },
{ TagID: 5, text: 'Tag5' },
{ TagID: 6, text: 'Tag6' }
];
It works, but it will not show what is returned from the MVC Controller even though the data returned is in the EXACT same format.
Any assistance is greatly appreciated!
This code is not correct:
$scope.loadTags = function ($query) {
var tags;
contractorService.gettags()
.success(function (data) {
tags = data;
// return where?
return tags.filter(function(tag) {
return tag.text.toLowerCase().indexOf($query.toLowerCase()) != -1
});
});
}
There is no reason to have a return statement within your success callback. Where would that return to? If you did something like this:
var tags = $scope.loadTags();
console.log(tags); // undefined
... tags would be undefined. The reason is because the return statement is NOT returning from the call to loadTags. It is instead returning from within a promise callback.
This is actually how it's done:
var tags = [];
$scope.loadTags = function () {
contractorService
.gettags()
.success(function (data) {
tags = data;
tags = tags.filter(function(tag) {
return tag.text.toLowerCase().indexOf($query.toLowerCase()) != -1;
});
});
};
Notice how there are no return statements (except for your filter).
This just does not work at all:
$scope.loadTags = function (query) {
return $http.get('/SSQV4/SSQV5/Contractor/GetMajorTags');
};
If you were to do something like this:
var tags = $scope.loadTags();
console.log(tags); // promise object. NO DATA
tags would contain a promise object NOT your data. You would need to do the following to get the actual data:
var tags = [];
$scope.loadTags().success(function(data) {
tags = data;
});

Flux store not running function before constructor

I have created a store that attempts to do an AJAX request and set this.restaurants to the returned data. If I hard code the data in as an array of json to the `this.restaurants everything works great. But my AJAX call is not being run in time. How can i fix this? The console log displays everything as expected
class RestaurantStore extends EventEmitter {
constructor() {
super()
this.restaurants = this.getRestaurants()
}
getRestaurants() {
const url = `/lib/data/restaurants.json`
var r = new XMLHttpRequest();
r.open("GET", url, true);
r.onreadystatechange = function () {
if (r.readyState != 4 || r.status != 200) return;
const json = r.responseText
console.log(json)
return json
// this.restaurants = json
};
r.send();
}
}

Get Specific Data From ngStorage

I have a bunch of data on JSON using LocalStorage from ng-storage like this,
[Object { judul="Just", isi="Testing"}, Object { judul="To", isi="Get"}, Object { judul="Specific", isi="Data"}]
but I want to get one specific data to get the "isi" value, how to do that ?
You can do this by adding a get function to your factory
for example :
.factory ('StorageService', function ($localStorage) {
$localStorage = $localStorage.$default({
things: []
});
var _getAll = function () {
return $localStorage.things;
};
//-----
var _get = function (isi) {
for( var i = 0; i < $localStorage.things.length ; i++ ){
if( $localStorage.things[i].isi === isi ){
return $localStorage.things[i];
}
}
}
return {
getAll: _getAll,
get : _get
};
})
and in your controller you can get the specific data by passing the id of your object
var objectInlocal = StorageService.get(Thing.isi);

Server Side Sorting using Mongoose (mongodb + node.js)

I am trying to sort based on a function. I am currently doing the following, and it works.
var _criteria = ... some search criteria
var _pageNumber = ... the page num I want to see
var _nPerPage = ... the number of documents per page
var _sort = {};
_sort.name = ... the column name I am sorting on
_sort.order = ... asc or desc sort
Collection.find(_criteria)
.skip((_pageNumber-1)*_nPerPage)
.limit(_nPerPage)
.sort(_sort.name,_sort.order)
.execFind(function (err, docs) {
...
});
Now I would like to sort based on some function that takes in a user input:
var sortFunc = function(x){ return (x - doc.score); };
// where doc.score is an attribute of the document I am searching on
// and x is a user provided value
and I can't find a way to do this. I tried to eval this function as follows:
var mongoose = require('mongoose');
var mdb = mongoose.connect(uri);
var myfunc = function(x){ return x; };
mdb.connection.db.eval( myfunc, "asdf", function (err, retval) {
console.log('err: '+err);
console.log('retval: '+retval);
});
but I get the following error:
err: Error: eval failed: db assertion failure
retval: null
Any help on this would be awesome.
Thanks a lot
I think you need do like this:
var mongoose = require('mongoose');
mongoose.connect(uri);
mongoose.connection.on("open", function(err){
mongoose.connection.db.eval("function(x){ return x; }", "asdf", function (err, retval) {
console.log('err: '+err);
console.log('retval: '+retval);
});
});
This can work on my PC. You must ensure that the connection is available.

Resources