I have an Employee
class and Salary
class.
Salary class has a pointer to Employee
class. Employee
does not have salaries pointers. But I can extend if needed.
When I remove Employee
record I would like to remove associated Salary
with it. Is this possible to do via some Back4App script or function?
Or there is just a way to do it via client code. I understand that I can create a query to remove Salary where employee is needed pointer:
let query = PFQuery(className: "Salary")
query.whereKey("employee", equalTo: pfEmployeeObjectReference)
query.findObjectsInBackground { (objects, error) in
//... skip cycle here
object.deleteEventually()
For your use-case, I recommend using the afterDelete Trigger of Employee class. To read more about triggers visit the following link: https://docs.parseplatform.org/cloudcode/guide/#afterdelete
So your after delete trigger code should look something like this:
Parse.Cloud.afterDelete("Employee", (request) => {
//This is the deleted object
var deletedObject = request.object
//Fetching the Salary object associated with the Employee object
var salaryObj = await deletedObject.fetch("pointer_to_salary")
//Deleting the salary object
await salaryObj.destroy({useMasterKey:true});
});