# Get relation changes in afterSave or beforeSave

**URL:** <https://community.parseplatform.org/t/get-relation-changes-in-aftersave-or-beforesave/2915>\
**Category:** Cloud Code\
**Created:** [September 15, 2022, 8:38pm UTC](https://community.parseplatform.org/t/get-relation-changes-in-aftersave-or-beforesave/2915 "2022-09-15T20:38:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnbraum](https://community.parseplatform.org/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@johnbraum](https://community.parseplatform.org/u/johnbraum)\
**Post date:** [September 15, 2022, 8:38pm UTC](https://community.parseplatform.org/t/get-relation-changes-in-aftersave-or-beforesave/2915/1 "2022-09-15T20:38:03Z")

</div>

Heys guys,

i have a Class “Department” with a relation “users” and an attribute userCount".

I want to set the userCount according to the count of the relation “users”. In beforeSave the changes to the relation aren’t applied and in the afterSave i will get a endless loop because I am saving the same object again.

Is there a trick to do that?

Thanks!

---

<div class="post-metadata">

**Author:** ![witekmikolajczak](https://community.parseplatform.org/user_avatar/community.parseplatform.org/witekmikolajczak/32/1323_2.png) [@witekmikolajczak](https://community.parseplatform.org/u/witekmikolajczak)\
**Post date:** [September 15, 2022, 9:13pm UTC](https://community.parseplatform.org/t/get-relation-changes-in-aftersave-or-beforesave/2915/2 "2022-09-15T21:13:31Z")

</div>

```auto
Parse.Cloud.afterSave('_User', async (request) => {
  const customClass = new Parse.Query('customClass');
  const userCount = new Parse.Query('userCount');

  const query = customClass.equalTo('objectId', customClassId);
  const customClassObject = await query.first()

  const customClassUsers= customClassObject.relation('users').query()
  const countUsers = await customClassUsers.count()

  const customClassCount = customClassObject.relation('userCount').add(countUsers)
}

```

hmm maybe something like that.  
**afterSave** trigger should trigger only once when object is saved.  
**countUsers** should be an object but .count() returning number, i don’t know how your schema looks like but maybe this will give you any clue.

---

<div class="post-metadata">

**Author:** ![johnbraum](https://community.parseplatform.org/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@johnbraum](https://community.parseplatform.org/u/johnbraum)\
**Post date:** [September 15, 2022, 9:17pm UTC](https://community.parseplatform.org/t/get-relation-changes-in-aftersave-or-beforesave/2915/3 "2022-09-15T21:17:47Z")

</div>

I’m sorry, i didnt describe my problem very well… There is no relation or Class userCount.

I have one Class “Department” with a relation called “users” and an attribute “userCount”. And i want to sync the count of the relation to this field. So I don’t have to fetch the relation to know the count of the users in the frontend.

---

<div class="post-metadata">

**Author:** ![witekmikolajczak](https://community.parseplatform.org/user_avatar/community.parseplatform.org/witekmikolajczak/32/1323_2.png) [@witekmikolajczak](https://community.parseplatform.org/u/witekmikolajczak)\
**Post date:** [September 16, 2022, 8:00am UTC](https://community.parseplatform.org/t/get-relation-changes-in-aftersave-or-beforesave/2915/4 "2022-09-16T08:00:46Z")

</div>

```auto
Parse.Cloud.afterSave('_User', async (request) => {
  const Department = Parse.Object.extend('Department');
  const departmentQuery = new Parse.Query(Department);
  const query = departmentQuery.equalTo('objectId', id); //get department with specified objectId
  const departmentObject = await query.first();
  const usersRelation = departmentObject.relation('users').query();
  query.first().then(async (res) => {
    const queryRelation = await res.relation('users').query().count(); //query users relation inside Department and retrive count of records
    departmentObject.set('userCount', queryRelation); // add this value to userCount
  });
});

```

i think this should work

---

<div class="post-metadata">

**Author:** ![johnbraum](https://community.parseplatform.org/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@johnbraum](https://community.parseplatform.org/u/johnbraum)\
**Post date:** [September 16, 2022, 8:43am UTC](https://community.parseplatform.org/t/get-relation-changes-in-aftersave-or-beforesave/2915/5 "2022-09-16T08:43:26Z")

</div>

Is the afterSave for the User triggered when I add a user to a relation of the department?

I had it like this:

```auto
Parse.Cloud.afterSave('Department', async ({ object }) => {
  const count = await object.relation('users').query().count({ useMasterKey: true });
  object.set('userCount', count);
  return object.save(null, { useMasterKey: true });
});

```

And this is obviously an endless loop…

---

<div class="post-metadata">

**Author:** ![dblythy](https://community.parseplatform.org/user_avatar/community.parseplatform.org/dblythy/32/231_2.png) [@dblythy](https://community.parseplatform.org/u/dblythy)\
**Post date:** [September 17, 2022, 4:46am UTC](https://community.parseplatform.org/t/get-relation-changes-in-aftersave-or-beforesave/2915/6 "2022-09-17T04:46:21Z")

</div>

The problem with `count` is that it runs over every single object in the database, which can be inefficient. See [avoiding count operations](https://docs.parseplatform.org/js/guide/#avoid-count-operations).

Instead, it would be more efficient to track the changes by using [operation](https://parseplatform.org/Parse-SDK-JS/api/master/Parse.Object.html#op): `object.op('users')`.

```auto
Parse.Cloud.afterSave('Department', async ({ object }) => {
  const op = object.op('users');
  if (op) {
    const changeCount = op.relationsToAdd.length - op.relationsToRemove.length;
    object.increment('userCount', changeCount);
    return object.save(null, { useMasterKey: true });
  }
}, {
  skipWithMasterKey: true
});

```

---

<div class="post-metadata">

**Author:** ![johnbraum](https://community.parseplatform.org/letter_avatar_proxy/v4/letter/j/dfb087/32.png) [@johnbraum](https://community.parseplatform.org/u/johnbraum)\
**Post date:** [September 17, 2022, 1:56pm UTC](https://community.parseplatform.org/t/get-relation-changes-in-aftersave-or-beforesave/2915/7 "2022-09-17T13:56:14Z")

</div>

Thanks! Helped a lot. \<3
