Skip to main content

Posts

Showing posts from 2021

Map contains key is case sensitive

 Here is my previous post related to map contains method.  How contains key method works ? Does it allow partial matches or exact matches ? We know map contains key method is case sensitive. if you are create a key using First Name , Last Name and DOB to find unique Account record then system will consider 2 different record for below scenario. 1. First Name - "Capital"  Last Name- "Testing" DOB- 01/01/1989 2. First Name - "CAPITAL" Last Name - "TESTING" DOB- 01/01/1989 The work wound is  map<String,Account> map_Name_Acc = new map<String,Account>(); String sKey = ''; for(Account acc:[SELECT firstname,Lastname,DOB__c from Account limit 10]{      sKey = acc.FirstName+'_'+ acc.LastName+'_'+acc.DOB__c; // this will create duplicate records since key is case sensitive  sKey = acc.FirstName.toLowercase()+'_'+ acc.LastName.toLowercase()+'_'+acc.DOB__c;  } toLowercase() - convert name to lower case, ...

How to split String from the character '*' ?

 This is a bug in split function, it does not work for '*'  or '+'.  We will get runtime error. System.StringException: Invalid regex: Dangling meta character '*' near index 0 *  String sBody = '*   Building Name:Brookwood Gardens  *   Is your unit isolated with barrier systems in place?Yes  *   Does your unit have its own supplies?Yes  *   Does your unit have any additional infection control equipment?HVAC filters: YesUVC lights: Yes'; List<String> strList = sBody.split('*'); // This will throw error. system.debug('strList '+strList); Solutions:  List<String> strList =sBody.split('\\*');  // This will solve.  

How to Identify whether Logged User has particular custom permission assigned ?

Earlier days we used to use Custom Permission in Visualforce page and Formula fields etc. We used to assign custom permission to a permission set. and We used to have one utility method to check whether user has permission set or not. public static  Boolean  doPermissoncheck( String  sPermissionName,  String  sUserId){      Boolean  isPermAssigned;      List < PermissionSetAssignment > lstOfAssignment      = [ SELECT   Id ,  PermissionSet . Name , AssigneeId   FROM   PermissionSetAssignment        WHERE   PermissionSet . Name  =:sPermissionName  AND   AssigneeId =:sUserId];     isPermAssigned = lstOfAssignment.isEmpty()? false : true ;      return  isPermAssigned;          } What If we want to know custom permi...

Apex programming Tricks

Convert List to Map  Lets assume we have list of accounts record and we need to create map of Id to Account record. Traditional  approach is using Iterating over list of Account records and storing in a Map. but the easiest approach is  List < Account > lstofAccount = [ Select  id,name  from   Account   limit   10 ]; system .debug( 'lstofAccount--' +lstofAccount); Map < Id , Account > map_account =  new   Map < Id , Account >(lstofAccount); system .debug( 'map_account--' +map_account); 11 : 00 : 02 : 011   USER_DEBUG  [ 4 ]| DEBUG | map_account--{ 00111000029360 TAAQ = Account :{ Id = 00111000029360 TAAQ ,  Name = Area   Agency   on   Aging   Region   9 ,  RecordTypeId = 012360000005 HcsAAE },  00111000029360 UAAQ = Account :{ Id = 00111000029360 UAAQ ,  Name = AULTMAN   HOSPITAL ,  RecordTypeId = 012360000005 HcsAAE }} Remove Dupl...

How to get record-Id in LWC screen action ?

 If you have created quick action and referred LWC component, you will get "Undefined" as Record Id in ConnectedCallback method, This is the issue I have come across.  connectedCallback() {         console.log( 'connected===============' );         console.log( this .recordId); // This will print undefined. } Solutions - 1: We can wrap LWC component with aura, and pass record Id. < aura:component   implements = "force:lightningQuickActionWithoutHeader, force:hasRecordId,force:appHostable >     < c:updateFiles   recordId="{!v.recordId}" /> </ aura:component > Solution - 2 Use RecordId in component html code. < div   style = "display:none" >      {recordId}   </ div > Component Controller import  {  LightningElement ,api,track }  from   'lwc' ; import  {  CloseActionScreenEvent...

How to display formatted value in LWC component ?

1. Hide all numbers and display only 4 digits of SSN or credit card. Declare a variable in Java Script controller that is being used in LWC component. < lightning-formatted-number   value = { ssn } ></ lightning-formatted-number >    In the java script controller  var num = '123456789234'; // you are getting SSN from apex class ssn = num.slice(-4).padStart(num.length,'*'); Console.log('ssn',ssn); 2. Append . to the end of string. var charString = 'Learning Salesforce'; charString.padEnd(30,'.'); 3. Append Currency Symbol  var testNum = 123445.34; testNum.toLocaleString('en-IN',{style:"currency",currency:'INR'}); testNum.toLocaleString('en-US', { style: 'currency',currency:'USD'}) testNum.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }) testNum.toLocaleString('en-GB', { style: 'currency', currency: 'EUR' }) 4. Display perce...

How to create Chatter Post and Notification from Flow ?

 It is very easy to create chatter post using process builder, there are many cool articles there. https://automationchampion.com/2015/10/03/getting-started-with-process-builder-part-47-mention-a-related-user-in-a-post-to-chatter-action/ This post is in regards to create notification and chatter post using flow. Use Case :We have downstream system which needs opportunity data from salesforce, we have fire and forgot integration approach to integrate both systems. Now on successful response we need to notify users that Opportunity has been pushed and one Id has been created in Downstream system. Chatter Post  1. Create Record Triggered Flow and Add your entry condition 2. Add action element 3. Select Messaging and Action as Post to Chatter 4. Create Text Template Type variable and Add your chatter message,  Even you can tag chatter post to certain users. Sometimes we can see <P><B> in the message, please remove that tag when we view message in text format in F...

How to have Chaining Promises in Lighting Component ?

We may need to call multiple apex function from aura component, each function is dependent to other function. In that case we may have call promise function inside another. Here is the sample code @AuraEnabled public   static   Opportunity  getOpportunity( String  opptyId){          return  [ select  id,name,stagename  from   Opportunity   where  id= 'opptyId' ]  } @AuraEnabled public   static   Boolean  checkFormAttached( String  opptyId){          Boolean  isNotAttached =  true ;          set < String > setOfNames =  new   set < String > { 'Admission Notification' , 'EligibilitySummary' , 'BAForm' };          List < ContentDocumentLink > contLinks = [ SELECT  id,contentd...

How to float text in lightning web component ?

 Use Case - Publish message to all users in salesforce in Home Page or any other record page.                    1. It should be easily configurable.                    2. We can stop displaying message whenever we want from a configuration. It should have Start Time and End Time. Here is my approach to achieve the same. 1. Create a custom Object Salesforce_News__c. custom fields - Start_Date__c , End_Date__c, Active__c and Message__c 2. Create record page for  Salesforce_News__c and add the page to application.  3. Create a permission set "Salesforce News configure"  which will have Create,Edit access to object Salesforce_News__c. 4. Assign permission to user who is going to configure message. 5. Create LWC component  6. Add LWC component to Home Page. Here is LWC component code. < template > < marquee >   < div   class = "slds-clearfix" ...

How to get Salesforce lookup field object id using javascript ?

 When we  add a look input field then salesforce add a hidden field to capture Id. We need to get value from hidden field to get record Id. For example  <apex:inputfield value="{!Custom__c.lookField__c}"  id="lookupfieldId" /> <script type="text/javascript"> function getLookupId() { var fieldId = document.getElementById('{!$Component.lookupfieldId} _lkid ').value; alert('id'+fieldId ); } </script> We can also inspect field to get Id use it  var fieldId =  document.getElementById('{!$Component.pageId:formId:pgBlkId:pgBlkSecId1:lookupfieldId} _lkid ').value;

How to change aura component bundle version ?

Source org and target org should be in compatible version to have successful deployments There are sometime org which will have higher version that might not supported in production. This could happen when a particular instance is in release preview and if we add new component in that org. That new component will have higher salesforce API version. While deploying we might get errors. Changing version in Apex and VF page is easy.  Setup-->Apex Class-->Click Edit next to class -->Version Settings -->Change version How to change version number of Aura Component ? 1. Developer console Query Editor  2. Query -  SELECT Id, DeveloperName, ApiVersion, Description FROM AuraDefinitionBundle WHERE DeveloperName = 'your component Name' 3 . Change version in the results 4. Click Save

How to get list of users added to a queue in Apex ?

 There is no separate objects to store queue name and all queue members. They are mapped to Group and Group Member respectively. Queues are stored as records in the table Group with 'Type' as "Queue".  Select Id from Group where type='Queue' and Name='Queue Name' - This query will return Id of queues. Use above query results (Id)  in the query [Select UserOrGroupId From GroupMember where GroupId =:grpIdset] to fetch all the users or groups which are members of the required queue.  We can also write both query combing  SELECT UserOrGroupId, COUNT( Id ) FROM GroupMember WHERE GroupId IN ( SELECT Id FROM Group WHERE Type = 'Queue' ) GROUP BY UserOrGroupId   For User level SELECT UserOrGroupId, COUNT( Id ) FROM GroupMember WHERE UserOrGroupId =: userfinfo.getUserid() AND GroupId IN ( SELECT Id FROM Group WHERE Type = 'Queue' ) GROUP BY UserOrGroupId  We know Queue can be created for different objects. We have a section names supported objec...