I am looking to automate certain push notifications based on whether data is present in a data source for a specific user. In this example, once a document has been uploaded and added to the data source, I would like a notification to be sent (just once) to that individual user letting them know a document is ready for them to view.
Thanks Ian. Having gone with the data source hook method I came up with the following to add to the data source hooks field in the Settings:
{
"type": "email",
"appId": [REMOVED],
"runOn": [
"insert"
],
"payload": {
"to": [
{
"name": "{{ Full name }}",
"type": "to",
"email": "{{ Email }}"
}
],
"html": "<h1>Your draft Will is ready for review, {{ First name }}.</h1><p>Head to the My Docs section of the app to access your Will.</p>",
"subject": "A document is ready for your review"
},
"conditions": [
{
"Will": {
"$exists": "true"
}
}
]
}
The idea is to send an email to the user when a document has been uploaded to the datasource to the ‘Will’ column.
Still getting to grips with when to use ‘{{’, ‘{{{’ or '{{ ’ but tried with each method and couldn’t get it to work. I know I’ll be missing something but can’t see what. Any ideas?
It doesn’t. I’ve also tried adding in hard values to the email and name fields. Some further information that might help - the app’s aren’t live yet and the data is being submitted to the datasource from a form in a separate admin app (all to the same single datasource though).
Hi Nathan
When the column you are referencing has a space inside it then handlebars require you to wrap the string in square brackets i.e. {{[foo bar]}} if you are trying to access {“foo bar”: “baz”}. So in your case it’ll be “name”: “{{ [Full name] }}” and {{ [First name] }}.
Can you try this? I have tested this hook and it is working as expected.
{
"type": "email",
"runOn": [
"insert"
],
"payload": {
"to": [
{
"name": "{{ [Full name] }}",
"type": "to",
"email": "{{ [Email] }}"
}
],
"html": "<h1>Your draft Will is ready for review, {{ [First name] }}.</h1><p>Head to the My Docs section of the app to access your Will.</p>",
"subject": "A document is ready for your review"
},
"conditions": [
{
"Will": {
"$ne": null
}
}
]
}
Also can I ask how you are writing the data to the DS? Are you using custom code to insert the data or using a form to enter the data?
Hi Deb - apologies for the delayed response. The data is written by an admin app (form component) to a datasource, this should then hopefully trigger the notification in the client-side mobile app. I will try the above code in the datasource and let you know.
Hi Deb - couldn’t get that working but think it must be my mistake somewhere. Is there any chance we could chat briefly over Zoom? Alternatively, would it help you looking at the app/datasource in question?
Hi Deb - thanks for your help with this. Your code worked but I had mistakenly left it as “runOn”: [“insert”] but from the screenshot you will see I was updating, not inserting. Works great!
Last minor point, the email in my inbox is from Fliplet - is there a way to change that name?
This has worked as it should but I’ve run into a problem.
The form in question is updated at several points. There are a few upload fields and a few choice fields and they are updated, only once, but at different times. The problem this has presented is the field ‘Will’ has a document uploaded to it and the notification works fine, I then come back into the form and upload a document in another field and the ‘Will’ field is still not equal to null so it sends the notification again.
Also, with the Yes/No (choice) fields I have used similar code, if they are left blank when the form is submitted they end up with ‘’ in the datasource so it concludes that the field is not equal to null and sends the notification anyway. Perhaps with these choice fields we can say “$eq”: ‘Yes’ or something similar?
Thanks Deb. I will try that with the Yes/No fields.
Any idea how I can go about the other issue? When the Will field does not equal null, the notification sends which is great, but then each other time the form is updated, it sees the field is not null and sends the same notification. Is there a way to only send it first time?
So the general idea is that you create a hidden form field that you use to determine if the notification has already been sent.
Create a hidden field in your form called “notificationSent”.
You can set the default value to “No”.
Use the snippet below which will check the DS and if it is already set to “No” then it will set it to “Yes”.
We need to add a condition to the DS hook to only send the notification when notificationSent: $eq: ‘No’.
Code for the screen JS:
//Change to the DS ID of where the entry is stored.
Fliplet.DataSources.connect(123456)
.then(function(connection) {
return connection.findById(Fliplet.Navigate.query.dataSourceEntryId);
})
.then(function(record) {
if (record.data.notificationSent === 'No') {
Fliplet.FormBuilder.get().then(function(form) {
form.field('notificationSent').set('Yes');
});
}
});
Ah okay that makes sense. The only other bit of information you don’t from me which may mean this doesn’t work is that there are three Yes/No fields and three documents upload fields, similar to ‘Will’ field (I’ve only told you about the ‘Will’ field!). A different document is uploaded to them at separate times.
Does this also set the hidden field of the other two document upload fields so they end up sending all three notifications at the same time? Can we maybe set ‘notificationSent’ using AfterFormSubmit and I have added the relevant doc each time?
I’ve attached a screenshot of the DS. The field titles ending in a question mark are Yes/No and the other three are document uploads. Most of them have data added to them at separate times.
Yes this can be extended to other form fields. Yes you can also use AfterFormSubmit to update the column. You can make a query with afterFormSubmit to update the relevant column. Are you comfortable to make these enhancements to the code?
I want utilise this across a few different fields, each having information submitted to them at different times, so if I utilise the screen JS for all of them will it set the field to ‘Yes’ for all of them on the first submit?
To avoid that, is is possible to update the screen JS to only set the notificationSent fields (I will have one for each field e.g. notificationSentWill, notificationSentInvoice, notificationSentDocuments etc.) to Yes if the field is not empty?
The AfterFormSubmit might just be over-complicating it.
Let me know if a screen recording or something similar would help!