Wednesday, October 28, 2020

How to set CreatedDate field value for Test Class Records in Salesforce

We can now set the Created date field value for the test records in Salesforce test classes using setCreatedDate(Id recordId, Datetime createdDatetime) method. 

Note: Both recordId and DateTime parameters are required.

Example:

@isTest 
private class SetCreatedDateTest {
    static testMethod void testSetCreatedDate() {
        Account a = new Account(name='myAccount');
        insert a;
        Test.setCreatedDate(a.Id, DateTime.newInstance(2020,12,12));
        Test.startTest();
        Account myAccount = [SELECT Id, Name, CreatedDate FROM Account 
                             WHERE Name ='myAccount' limit 1];
        System.assertEquals(myAccount.CreatedDate, DateTime.newInstance(2020,12,12));
        Test.stopTest();
    }
}


Thursday, October 22, 2020

Auto Add Fields to Custom Report Types in salesforce

 From Winter 21 onwards we can auto-add, custom fields that you add to a Salesforce object are added automatically to all the custom report types based on that object. When you create a report from the custom report type, all the custom fields are right there for you to add to your report.

How to Enable:

Path:

From Setup, enter Reports & Dashboard Settings in the Quick Find box, and then Reports & Dashboard Settings

Enable the Auto add new custom fields to custom report type layouts



Monday, October 19, 2020

How to extract specific tags information from String using Apex in Salesforce

we can use the regular expression and utilize the Apex Pattern and Matcher classes.

Below is the generic regular expression to get the specific tags information from the HTML string.

Replace your tag and use it for your requirement.

(?i)<test([^>]+)>(.+?)</test>
Ex: 
(?i)<div([^>]+)>(.+?)</div>
Demo:
from the below string, I am extracting the a (anchor) tag information.

Sample Html String:

"We at <a href="https://www.linkedin.com/company/etg/" rel="noopener noreferrer" target="_blank">ETS, Inc.</a> are eager to share with you that we are a Strategic Solution Partner for the <a href="https://www.linkedin.com/company/commercecloud-demandware/" rel="noopener noreferrer" target="_blank">Salesforce Commerce Cloud</a>.<br>If you need any assistance with your <a href="https://www.linkedin.com/feed/hashtag/?keywords=ecommerce&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6721841224095014912" rel="noopener noreferrer" target="_blank">#eCommerce</a> systems, let us know how we can help!"

From the above string, I am getting the anchor tag information.

Sample Code:
String strMessage = 'We at <a href="https://www.linkedin.com/company/etg/" rel="noopener noreferrer" target="_blank">ETS, Inc.</a> are eager to share with you that we are a Strategic Solution Partner for the <a href="https://www.linkedin.com/company/commercecloud-demandware/" rel="noopener noreferrer" target="_blank">Salesforce Commerce Cloud</a>.<br>If you need any assistance with your <a href="https://www.linkedin.com/feed/hashtag/?keywords=ecommerce&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6721841224095014912" rel="noopener noreferrer" target="_blank">#eCommerce</a> systems, let us know how we can help!';

Pattern objPattren = Pattern.compile('(?i)<a([^>]+)>(.+?)</a>');
Matcher objMatcher = objPattren.matcher(strMessage);

while (objMatcher.find()) {
    String groupValue = objMatcher.group();
    System.debug('groupValue => '+groupValue);
}
Output:



Thursday, October 8, 2020

How to Query the Lookup Filter Information Using SOQL in Salesforce

 To Query the lookup filter information we can use the LookupFilter object.

Note: Select the Tooling API option when you querying the data

Example:

Select id, Active, IsOptional, DeveloperName, SourceObject, TargetEntityDefinition.DeveloperName, SourceFieldDefinitionId, SourceFieldDefinition.DeveloperName From lookupFilter
Result:


Sunday, October 4, 2020

How to check current user has a Custom Permission in Salesforce Using Apex?

To check the custom permission using Apex we can use the FeatureManagement class.

Demo:

Crate the custom permission


Sample Code:

Boolean bHasPermission = FeatureManagement.checkPermission('Test_Custom_Permission');
System.debug('has permission => '+bHasPermission);

Result: