# Automatically Apply Badges to Group Members (in Hosted Version)

**URL:** https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728
**Category:** Support
**Created:** [Junho 1, 2017, 11:58am UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728 "2017-06-01T11:58:29Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![apautler](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@apautler](https://meta.discourse.org/u/apautler)
#### Post date: [Junho 1, 2017, 11:58am UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/1 "2017-06-01T11:58:29Z")

</div>

I’d like to automatically apply certain badges to users in certain groups. [In searching the forum](https://meta.discourse.org/t/automatically-grant-a-badge-for-all-members-of-a-group/18147), it appears this may be possible via SQL queries, etc. We are on the hosted plan, so that is not possible. In talking with Discourse support, it appears a solution might be to make an app/script that uses the API to apply the badge to users in a groups. Two questions with this:

1. Has someone done this already and would be willing to share?
2. How would I implement an app/script that uses the API within the hosted forum setup?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [Junho 1, 2017, 12:41pm UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/2 "2017-06-01T12:41:01Z")

</div>

> [@apautler](#):
>
> We are on the hosted plan, so that is not possible.

It is possible on hosted plans, just let us know by emailing [team@discourse.org](mailto:team@discourse.org) and we can enabled them.

---

<div class="post-metadata">

### Author: ![apautler](https://avatars.discourse-cdn.com/v4/letter/a/977dab/32.png) [@apautler](https://meta.discourse.org/u/apautler)
#### Post date: [Junho 1, 2017, 12:47pm UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/3 "2017-06-01T12:47:29Z")

</div>

@sam Gotcha. What do you think is the better option though? Is it dangerous to begin making SQL queries from within the hosted platform? Makes me a bit nervous because there are other admins in our account and I worry if they accidentally do something in the currently hidden SQL area, it could seriously mess up our database.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [Junho 1, 2017, 12:49pm UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/4 "2017-06-01T12:49:00Z")

</div>

If this is a once off I would just make the badges, let us know when you are done and then disable badge SQL.

Should be fine.

I think it makes sense to extend groups to, optionally, automatically grant a badge, so SQL is not involved.

You can also do this via the API but it would require running a regular job on your end.

---

<div class="post-metadata">

### Author: ![Cozdabuch](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cozdabuch/32/139120_2.png) [@Cozdabuch](https://meta.discourse.org/u/Cozdabuch)
#### Post date: [Outubro 3, 2017, 2:42am UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/5 "2017-10-03T02:42:56Z")

</div>

Is there a way to paste an example code that we can upload on our site?  
Is user1 is added to groupX, award badgeX

Simple, nothing more to it

---

<div class="post-metadata">

### Author: ![scombs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/scombs/32/48616_2.png) [@scombs](https://meta.discourse.org/u/scombs)
#### Post date: [Outubro 3, 2017, 2:57am UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/6 "2017-10-03T02:57:08Z")

</div>

Here is the badge SQL, just replace your\_group\_name

```
select user_id, created_at granted_at, NULL post_id
from group_users
where group_id = (
  select g.id from groups g where g.name = 'your_group_name'
)

```

---

<div class="post-metadata">

### Author: ![Cozdabuch](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cozdabuch/32/139120_2.png) [@Cozdabuch](https://meta.discourse.org/u/Cozdabuch)
#### Post date: [Outubro 3, 2017, 3:06am UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/7 "2017-10-03T03:06:54Z")

</div>

This will run via the scheduler correct? It is not triggered be the group add, but just runs daily and awards based on group membership?

---

<div class="post-metadata">

### Author: ![scombs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/scombs/32/48616_2.png) [@scombs](https://meta.discourse.org/u/scombs)
#### Post date: [Outubro 3, 2017, 3:29am UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/8 "2017-10-03T03:29:29Z")

</div>

In the badge settings you need to set the trigger to daily and select the option for revocation daily

 ![Screenshot_20171002-232556](https://global.discourse-cdn.com/meta/original/3X/e/9/e950cfad7120e8baeeaf72800eb61c7e4082ecef.jpg)

---

<div class="post-metadata">

### Author: ![Cozdabuch](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cozdabuch/32/139120_2.png) [@Cozdabuch](https://meta.discourse.org/u/Cozdabuch)
#### Post date: [Outubro 3, 2017, 10:56pm UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/9 "2017-10-03T22:56:50Z")

</div>

@scombs, I had posted in another thread, and someone responded almost at the same time. The code is slightly different. Do you know what the difference would be?

main difference is: **`gu.`** vs **`g.`**

Other poster’s code:

```plaintext
SELECT gu.user_id, gu.created_at AS granted_at, null AS post_id
FROM group_users AS gu
WHERE gu.group_id = ( SELECT id FROM groups WHERE name ilike 'InsertTheTeamNameHere' )

```

is there any practical difference?  
I’m a complete novice at this, and I just want to give the site owner the code to copy/paste so he doesn’t have to modify it at all.

btw, the group name we are using is: `Confirmed_ST_User`

---

<div class="post-metadata">

### Author: ![scombs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/scombs/32/48616_2.png) [@scombs](https://meta.discourse.org/u/scombs)
#### Post date: [Outubro 4, 2017, 12:00am UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/10 "2017-10-04T00:00:47Z")

</div>

Not sure. I saw the [other post](https://meta.discourse.org/t/badge-sql-can-no-longer-be-edited-by-default/47894/30) too.

Here is the #howto with the code

> [@Grant a badge for all members of a group](https://meta.discourse.org/t/automatically-grant-a-badge-for-all-members-of-a-group/18147):
>
> I just created a badge for all the awesome [plugin authors](https://meta.discourse.org/badges). Creating a badge targeting a group is quite easy, just type this into the SQL box: select user\_id, created\_at granted\_at, NULL post\_id from group\_users where group\_id = ( select g.id from groups g where g.name = 'GROUP\_NAME\_IN\_LOWER\_HERE' ) I will add a trigger for this at some point so granting is faster, in the mean time just go to /sidekiq/scheduler and kick off the badge job to rush it. See [Triggered custom badge queries](https://meta.discourse.org/t/triggered-custom-badge-queries/19336) for mo…

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [Outubro 4, 2017, 12:18am UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/11 "2017-10-04T00:18:44Z")

</div>

Should be the same as I explained there. 🙂

---

<div class="post-metadata">

### Author: ![Cozdabuch](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cozdabuch/32/139120_2.png) [@Cozdabuch](https://meta.discourse.org/u/Cozdabuch)
#### Post date: [Outubro 4, 2017, 12:47am UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/12 "2017-10-04T00:47:28Z")

</div>

> [@scombs](#):
>
> select user\_id, created\_at granted\_at, NULL post\_id  
> from group\_users  
> where group\_id = (  
> select g.id from groups g where g.name = ‘your\_group\_name’  
> )

thanks @scombs @pfaffman!! 😀

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [Outubro 4, 2017, 1:34am UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/13 "2017-10-04T01:34:51Z")

</div>

The code is identical, just some renaming, capitalization, and formatting differences.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [Outubro 31, 2019, 1:09pm UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/14 "2019-10-31T13:09:08Z")

</div>



---

<div class="post-metadata">

### Author: ![paulrudy](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/paulrudy/32/259901_2.png) [@paulrudy](https://meta.discourse.org/u/paulrudy)
#### Post date: [Outubro 21, 2020, 8:49pm UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/15 "2020-10-21T20:49:25Z")

</div>

> [@sam](#):
>
> Acho que faz sentido estender os grupos para, opcionalmente, conceder automaticamente uma insígnia, de modo que o SQL não seja envolvido.

Espero que não haja problema em reativar este tópico para perguntar: Existe alguma probabilidade de estender os grupos para incluir a opção de conceder automaticamente uma insígnia, sem SQL?

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [Maio 21, 2021, 6:41pm UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/16 "2021-05-21T18:41:54Z")

</div>

Estou apenas adicionando uma nota aqui para informar que não é mais possível habilitar SQL de emblemas personalizados para sites nos planos de hospedagem Standard ou Business. Você ainda pode criar emblemas personalizados, mas precisará concedê-los manualmente ou por meio da API do Discourse. Detalhes sobre como conceder um emblema via API podem ser encontrados aqui: [Grant a custom badge through the API](https://meta.discourse.org/t/how-to-grant-a-custom-badge-through-the-api/103270).

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [Maio 21, 2021, 6:41pm UTC](https://meta.discourse.org/t/automatically-apply-badges-to-group-members-in-hosted-version/63728/17 "2021-05-21T18:41:58Z")

</div>


