I have been trying to work on a requirement where I made an app using App Maker, it is a directory app, user will open the homepage for that app and should be able to see the list of his contacts in the same OrgUnit that he is in on G Suite.
I understand that the default people or directory model does not show the orgUnitPath attribute, so I used the AdminDirectory directly.
In application settings I enabled the AdminDirectory API. I created a new calculated datasource named Test. Then I added 3 fields to the datasource: primaryEmail, fllName, and orgUnitPath. Then in the events I added the following script:
var usersArr = [];
var pageToken, page;
var conditions = {
customer: 'my_customer',
query: 'orgUnitPath=\'/OU_NAME\'',
pageToken: pageToken
}
do {
page = AdminDirectory.Users.list(conditions);
for (var i=0;i<page.users.length;i++)
{
console.log(page.users[i].orgUnitPath);
usersArr.push(page.users[i].primaryEmail,page.users[i].name.fullName,page.users[i].orgUnitPath);
}
//page.users.forEach(function(user) {
//usersArr.push(user.primaryEmail, user.name.fullName, user.orgUnitPath);
//});
if (page.nextPageToken) {
conditions.pageToken = page.nextPageToken;
}
} while (page.pageToken);
console.log(usersArr.length);
console.log(usersArr);
return usersArr;
When I try to preview the app, I get the following error in console:
E - Tue Oct 16 11:36:43 GMT+300 2018 - The function queryRecords must return an array of records, but the array contained an element that was not a record. Error: The function queryRecords must return an array of records, but the array contained an element that was not a record.
E - Tue Oct 16 11:36:43 GMT+300 2018 - Executing query for datasource Test: (Error) : The function queryRecords must return an array of records, but the array contained an element that was not a record.
E - Tue Oct 16 11:36:43 GMT+300 2018 - Executing query for datasource Test failed.
I think I am not getting the point where I need to make the returned array into proper format to be accepted by the datasource so I can use it with it.
Not sure if I did put all the required details.. Please let me know of any missing information and I'll put them here!
I would really appreciate any help or guidance on this...
Regards
App Maker is expecting you to return an array of Record objects for the datasource you are defining (in this case "Test" records).
You can put this script in the datasource server script or as a function that's called from the datasource server script as Markus suggested.
var usersArr = [];
var pageToken, page;
var conditions = {
customer: 'my_customer',
query: 'orgUnitPath=\'/OU_NAME\'',
pageToken: pageToken
}
do {
page = AdminDirectory.Users.list(conditions);
for (var i = 0; i < page.users.length; i++) {
var userRecord = app.models.Test.newRecord();
userRecord.primaryEmail = page.users[i].primaryEmail;
userRecord.fullName = page.users[i].name.fullName;
userRecord.orgUnitPath = page.users[i].orgUnitPath;
usersArr.push(userRecord);
}
if (page.nextPageToken) {
conditions.pageToken = page.nextPageToken;
}
} while (page.pageToken);
return usersArr;