In App Maker, how do you make dynamic table cell text? - google-app-maker

In App Maker, I am displaying a table and want to replace table cell data with different text using a data lookup from another table. Assume two tables, Departments and Employees.
Departments is two fields, DeptID and DeptDescription.
Employees is multiple fields including DeptID.
In the table listing for Employees, I would like to replace the DeptID with the DeptDescription. (The page datasource is Employees. I do not want to set up a relationship between the data models.)
I am guessing I want to do some scripting in the onDataLoad event for the table cell label for DeptID. I have this much so far:
app.datasources.Departments.query.filters.DeptID._equals = widget.datasource.item.DeptID;
app.datasources.Departments.newQuery().run();
widget.text = app.datasources.Departments.item.DeptDescription;
I know this is not correct, but am I close?

This answer is untested, but I wanted to present a possible solution that would not require a lot of DB calls, especially ones that make repeated calls to a server script which might consume a lot of processing time when you do line item calls.
Set up a separate datasource under the Department model. Change the default 'Query Builder' to 'Query Script' and add a parameter of type 'list(number)' or 'list(string)', this should match your Primary Key field type. Uncheck the 'auto load' option.
In your 'Query Script' portion enter the following code:
query.filters.Id._in = query.parameters.YourParameter;
return query.run();
Go to your Employees datasource that is supposed to generate your table and find your 'On Load' client script section. In this section enter the following code:
var departmentsDs = app.datasources.YourDepartmentsDs;
departmentsDs.properties.YourParameter = datasource.items.map(function(deptIds) {return deptIds.DeptID;});
departmentDs.load();
Now go the page that contains your table. If you have not already create a label widget do so now. In this label widget for the text binding enter the following:
#datasources.YourDepartmentsDs.loaded && (#datasources.YourDepartmentsDs.items).map(function(Id){return Id.Id}).indexOf(#widget.datasource.item.DeptID) !== -1 ? #datasources.YourDepartmentDs.items[(#datasources.YourDepartmentsDs.items).map(function(Id){return Id.Id}).indexOf(#widget.datasource.item.DeptID)].DeptDescription : 'Unable to retrieve Dept Description'
As stated this is untested and I wrote the code from memory without App Maker in front of me so it may require some additional tweaking. Going with the first option presented by J.G. would also be a very viable solution though. And I apologize but the code formatter does not seem to be working for me.

1 way) Create an aggregate table that joins your tables if you need to bypass using the relations feature. This way you can use sql to join the two tables in the datasource definition
2) if you don't want to make a new table. Change the text from a value binding to "more options"
=getDescription(#datasource.item.DeptId)
and then the code you wrote in a client side script
function getDescription(id){
google.script.run
.withSuccessHandler(function successHandler(result){ return result;})
.withFailureHandler( function failureHandler(e){ console.log(" Failed" +e);})
.queryValue(id);
}
server side script:
function queryValue(id){
var query = app.models.Departments.newQuery();
query.filters.DeptID._equals = id;
var results = query.run();
return results[0]["DeptDescription"];
}
that last line might be results[0].DeptDescription

Related

How to get the selected Lookup value from a custom Lookup from ax2012

Dears,
I'm using Microsoft Dynamics AX2012 R3
The Case: I want to make a custom lookup in Inventory Management ==> Journals ==> Item Transactions ==> Movement ==> Lines
I want to relate the Item Id to show all lines in the SalesLines Table in order to get the ItemId and It's dimensions (Config/Size/Color/Style) and when I select it I want to move the selection to the movement line.
My Solution:
Step1: I've made a custom view (viewCustomItemId) that shows all SalesLines tables and related to InventDim to show the dimensions.
Step2: I've made a custom form (frmCustomItemId) and added the view (viewCustomItemId) as a datasource and added all fields to a grid
Step3: In the form (InventJournalMovement) I've overrided the lookup method and added below code
Args args = new Args();
viewCustomItemId _viewCustomItemId;
args.name(formstr(frmCustomItemId));
args.caller(element);
newPopup = classFactory.formRunClass(args);
this.performFormLookup(newPopup);
newPopup.init();
newPopup.wait();
if (newPopup.closedOk())
{
_viewCustomItemId = newPopup.docCursor();
InventJournalTrans.ItemId = _viewCustomItemId.ItemId;
InventDim.configId = _viewCustomItemId.configId;
InventDim.InventSizeId = _viewCustomItemId.InventSizeId;
InventDim.InventColorId = _viewCustomItemId.InventColorId;
InventDim.InventStyleId = _viewCustomItemId.InventStyleId;
InventJournalTrans_DS.research();
}
Problem: When the lookup form is closed all fields are set to the movement line but I get warning message (Field 'Item number' must be filled in.) and the line number in the database are set to be minus for this line and also the default site and warehouse for this Item is not automatically set.
Question: Looks like I've retrieved the values but didn't pass it correctly to the lookup of the field, I don't understand what exactly I missed to make it work normally.
Thanks in advance.
I'm surprised I missed this at first glance.
This line is your problem:
InventJournalTrans_DS.research();
You're researching before you've updated/written the data. I'm not sure why you're researching in the first place, but you have to do one or the other. Write/Update, then research or don't research. The research is where you're losing the ItemId you've input.

GSuite Appmaker - how to set default value in table using button

I am trying to put together a mock up for a data collection app for a local nonprofit using GSuite's AppMaker. So far I really like the tool.
One thing I need to be able to do is streamline the data entry for the site representatives. In my app, I display a list of students in a table, where one of the columns is a boolean value which represents attendance. The desired result is for the teachers to be able to input the date field one time using the date input button at the bottom of the page. Then they can quickly point and click down the Present column to log attendance.
My question is: how would I link the date selector dropdown so that the date field pre-populates with the selected date from the input field? I don't want them to have to enter the field over an over again since its the same for each row and I don't want the user experience to feel clunky.
Screenshot of the App for reference:
Using client script, you can add the following to the onValueEdit event handler of the date widget at the bottom.
var items = widget.root.descendants.<YourTable>.datasource.items;
items.forEach(function(item){
item.<DateField> = newValue;
});
The only thing to take into account is that when using client scripting, you will only update the records loaded in the table at the moment; i.e, if your table has paging, it will only update the current page. If you are using paging, then you will need to add the following code to the onPreviousClick and the onNextClick event handlers of the pager widget:
var selectedDate = widget.root.descendants.<YourDatePicker>.value;
var items = widget.root.descendants.<YourTable>.datasource.items;
items.forEach(function(item){
item.<DateField> = selectedDate;
});

Select all Rows in Dynamics CRM Grid

Lets say I have a Grid in CRM showing me Invoices which need to be created by using a custom javascript dialog. In this dialog I want to put two options which are doing the following:
1) selects all Rows flagged as selected in the Grid
2) selects all existing Rows in the Grid
Particularly I have an issue with No. 2)
I cannot figure out how to get all rows if I have lets say 5 pages of invoices. So how can I select all rows in the Grid and not only those shown on the actual grid page?
Any advice is highly appreciated.
Regards
Sandro
If you want to retrieve only the elements that are selected in a grid, then you can use the Ribbon Workbench to configure a new button on the sub grid. The Button must have a Command Action which can be configured to pass selected items to a JavaScript method using the SelectedControlSelectedItemIds parameter
There is a SelectedControlAllItemIds parameter too, but I think this only gives "All Items" in the current page, not all pages. Not sure about these last bits so definitely worth a try
If that doesn't work, then you will have to do this yourself using a WebApi call. For example, to get all Contacts associated with an Account, you might do something like this:
// Get the ID of the parent account
var accountid = {AccountId};
// Execute a query to retrieve all Contacts associated with the given Account ID
Xrm.WebApi.retrieveMultipleRecords("contacts", "?$select=contactid&$filter=_parentcustomerid_value eq " + accountid)
.then(
function(results) { console.log(results); },
function(error) { console.log(error); }
);
Read more about retrieveMultipleRecords here

