Skip to content

Instantly share code, notes, and snippets.

@arun12209
Created July 16, 2023 15:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save arun12209/2b878206993ac63c9d823bef8cdb259d to your computer and use it in GitHub Desktop.
AccountController.cls
public with sharing class AccountController {
@AuraEnabled(cacheable=true)
public static List<Account> getSubscribedAccounts() {
List<Account> subscribedAccounts = new List<Account>();
Set<Id> accountIds = new Set<Id>();
List<EntitySubscription> subscriptions = [
SELECT ParentId
FROM EntitySubscription
WHERE SubscriberId = :UserInfo.getUserId()
];
Set<Id> accountRecordTypeIds = new Set<Id>();
for (EntitySubscription subscription : subscriptions) {
if (subscription.ParentId.getSObjectType() == Account.sObjectType) {
accountIds.add(subscription.ParentId);
accountRecordTypeIds.add(subscription.ParentId);
}
}
if (!accountRecordTypeIds.isEmpty()) {
subscribedAccounts = [
SELECT Id, Name
FROM Account
WHERE Id IN :accountRecordTypeIds
];
}
return subscribedAccounts;
}
@AuraEnabled
public static Boolean removeAccountSubscription(Id accountId) {
try {
List<EntitySubscription> subscriptions = [
SELECT Id
FROM EntitySubscription
WHERE SubscriberId = :UserInfo.getUserId()
AND ParentId = :accountId
AND Parent.Type = 'Account'
];
if (!subscriptions.isEmpty()) {
delete subscriptions;
return true;
}
} catch (Exception ex) {
System.debug('Error removing account subscription: ' + ex.getMessage());
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment