Monday, 21 July 2014

Apex code to create a work.com Thanks


Some situations demand us to create work.com thanks batch through apex code . Here I am explaining how to do this
  For this you need  enable work.com in your org.  Then SF will create the related objects.  Now using apex code you can create a chatter post which can hold a Thanks batch.

Below are the objects related to work.com

WorkBadge
WorkBadgeDefinition
WorkBadgeDefinitionHistory
WorkBadgeDefinitionShare WorkThanksShare


First query a badge definition from  WorkBadgeDefinition  object.

WorkBadgeDefinition wbd = [SELECT CreatedById,CreatedDate,Description,Id,ImageUrl,IsActive,IsCompanyWide,IsDeleted,IsLimitPerUser,LastModifiedById,LastModifiedDate,LimitNumber,LimitStartDate,Name,OwnerId,SystemModstamp FROM WorkBadgeDefinition where Name ='Thanks' limit 1];  
   
Now create a work thanks object and fill the required fields

WorkThanks wt =  new WorkThanks();
wt.Message = 'congrats';
wt.giverId = userinfo.getUserId();
insert wt;

Create a badge with badge definition

WorkBadge wb = new WorkBadge();
wb.RecipientId = userinfo.getUserId();
wb.SourceId  = wt.id;
wb.DefinitionId = wbd.id;
insert wb;


Create a chatter post through feeditem object to hold the thanks

feeditem  ft = new feeditem();
ft.type= 'RypplePost'; // tyep has to be this to hols a thanks
ft.body = 'Thanks';
ft.RelatedRecordId = wt.id;
ft.parentid = '0019000000xZ3Ky';
insert ft;

No comments:

Post a Comment