---
schema_version: "1"
id: 53cf61a4bcabdf0c8e39db5fa07b3b0d546dbedb49b485de798b2bab64179add
title: Use the API to Give Recognition Badges Sample Code
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/experience/264-0-0/networks_recog_badges_via_api.md
canonical_url: https://help.salesforce.com/s/articleView?id=experience.networks_recog_badges_via_api.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 69979d55bd7320f583dbd596c9540bc0ce31f3f5682e8bd255f57aaeb68e4fac
description: You can use the API to programmatically give recognition badges to Experience Cloud site members.
products:
  - Experiences
release: "264"
---
# Use the API to Give Recognition Badges Sample Code

You can use the API to programmatically give recognition badges to Experience Cloud site members.

## Assign a Recognition Badge and Notify a Member Using Apex

```
ID networkID = '0DBB0000000CdBaOAK';
ID badgeDefintionID = '0W1B0000000ekuJKAQ';
ID giverID = '005B0000003InDeIAK';
ID recipientID = '005B000000483dDIAQ';
String message = 'Welcome to the site';

WorkThanks thanks = new WorkThanks (
NetworkdID=networkID
GiverID=giverID,
Message=message
);
insert thanks;

WorkBadge badge = new WorkBadge(
DefinitionId=badgeDefinitionId,
NetworkID=networkID
RecipientId=recipientID,
SourceId=thanks.id
);
insert badge;

// optional: create a Chatter post on the user profile
// showing the badge. The user will be notified through
// the standard Chatter notification mechanisms.
FeedItem feedItem = new FeedItem(
NetworkScope=networkID,
ParentId=recipientID,
RelatedRecordId=thanks.id,
Body=message,
Type='RypplePost',
Visibility='AllUsers'
);
insert feedItem;

// Unfortunately, there is a limitation in the WDC API that
// prevents these 2 lines from working:
// thanks.FeedItemID = :feedItem.id;
// update thanks;
```

## Assign a Recognition Badge When a Member Reaches a Number of Reputation Points

To avoid assigning the same badge twice, we’ve added a custom “hasBadge\_c” field on the user object.

```
trigger NetworkMemberTrigger on NetworkMember (after update) {
              for (NetworkMember nm : Trigger.New) {
                   User user = [select ID, hasBadge_c from User where ID = 
:nm. MemberID]; 
         If (nm.ReputationPonts >= 40 && !user.hasBadge_c) {
             WorkThanks thanks = new WorkThanks(
             GiverId='005B00000036ukY',  
             Message='inserted by trigger',
             NetworkId=nm.NetworkId);
             insert thanks;

            WorkBadge badge = new WorkBadge(
            DefinitionId='0W1B0000000T1QBKA0',
            NetworkId=nm.NetworkId,
            RecipientId=nm.MemberId,
            SourceId=thanks.id);
            insert badge;

            user.hasBadge_c = true;
            update user;
      }  
   }
}
```

## Automatically Add Reputation Points When Badge Is Assigned to a Member

To avoid hardcoding the number of reputation points that are assigned with the badge, this example assumes that the WorkBadgeDefinition entity has been customized with an additional "ReputationPoints\_\_c" field.

```
trigger WorkBadgeTrigger on WorkBadge (after insert) {
             for (WorkBadge badge : Trigger.new) {
                 if (badge.DefinitionId == '0W1B0000000T1QBKA0') {
                     WorkBadgeDefinition badgeDefinition = [select Id,
ReputationPoints_c from WorkBadgeDefinition where Id = 
:badge.DefinitionId];
            NetworkMember member = [select Id, ReputationPoints from
NetworkMember where MemberID = :badge.RecipientId];
            member.ReputationPoints = member.ReputationPoints + 
badgeDefinition.ReputationPoints_c;
            update member;
      }
   }
}
```

## Assign Recognition Badge When Member Creates Number of Posts or Comments

The ChatterActivity object contains the following useful fields:

- CommentCount
- CommentRecievedCount
- LikeReceivedCount
- PostCount

These counters can be used to programmatically assign badges based on various thresholds. For example, members could get a badge whenever they create 100 posts or whenever they received 100 likes.

```
trigger ChatterActivityTrigger on ChatterActivity (after update) {
              for (ChatterActivity ca : Trigger.New) {
                  if (ca.PostCount >= 10) {
                      // TODO: assign badge to user if he hasn't received one already
      }
   }
}
```