Tag Archives: Apex

Get Weekly Days Business Hours Difference

BusinessHours bh = [SELECT Id, Name, IsActive, IsDefault, SundayStartTime, SundayEndTime, 
                    MondayStartTime, MondayEndTime, TuesdayStartTime, TuesdayEndTime, 
                    WednesdayStartTime, WednesdayEndTime, ThursdayStartTime, ThursdayEndTime,
                    FridayStartTime, FridayEndTime, SaturdayStartTime, SaturdayEndTime,
                    TimeZoneSidKey FROM BusinessHours WHERE IsDefault = true];

DateTime startTime;
DateTime endTime;
Datetime dt = System.now();
String dayOfWeek = dt.format('EEEE');

if(dayOfWeek.equalsIgnoreCase('Saunday')){
    startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.SundayStartTime.hour(), bh.SundayStartTime.minute(), bh.SundayStartTime.second(), 0));
    endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.SundayEndTime.hour(), bh.SundayEndTime.minute(), bh.SundayEndTime.second(), 0));
}else if(dayOfWeek.equalsIgnoreCase('Monday')){
    startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.MondayStartTime.hour(), bh.MondayStartTime.minute(), bh.MondayStartTime.second(), 0));
    endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.MondayEndTime.hour(), bh.MondayEndTime.minute(), bh.MondayEndTime.second(), 0));
}else if(dayOfWeek.equalsIgnoreCase('TuesDay')){
    startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.TuesdayStartTime.hour(), bh.TuesdayStartTime.minute(), bh.TuesdayStartTime.second(), 0));
    endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.TuesdayEndTime.hour(), bh.TuesdayEndTime.minute(), bh.TuesdayEndTime.second(), 0));
}else if(dayOfWeek.equalsIgnoreCase('Wednesday')){
    startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.WednesdayStartTime.hour(), bh.WednesdayStartTime.minute(), bh.WednesdayStartTime.second(), 0));
    endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.WednesdayEndTime.hour(), bh.WednesdayEndTime.minute(), bh.WednesdayEndTime.second(), 0));
}else if(dayOfWeek.equalsIgnoreCase('Thursday')){
    startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.ThursdayStartTime.hour(), bh.ThursdayStartTime.minute(), bh.ThursdayStartTime.second(), 0));
    endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.ThursdayEndTime.hour(), bh.ThursdayEndTime.minute(), bh.ThursdayEndTime.second(), 0));
}else if(dayOfWeek.equalsIgnoreCase('Friday')){
    startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.FridayStartTime.hour(), bh.FridayStartTime.minute(), bh.FridayStartTime.second(), 0));
    endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.FridayEndTime.hour(), bh.FridayEndTime.minute(), bh.FridayEndTime.second(), 0));
}else if(dayOfWeek.equalsIgnoreCase('Saturday')){
    startTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.SaturdayStartTime.hour(), bh.SaturdayStartTime.minute(), bh.SaturdayStartTime.second(), 0));
    endTime = DateTime.newInstance(Date.today(), Time.newInstance(bh.SaturdayEndTime.hour(), bh.SaturdayEndTime.minute(), bh.SaturdayEndTime.second(), 0));
}

//get the difference between start time and end time
Long difference = BusinessHours.diff(bh.id, starttime, endtime);
Double hours = difference/3600000;
System.debug('Business Hours Diff-' + hours);

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);