Tag Archives: Apex

Manage Knowledge Articles Using Apex

Create new article:

Knowledge__kav ka = new Knowledge__kav();
ka.Title = 'Salesforce CRM';
ka.UrlName = 'salesforce-crm';
ka.Summary = 'Salesforce Cloud CRM';
ka.Language = 'en_US';
insert ka;

Publish a draft article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.publishArticle(knowledgeArticleId, true);

Unpublish a published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.publishArticle(knowledgeArticleId, true);

Schedule archive of a published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
Datetime scheduledDate = System.now().addMonths(2); //Add date to schedule the archive
KbManagement.PublishingService.archiveOnlineArticle(knowledgeArticleId, scheduledDate);

Note: If the specified scheduledDate is null, the article is archived immediately.

Cancel a scheduled archive published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.cancelScheduledArchivingOfArticle(knowledgeArticleId);

Delete an archived article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.deleteArchivedArticle(knowledgeArticleId);

Delete a draft article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.deleteDraftArticle(knowledgeArticleId);

Create a draft article from the archived article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.editArchivedArticle(knowledgeArticleId);

Unpublish a published article:

String knowledgeArticleId = 'kA06g000002AJ7t'; //Add knowledge article record id
KbManagement.PublishingService.editOnlineArticle(knowledgeArticleId, true);

Create FeedComment Record in Apex Test Class

Sample Code:

//Parent Record
Account acc = new Account(Name = 'Test Account');
insert acc;

//Create Related Feed Item Record
FeedItem fi = new FeedItem(ParentId = acc.Id, Body = 'Test Body');
insert fi;

//Create Feed Comment Record
FeedComment fc = new FeedComment(FeedItemId = fi.Id, CommentBody = 'Test Comment');
insert fc;

//Get Feed Comment Record
FeedComment objFC = [Select Id, CommentBody, FeedItemId, ParentId FROM FeedComment LIMIT 1];

//Check Feed Comment Parent Id
System.assertEquals(objFC.ParentId, acc.Id);

Check Current User has a Custom Permission in Salesforce Using Apex

we can use FeatureManagement.checkPermission method, to determine which users have access to a specific custom permission.

Sample Code:

Boolean hasCustomPermission = FeatureManagement.checkPermission('YOUR_CUSTOM_PERMISSION_API_NAME');

Converting DateTime to Date in Salesforce

Solution 1: Create a date newInstance() and pass the Year, Month and Day from the dateTime.

DateTime dt = System.now();
Date d = Date.newInstance(dt.year(), dt.month(), dt.day());
System.debug('Print Date:' + d);

Solution 2: Get date from the DateTime using Date() method.

DateTime dt = System.now()
Date d = dt.date();
System.debug('Print Date:' + d);

Get Record Type Info In Apex

Get Record Type Id by Name:

Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Partner Account').getRecordTypeId();

Get Record Type Id by Developer Name:

Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Partner_Account').getRecordTypeId();

Get Record Type Developer Name by Name:

String recordTypeDevName = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Partner Account').getDeveloperName();

Get Record Type Developer Name by Id:

String recordTypeDevName = Schema.SObjectType.Account.getRecordTypeInfosById().get('01258000000DKIg').getDeveloperName();

Get Record Type Name by Developer Name:

String recordTypeName = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Partner_Account').getName();

Get Record Type Name by Id:

String recordTypeName = Schema.SObjectType.Account.getRecordTypeInfosById().get('01258000000DKIg').getName();