I have a function called FlipDataViewItemIsSelected:
function FlipDataViewItemIsSelected(five, context, result) {
five.log('FlipDataViewItemIsSelected');
const _five = five;
const myForm = five.getVariable('MyForm');
const myKey = five.getVariable('MyKey');
const subformName = five.getVariable('SubformName');
const ctxResult = GetSelectedJoinTableContext(five, subformName);
const myParms = ctxResult.context;
// collect new TO email list if wanted
if (myForm === 'GenerateEmails' && myKey === 'GENERIC') {
myParms.ReturnSelectedEmails = true;
}
five.executeFunction('SetIsSelectedServer', myParms, null, '', '', function (result) {
five.log(`client: result.serverResponse=${JSON.stringify(result.serverResponse)} `);
// handle error
if (result.serverResponse.errorCode !== 'ErrErrorOk') {
const functionMessage = result.serverResponse.message || 'Unknown server error';
_five.showMessage(functionMessage);
return;
}
const toList = result.serverResponse.ToList || '';
if (toList) {
five.log(`Send to: ${toList}`);
} else {
five.log('No TO list returned.');
}
});
}
it calls SetIsSelectedServer:
function SetIsSelectedServer(five, context, result) {
/*
modify the 'IsSelected' field in context.SelectedTable table as follows:
if context contains an 'IsSelected' member: use its value to set 'IsSelected' in all records for context.UserKey
otherwise: set 'IsSelected' to context.SelectedValue for record identified by context.UserKey and context.SelectedField
if context.IsSetup = true, update the permanent join table as follows:
first, delete all records from context.JoinTable for context.LeftValue
ex: DELETE FROM BoardReports WHERE BoardPositionKey = '{CurrentLeftFieldValue}'
second, add records back into context.JoinTable for 'selected' records in context.SelectedTable for this user and 'Right' field value
ex: INSERT INTO BoardReports (BoardPositionKey, ReportKey) SELECT '{CurrentBoardPositionKey}', ReportKey
FROM SelectedReports WHERE UserKey = '{CurrentUserKey}' AND IsSelected = true
expected context:
{
UserKey: five.getVariable('UserKey'),
IsSetupForm: true | false,
IsSelectAllForm: true | false,
IsSelectNoneForm: false | true,
IsUseDefaults: isUseDefaults,
SelectedTable: selectedTable,
SelectedEntityField: selectedField,
SelectedEntityName: selectedEntityName,
SelectedValue: selectedValue,
JoinTable: joinTable,
JoinLeftField: joinLeftField,
JoinLeftValue: joinLeftValue,
JoinRightField: joinRightField,
JoinRightValue: joinRightValue,
IsSelected: true|false|missing, - (optional, see first note above)
ReturnSelectedEmails: true|false|missing (optional, return selected member emails if truthy)
};
*/
let sql;
let phase = 'starting';
try {
// determine whether to set all records for UserKey or flip a single record
if (context.IsSelected !== undefined) {
phase = 'set all IsSelected';
sql = `UPDATE \`${context.SelectedTable}\` SET \`IsSelected\` = ? WHERE UserKey = ?`;
five.executeQuery(sql, 0, context.IsSelected, context.UserKey);
// five.log(`SetIsSelectedServer: ${phase}, userKey=${context.UserKey}, IsSelected=${context.IsSelected}`);
} else {
phase = 'flip single IsSelected';
sql = `UPDATE \`${context.SelectedTable}\` SET \`IsSelected\` = NOT \`IsSelected\`
WHERE UserKey = ? AND \`${context.SelectedEntityField}\` = ?`;
five.executeQuery(sql, 0, context.UserKey, context.SelectedEntityValue);
// five.log(`SetIsSelectedServer: ${phase}, userKey=${context.UserKey}, selectedValue=${context.SelectedEntityValue}`);
}
// update join table if setup form
if (context.IsSetupForm) {
phase = 'update join table';
// delete existing join records
sql = `DELETE FROM \`${context.JoinTable}\` WHERE \`${context.JoinLeftField}\` = ?`;
five.executeQuery(sql, 0, context.JoinLeftValue);
// insert selected records for this user
sql = `INSERT INTO \`${context.JoinTable}\` (\`${context.JoinLeftField}\`, \`${context.JoinRightField}\`)
SELECT ? AS \`${context.JoinLeftField}\`, \`${context.SelectedEntityField}\`
FROM \`${context.SelectedTable}\`
WHERE IsSelected = true AND UserKey = ?`;
five.executeQuery(sql, 0, context.JoinLeftValue, context.UserKey);
five.log(`SetIsSelectedServer: ${phase} completed`);
}
// optionally rebuild the TO list if requested
if (context.ReturnSelectedEmails) {
phase = 'rebuild TO list';
sql = `SELECT m.FullName, m.Email FROM SelectedMembers AS sm
INNER JOIN Members AS m ON m.MembersKey = sm.MemberKey
WHERE sm.UserKey = ? AND sm.IsSelected = true
AND m.Email IS NOT NULL AND m.Email <> ''
ORDER BY m.SortName`;
const queryResult = five.executeQuery(sql, 0, context.UserKey);
const rows = queryResult.values || [];
const toList = rows.map(r => `${r.FullName} <${r.Email}>`).join('; ');
five.log(`server: toList: ${toList}`);
result.ToList = toList;
// return five.success(result);
}
return five.success(result);
} catch (e) {
five.log(`SetIsSelectedServer ERROR during "${phase}": ${e.message}`);
return five.error(`Error during "${phase}": ${e.message}`);
}
}
Right now, I’m only concerned with the code inside the IF block near the end. I want to create a string of email addresses and return them to the client. the query works, and the server function properly logs the result.
But I can’t seem to get the returned text to appear properly in the client function. I’ve tried many different ways using the result object. the only way I can get it to be seen by client is to just return the string in the message property. but that causes a message box / alert to pop up in the client UI. So can you please help me with this? ChatGPT thinks it’s smart enough to answer this, but not really. I get a lot of help from that, but it doesn’t know Five.
Can you help please? Thanks…