Wednesday 27 May 2015

How to Convert Salesforce 18 digit ID to 15 digit ID in Apex


              You can convert your 18 digit Salesforce ID to 15 digit using the String method. The below example will show how to use,

  1. String str = '0019000001O73UCAAZ';
  2. String convertedStr = str.subString(015);
  3. System.debug('15 digit ID:'+convertedStr);


However, Salesforce recommends that you use the 18-character ID.

Reference: Field Types

How to Convert Salesforce 15 digit ID to 18 digit ID in Apex

          Here is the tricky way to covert your 15 digit ID value to 18 digit in salesforce.

Consider the below example,

  1. String str = '0019000001O73UC';
  2. Id convertedId = str;
  3. System.debug('Converted ID value'+convertedId);

You can just assign your 15 character string value to the ID datatype. It will automatically convert this as 18 Digit.

Note: This one only applicable for Salesforce Id's not for other id such as External Id. If you try to assign any other values to Id data type, then you will receive an System.StringException: Invalid id Exception.

Tuesday 12 May 2015

Failed to create createContainerMember for containerId=undefined: null is not a valid containerId.


           Some of the user may face this issue while using Developer Console in salesforce. 

"Failed to create createContainerMember for containerId=undefined: null is not a valid containerId."

         Follow the below steps in order to solve this issue, 

  1. Go to Developer Console -> Work Space -> New Workspace
  2. Create a new workspace
  3. Save classes/triggers 
 
Knowledge Article Reference:
 
https://help.salesforce.com/apex/HTViewSolution?id=000204953&language=en_US




Tuesday 5 May 2015

How to test your Permission Set logic in Apex Test Class


          Here is the simple example to test your permission set logic in your test class.  The below class will query the Permission set named as "AccountRating" and Checking if the logged in user available in that permission set. If yes, i am creating account record.

Apex Class

  1. public class PermissionSetClass
  2. {
  3.     public PermissionSetClass()
  4.     {
  5.         List<PermissionSetAssignment> perAssignments = [SELECT AssigneeId, PermissionSet.Name FROM PermissionSetAssignment WHERE   PermissionSet.Name = 'AccountRating' AND AssigneeId=:UserInfo.getUserId()];
  6.         if(!perAssignments.isEmpty())
  7.         {
  8.             Account acc = new Account();
  9.             acc.Name = 'Test Account';
  10.             insert acc;
  11.         }    
  12.     }
  13. }


        To write a test class for the above logic the following thing is needed. 

Step 1: Insert a test user record.
Step 2: Query your Permission Set information that you have used in class.
Step 3: Assign the inserted user to the above queried Permission Set. For this PermissionSetAssignment object is used.
Step 4:  Initialize your class/method by running as the above inserted user. 

Test Class

  1. @isTest
  2. private class TestPermissionSetClass
  3. {
  4.     static testMethod void checkPermissionSet()
  5.     {
  6.         Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
  7.        
  8.         User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
  9.                           EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
  10.                           LocaleSidKey='en_US', ProfileId = p.Id,
  11.                           TimeZoneSidKey='America/Los_Angeles',     UserName='testpermissionsetuser@testorg.com');
  12.         insert u;
  13.        
  14.         // Query your permission set name from Organization that your want to test.
  15.         PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'AccountRating'];
  16.        
  17.         // Assign the above inserted user for the above Permission Set.
  18.         PermissionSetAssignment psa = new PermissionSetAssignment();
  19.         psa.AssigneeId = u.Id;
  20.         psa.PermissionSetId = ps.Id;
  21.         insert psa;
  22.        
  23.         // Run your code with inserted user.
  24.         System.runAs(u)
  25.         {
  26.             PermissionSetClass pr = new PermissionSetClass();
  27.         }
  28.     }
  29.    
  30. }

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...