Database Question (LINQ-to-SQL)

I am currently working on a website where we can track small event for our clients. I am using linq-to-sql and asp.net to build it. So far the site is really small about 10 pages, and only 2 tables, events and clients. I have listed the columns for each table below:
Events:
-eventid
-eventname
-datecreated
-details
-datedue
-status
-clientname (this should be clientid)
-sentemail
Clients:
-clientid
-clientname
-clientemail
-clientphone
the issue I am having is getting the 2 database tables* to work together. Ideally I would like to have clientname replaced with clientid in the Events table.
I had it set up this way before but ran into an issue when listing out the events:
When I am listing out the events I would like it to show the client name as appose to the clientID. Unfortunately I am pretty new to ASP.net and although I could easily do it in PHP have no idea how to get it to work. Its not a huge deal but I would like to be able to update the client list and events apart from one another.
Sorry If this makes little to no sense, If there is any confusion I will try to word it better.
*correction on my part
Once you've got your database normalized, create a Linq2Sql context, and then you can write a query similar to this (to get a list of event names and their client names):
var results = from e in dbContext.Events join c in dbContext.Clients on e.clientid equals c.clientid select new { e.eventname, c.clientname };
in addition to the answer of Andrew Lewis you need to:
1) add the clientid field to the events table
2) run a sql query to fill the clientid field:
update events set clientid = (select clientid from client where name = events.clientname)
3) remove the clientname field from the events table

vb.net to reduce code for displaying data in textboxes

On the page I have created I have a search facility that if a doctors number is searched it will bring up the doctors details, once search button is clicked the results are displayed in textboxes (I cannot use gridviews because this is not wanted)
sample of code placed on the search button
Query statement = "SELECT DocNumber FROM tblDoctor WHERE DNum LIKE '%"
execute the query and get the result
The result is converted to string and Execute Scalar is used
DocNum.Text = Result1
Query statement = "SELECT DocName FROM tblDoctor WHERE DNum LIKE '%"
execute the query and get the result
The result is converted to string and Execute Scalar is used
DocName.Text = Result2
etc.... there are are 14 other textboxes that I want too display data in, so there is a large amount of repeated lines of code following the structure above. Can anyone suggest a better way of doing this?
Another problem of repetition of code comes from the prev page that is linked to it. The page before has a summary of details of doctors, once the row is clicked it takes you to this page displaying a more detailed view of their personal details. The doctor number selected will be passed onto the more detailed view using a querystring so I have the code
Automatic population of the selected doctors will fill the labels
on page load
Request the query string and store into variable dNum
Query statement = "SELECT DocNumber FROM tblDoctor WHERE DNum = " & dNum"
Get result from query convert to string and use execute scalar
lblDocNum.Text = Res1
Query statement = "SELECT DocNumber FROM tblDoctor WHERE DNum = " & dNum"
Get result from query convert to string and use execute scalar
lblDocNum.Text = Res1
etc...
What I am doing works correctly but the coding style looks poor. Any help would be much appreciated.
Thank you
Why not use a DataReader or DataSet or whatever you prefer to return the whole record, then simple move from column to column and populate the textboxes that way? Instead of returning one value at a time.
If the goal is less code,
SELECT * FROM tblDoctor WHERE xxx
into a DataTable or DataReader, as Thyamine suggested, above.
From there, you could also put the textboxes in an HTML table in a Repeater which you will bind to that datatable. You won't have to individually assign any of the values, the databinding will do it for you!
I know that people think HTML tables are evil but it's the easiest way to line up the labels I assume you will also want.
I also know that the control I suggested is called a Repeater but you only have one record. If you don't tell the compiler, I won't. :)
From parts of your question, it sounds like you're wondering whether to send all of the bits of information along in the querystring but that doesn't sound like a good idea to me because it invites users to mess with the data in the querystring.
You didn't mention - are these textboxes meant for editing? A Save button inside the Repeater would have easy access to all of the controls to build your update statement. You could also put the Save button outside the repeater and refer to the repeater's first Item to find the controls.

Resources