Send email when deleting record

Hi,

I have tried to see if there is already a post like this, but cant seem to find anything. Apologies if i have missed one

I have the app set up to email a department, when a record is added. They also get an email when its edited. both of these use the page submit rules. I would like to also send an email if its deleted, but as this isn’t done by a page submit, its not possible currently. Speaking with support, they said to look at custom code, so i guess here i am, hoping that someone has done something similar and can offer some advice? anything.

Thanks for reading,

Chris

Just to update, im currently running the code below, to ask the user to email the concerned department, and giving them the record ID to include:

$(document).on(‘knack-record-delete.view_56’, function(event, view, record) {
alert('You have removed this role. Please inform ORD. You should include the record ID (used by ORD), which is: ’ + record.id);
});

However as all other forms do the email for them, this leaves me open to problems (they miss the alert or are just too lazy to send it etc).

Looking around online, it doesnt look like i can send an email from javascript directly. Im still hoping to find some way of triggering an email automatically.

again, any help is appreciated.

Hello Chris,

You can create a form and submit that form via API, and in that form add a form rule to send an email.

Thanks,
Sunny Singla

hi,

Thanks for coming back to me on this. I have read through the attached, and i admit im a little lost here.

If im understanding this correct,

  • I create a form that loads the record being deleted, and sends its info via email
  • i then call that form via the api using the javascript for on delete?
  • this should then delete the record, as well as send the email, without the user seeing the form?
    would the form not fail, if the record is being deleted prior to the form?

apologies, but im not sure i grasp the concept here

chris

Hello Chris,

No, You no need to change any functionality.

  1. show views to the user where the user can click delete.
  2. Create a new object where we can store deleted records (fields that you want to delete)
  3. Create a new form to create a record in the newly created object (in that form you can send emails to users).
  4. After deleting a record use below code

$(document).on(‘knack-record-delete.view_56’, function(event, view, record) {
var data={};
data.field_X= record.id;
data.field_Y= record.field_xyz;
etc…
$.ajax({
url: ‘https://us-api.knack.com/v1/pages/scene_XXX/views/view_YYY/records’, // change sceneid and view id
type: ‘POST’,
data: data,
headers: {
‘Authorization’: Knack.getUserToken(),
‘X-Knack-Application-Id’: Knack.application_id
},
success: function (response) {

            }
        });

});

Hi,

Let me start by saying, you are amazing! this works, and is exactly what i’m after.

one problem i have with it. the fields that are ‘connections’ don’t seem to go through. All the date fields, short text fields etc go through and i seen a newly created record in the new object.However the fields that are ‘connections’ are blank.Do i have to do something different with them? In the new object, i have set them up as connections the same as the first object. Does the script need anything additional for connected fields?

The code is below:

$(document).on(‘knack-record-delete.view_56’, function(event, view, record) {
var data={};
data.field_340= record.id; //id
data.field_328= record.field_142; // connected field
data.field_329= record.field_143; //connected field
data.field_330= record.field_144; //date field
data.field_331= record.field_145; //date field
data.field_332= record.field_147; //Short text field
data.field_333= record.field_148; //Connected field
data.field_336= record.field_169; //Connected field
data.field_337= record.field_170; //Connected field
data.field_338= record.field_238; //Connected field

$.ajax({
url: ‘https://us-api.knack.com/v1/pages/scene_86/views/view_142/records’,
type: ‘POST’,
data: data,
headers: {
‘Authorization’: Knack.getUserToken(),
‘X-Knack-Application-Id’: Knack.application_id
},
success: function (response) {
}

    });

Bonus question: is there a way to get the code to monitor two views? Currently it is looking at view_56 but i would also like it to look at view_92. I guess i can copy the entire code, with the change, just wondering if there is a better way to do it?

Connected fields work like record.field_142_raw[0].id or if they are multiple then you need to use loop.

use for multiple views
$(document).on(‘knack-record-delete.view_56 , knack-record-delete.view_56’, function(event, view, record) {

thanks for coming back to me. i think i’m missing something here.

In my new object i have set the fields up as connections also. or should i have set them up as text?

When i try add what you suggested it stops the code running and gives me errors:

for example:

data.field_328_raw[0].id= record.field_142_raw[0].id;
image

you need to check that .

if there is a value then maybe that connection is empty. Or try to print that record data first

so its definetley that line thats causing the issue:

image

the [0], what does that refer to? is there anywhere i can read up on it?

both fields 328 and 143 are connected fields to the same object so shouldn’t be an issue there.

first do console.log(data); and see what i shows

Im sorry, i think i may be out of my depth here. The code isnt running at all in the browser, as it gives that error. Nothing is being moved to the new object when deleting now. When i inspect via the browser it points me to that line as the problem.

okay, so i have been playing around with it and found i just needed to do it on the second part, ie:
data.field_328= record.field_142_raw[0];
This seems to work, however it doesnt work for the conenctions to the user accounts. two of the fields are connections to users accounts.

console gives me:

Everything is going through, except the field 336 and 337. It so close i can almost taste it.

for field_336 and field_337 try field_336[0].id and same for 337 and 333

when i try 336[0].id or 336_raw[0].id i get errors in the console.

when i place [0].id on the fields there being copied from i get undefined, but if just have _raw[0] (as below), the data appears in the console, but isn’t being place in the object.

image

image

a record is being created with the all the information in the ‘deleted’ object, just the two connected fields for users is staying blank, even thought the correct id / identifier is being placed into the data?

Hello try field_336.id or field_337.id

no joy with that, it just gives the error in console and stops the code:
image
image

if i do this:
image
the correct info is being passed in to the ‘data’, see console log:
image
but doesn’t get sent to the object with the rest of the data. these field are blank when the record is inserted.

you are doing wrongly.

left side need to add field_id (new object) and right side need to add old object id’s

for any professional help please email me.

Regards,
Sunny Singla
ssingla1985@gmail.com

I really appreciate your help with this, id be very lost without you!

that is the way i have it currently. field 336 and 337 are for the new ‘deleted’ object. Field 169 and 170 are from the original object, where the item is being deleted from.

if i put [0] .id or .id after 169 or 170 i just get undefined in the console. But if i put _raw[0] after them i get the data appear in the console. it just doesn’t make it to the object, along with the other fields for some reason.

yes try to use _raw[0].id on right. on left no need to add anything like this. just data.field_xyz … if still having an issue then please email.