Sample Code:global class SubscribeOrUnsubscribeLead {
webservice static String subscribeLead(Id leadId) {
List<EntitySubscription> listEntitySub = new List<EntitySubscription>();
listEntitySub = [SELECT Id FROM EntitySubscription WHERE ParentId =: leadId AND SubscriberId =:
UserInfo.getUserId()];
if(listEntitySub.size() == 0) {
EntitySubscription entitySub = new EntitySubscription(ParentId = leadId, SubscriberId = UserInfo.getUserId());
insert entitySub;
return ‘Subscribed’;
} else {
return ‘You have already subscribed’;
}
}
webservice static String unSubscribeLead(Id leadId) {
List<EntitySubscription> listEntitySub = new List<EntitySubscription>();
listEntitySub = [SELECT Id FROM EntitySubscription WHERE ParentId =: leadId AND SubscriberId =:
UserInfo.getUserId()];
if(listEntitySub.size() > 0) {
delete listEntitySub;
return ‘Unsubscribed’;
} else {
return ‘You are not subscribed to this’;
}
}
}
webservice static String subscribeLead(Id leadId) {
List<EntitySubscription> listEntitySub = new List<EntitySubscription>();
listEntitySub = [SELECT Id FROM EntitySubscription WHERE ParentId =: leadId AND SubscriberId =:
UserInfo.getUserId()];
if(listEntitySub.size() == 0) {
EntitySubscription entitySub = new EntitySubscription(ParentId = leadId, SubscriberId = UserInfo.getUserId());
insert entitySub;
return ‘Subscribed’;
} else {
return ‘You have already subscribed’;
}
}
webservice static String unSubscribeLead(Id leadId) {
List<EntitySubscription> listEntitySub = new List<EntitySubscription>();
listEntitySub = [SELECT Id FROM EntitySubscription WHERE ParentId =: leadId AND SubscriberId =:
UserInfo.getUserId()];
if(listEntitySub.size() > 0) {
delete listEntitySub;
return ‘Unsubscribed’;
} else {
return ‘You are not subscribed to this’;
}
}
}
Subscribe button code:
{!REQUIRESCRIPT(“/soap/ajax/15.0/connection.js”)};
{!REQUIRESCRIPT(“/soap/ajax/15.0/apex.js”)};
alert(sforce.apex.execute(“InfallibleTech.SubscribeOrUnsubscribeLead”,”subscribeLead”,{leadId:”{!Lead.Id}”}));
location.reload(true);
Unsubscribe button code:
{!REQUIRESCRIPT(“/soap/ajax/15.0/connection.js”)}
{!REQUIRESCRIPT(“/soap/ajax/15.0/apex.js”)}
alert(sforce.apex.execute(“InfallibleTech.SubscribeOrUnsubscribeLead”,”unSubscribeLead”,{leadId:”{!Lead.Id}”}));
location.reload(true);
Output: