Friday, 1 May 2020

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 the Quick Find box and then select Queues. From the Queues setup page, select New. Then, create a queue and assign Task as the supported object.


Reference:



Prepopulate or Clone field values using Custom Button or Custom Link in Lightning Salesforce

Salesforce announced to create custom buttons and links that pass default field values to a new record beginning from Spring'20 release.

To construct a custom button or link that launches a new record with prepopulated field values, use this sample formula:

  1. /lightning/o/Account/new?defaultFieldValues=
  2.     Name={!URLENCODE(Account.Name)},
  3.     OwnerId={!Account.OwnerId},
  4.     AccountNumber={!Account.AccountNumber},
  5.     NumberOfEmployees=35000,
  6.     CustomCheckbox__c={!IF(Account.SomeCheckbox__ctruefalse)}


Note:
The URLENCODE function works only when creating custom buttons and links. You can’t use it for custom fields.

Reference:

Spring 20 Release Notes 1

Spring 20 Release Notes 2

Tuesday, 26 March 2019

Create Lightning Web Components - Basic - Key Points

You can learn creating a simple lightning web component from the below trailhead module,

I wanted to highlight the key points from the above modules,
  • You can use if:false and if:true conditional directives within your template to determine which visual elements are rendered. Similar to rendered in the visualforce page or aura:if in the aura components. 
  • Lightning web components use common JavaScript ECMAScript 8 methods and syntax.
  • The JavaScript file for a Lightning web component must include at least this code, where MyComponent is the name you assign your component class.

  1. import { LightningElement } from 'lwc';
  2. export default class MyComponent extends LightningElement {
  3. }

  • The export statement defines a class that extends the LightningElement class. As a best practice, the name of the class usually matches the file name of the JavaScript class, but it’s not a requirement.
  • Decorators are often used in JavaScript to extend the behavior of a class, property, getter, setter, or method.
    • @api: Marks a property as public for use in your template or other components.
    • @track: Marks a property for internal monitoring. A template or function using this property forces a component to rerender when the property’s value changes. Use this to store values locally, especially as a user interacts with your component.
    • @wire: Gives you a way to get and bind data. This implementation simplifies getting data from a Salesforce org
  • But, be aware, apply only one Lightning Web Component decorator to a property at a time. For example, a property can’t have @api (public reactive) and @track (private reactive) decorators at the same time.
  • Respond to any of these lifecycle events using callback methods. For example, the connectedCallback() is invoked when a component is inserted into the DOM. The disconnectedCallback() is invoked when a component is removed from the DOM.

What is Lightning Web Component Playground?

Salesforce introduced a interactive code editor to test the Lightning Web Components. By using Playground you can Write JavaScript, HTML, and CSS code, and preview the output as you develop.

Playground documentation:
https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.install_playground


Playground Code Editor URL:
https://developer.salesforce.com/docs/component-library/tools/playground

What is Lightning Web Component in Salesforce?

Lightning Web Components is a new programming model for building Lightning components. 

The Lightning Web Components programming model is focused on both the developer and user experience. Because we’ve opened the door to existing technologies, you use the skill you’ve developed outside of Salesforce to build more performant Lightning web components. All of this is available to you without giving up what you’ve already accomplished with Aura components.

It uses web standards breakthroughs, can coexist and interoperate with the Aura programming model, and delivers unparalleled performance.

To create and develop Lightning Web Components and use their powerful features and performance benefits, you need to set up Salesforce DX.

Lightning Web Components consist of three files,

.html file --> Html code for designing.
.js file     --> Javascript logic.
.css file   --> External style. (Not mandatory)

Reference:
Salesforce DX
Basic Lightning Web Component

Wednesday, 9 May 2018

How to fetch field permission details for all profiles in SOQL query

Here is the way to fetch field permission details for all profiles in SOQL query by using FieldPermissions object.

Query specific field permission for all Profiles:

  1. SELECT Profile.Name FROM PermissionSet WHERE IsOwnedByProfile = TRUE AND Id IN (SELECT ParentId FROM FieldPermissions WHERE FIELD ='Payment__c.Firm__c' AND SobjectType = 'Payment__c' AND PermissionsRead = TRUE AND PermissionsEdit = FALSE) ORDER BY Profile.Name

PermissionsRead --> View permission for the field.
PermissionsEdit --> Edit permission for the field.

The below approach also works but the profile name will be displayed as [Object Object] in Query Editor,


  1. SELECT Parent.Profile.Name FROM FieldPermissions WHERE FIELD = 'Payment__c.Firm__c'  AND Parent.IsOwnedByProfile = TRUE AND SobjectType ='Payment__c' AND PermissionsRead = TRUE AND PermissionsEdit = FALSE ORDER BY Profile.Name

Reference:

Field Permission

Object Permission

How to fetch object permission for all profiles in SOQL query

You may be in a situation to verify object permission details for all or specific profile. So you may check object details from UI one by one by navigating each profile, and this will be time-consuming. Instead, you can fetch details by querying ObjectPermission. Here is the example,


Query specific object permission for all Profiles:

  1. SELECT Profile.Name FROM PermissionSet WHERE IsOwnedByProfile = TRUE AND Id IN (SELECT ParentId FROM ObjectPermissions WHERE PermissionsRead = TRUE AND SObjectType = 'CustomObject__c') ORDER BY Profile.Name

You can check all object level permission in the query. Such as,
PermissionsRead 
PermissionsCreate
PermissionsEdit
PermissionsDelete
PermissionsViewAllRecords
PermissionsModifyAllRecords


The below approach also works but the profile name will be displayed as [Object Object] in Query Editor,

  1. SELECT Parent.Profile.Name FROM ObjectPermissions WHERE Parent.IsOwnedByProfile = TRUE AND SObjectType = 'CustomObject__c'

Reference:

Stackexchange

Object Permission

Friday, 4 May 2018

Property 'userLicense' not valid in version 38.0 Salesforce

You will get below error message when you try to deploy PermissionSet with Package Version of 38.0

Property 'userLicense' not valid in version 38.0 Salesforce

Solution:

If you open your permission set metadata file you will see tag like below,
    <userLicense>Salesforce</userLicense>

From API Version 38.0 or later, then you have to change as license only.
    <license>Salesforce</license>


As per Salesforce documentation,

userLicense tag is Deprecated. The user license for the permission set. A user license determines the baseline of features that the user can access. Every user must have exactly one user license. Available up to API version 37.0. In API version 38.0 and later, use license.

Reference:
Permission Set Metadata API

How to Check if the Current/Running User has a Custom Permission in Salesforce?

FeatureManagement:

For this requirement we can use FeatureManagement.checkPermission method to identify Current/Running User has a Custom Permission. This method available from Winter 18 onwards.

checkPermission(apiName)
Checks whether a custom permission is enabled.

   Signature
      public static Boolean checkPermission(String apiName)
   Parameters
      apiName
   Type: String

Example:
  1. if(FeatureManagement.checkPermission('Chat_CustomPermission_Access')){
  2.     // Perform some logics.
  3. }

Reference:
FeatureManagement

Wednesday, 28 February 2018

One Universal Schedule Class to Execute All Batch Classes In Salesforce

Are you creating schedule class for each batch class if you need to schedule that?

If yes, stop doing like that and start creating a universal (common scheduler class) like below and use it instead of creating multiple schedules.

Step 1: 
Create a universal scheduler class

  1. public class UniversalScheduler implements Schedulable{
  2.   public Database.Batchable<SObject> batchClassName{get;set;}
  3.   public Integer batchScopeSize{get;set;} {batchScopeSize = 200;}
  4.   public void execute(SchedulableContext sc) {
  5.      Database.executebatch(batchClass, batchSize);
  6.     }
  7. }


Step 2:
Let say, You have two batch classes and you want to schedule, then schedule batch class like below using UniversalScheduler class

Batch Class 1:
  1. AccountBatchProcess accBatch = new AccountBatchProcess(); // Batch Class Name
  2. UniversalScheduler scheduler = new UniversalScheduler();
  3. scheduler.batchClass = accBatch;
  4. scheduler.batchSize = 100;
  5. String sch = '0 45 0/1 1/1 * ? *';
  6. System.schedule('Account Batch Process Scheduler', sch, scheduler);


Batch Class 2:
  1. ContactBatchProcess cntBatch = new ContactBatchProcess(); // Batch Class Name
  2. UniversalScheduler scheduler = new UniversalScheduler();
  3. scheduler.batchClass = cntBatch;
  4. scheduler.batchSize = 500;
  5. String sch = '0 45 0/1 1/1 * ? *';
  6. System.schedule('Contact Batch Process Scheduler', sch, scheduler);



What is aura:attribute in lightning components?

What is aura:attribute?
Attributes on components are like instance variables in objects. They’re a way to save values that change, and a way to name those value placeholders. 

Example:
Created Component called AttributeComponent:

  1. <aura:component >
  2.     <aura:attribute name="ProjectName" type="String" required="true"/>
  3.     <aura:attribute name="DefaultView" type="String" default="This is default String"/>
  4.     <aura:attribute name="AccountInfo" type="Account" />
  5.  
  6.     <lightning:card title="Grouped Item">
  7.         <p> Project Name: {!v.ProjectName}</p>
  8.         <p> Default Value: {!v.DefaultView}</p>
  9.         <p> Account Name: {!v.AccountInfo.Name}</p>
  10.         <p> Account Number: {!v.AccountInfo.AccountNumber}</p>
  11.     </lightning:card>
  12.    
  13. </aura:component>

I have created 3 attributes and displayed in the above example.
First Attribute called "ProjectName" and type "String" and required as true.
Second Attribute called "DefaultView" and type "String" with Default value.
Third Attribute called "AccountInfo" and type "Account" (Standard Object).

Created another Component called PassAttributeValue:

  1. <aura:component >
  2.     <c:AttributeComponent ProjectName="My Project" AccountInfo="{'sobjectType': 'Account', 'Name' : 'Test Account', 'AccountNumber' : '322342'}"/>
  3. </aura:component>

In this component, I am passing/assigning values to the attributes. So if I run PassAttributeValue component via lightning app, then output will be as below,



Attribute Data Types:
aura:attribute type supports the following data types,
  • Primitives data types, such as Boolean, Date, DateTime, Decimal, Double, Integer, Long, or String. The usual suspects in any programming language.
  • Standard and custom Salesforce objects, such as Account or MyCustomObject__c.
  • Collections, such as List, Map, and Set.
  • Custom Apex classes.
  • Framework-specific types, such as Aura.Component, or Aura.Component[]. These are more advanced than we’ll get to in this module, but you should know they exist.

Reference:

What is lightning:card in Salesforce?

Lightning:card tag (lightning component) is similar to apex:outputpanel (Visualforce page). It act as a container. 

A lightning:card is used to apply a stylized container around a grouping of information. The information could be a single item or a group of items such as a related list.

A lightning:card contains a title, body, and footer. To style the card body, use the Lightning Design System helper classes.

  1. <aura:component >
  2.     <lightning:card title="Container" iconName="standard:account">
  3.      This is for Testing.
  4.     </lightning:card>
  5. </aura:component>

title attribute is required to use lightning:card.
iconName You can set icon name from this link.

Reference:

Tuesday, 27 February 2018

Is My Domain Required to Use Lightning Components in Salesforce?

Is My Domain Required to Use Lightning Components in Salesforce?

YES

Reason:

To use Lightning Components, your organization needs to have a custom domain configured using My Domain.

So what the heck is a custom domain, and why do you need to have one to use Lightning Components? First of all, a custom domain is a way to have your very own Salesforce server…sort of. It’s a way for you to use Salesforce from your own, customized URL, rather than a generic Salesforce instance URL. That is, once you have a custom domain, you’ll use Salesforce at https://yourDomain.my.salesforce.com/, which is reserved exclusively for your org’s use. Let other folks continue to use and share https://na30.salesforce.com/. Your custom domain puts you on your own private Internet island.

Setting up a custom domain has a lot of benefits besides just getting you a cool URL. Among other things, a custom domain lets you:
  • Highlight your business identity with your unique domain URL
  • Brand your login screen and customize right-frame content
  • Block or redirect page requests that don’t use the new domain name
  • Work in multiple Salesforce orgs at the same time
  • Set custom login policy to determine how users are authenticated
  • Let users log in using a social account, like Google and Facebook, from the login page
  • Allow users to log in once to access external services

Reference:
https://trailhead.salesforce.com/modules/lex_dev_lc_basics/units/lex_dev_lc_basics_prereqs

Thursday, 28 December 2017

How to enable debug for site user in Salesforce

1. Set a browser cookie:

  • Open your Force.com site URL in your browser tab. (This is important - You won't see debug log until Setting cookie without opening your site)
  • Open the Chrome DevTools Console by pressing Ctrl+Shift+J (Cmd+Opt+J on macOS).
  • Execute a command to set the cookie. (Copy and paste the below command and click Enter)

          If you use a .force.com domain, use this command.
         document.cookie="debug_logs=debug_logs;domain=.force.com";

         If you use a custom domain (for example, yourCustomDomain.com), use this command.
         document.cookie="debug_logs=debug_logs;domain=yourCustomDomain.com";


2. Find the name of your site’s guest user:
  • From Setup, enter Sites in the Quick Find box, then select Sites.
  • Select your site from the Site Label column.
  • Select Public Access Settings | View Users.
3. Set a user-based trace flag on the guest user.
  • From Setup, enter Debug Logs in the Quick Find box, then click Debug Logs.
  • Click New.
  • Set the traced entity type to User.
  • Open the lookup for the Traced Entity Name field, and then find and select your guest user.
  • Assign a debug level to your trace flag.
  • Click Save.

Reference:

Tuesday, 6 June 2017

How to use Limits Apex Methods to avoid Hitting Governor Limits

Many of us facing governor limit error in trigger/classes/test classes. Few of the governor limit errors as follow,

1. Too many SOQL queries: 101
2. Too many dml rows 10001
3. too many query rows 50001.

This article is helpful to verify the Governor Limit by checking limit methods to avoid hitting the governor limit.

Here is the snippet,
  1. System.assert((Limits.getDMLRows() * 100 / Limits.getLimitDMLRows()) < 50);
  2. System.assert((Limits.getDMLStatements() * 100 / Limits.getLimitDMLStatements()) < 50);
  3. System.assert((Limits.getAggregateQueries() * 100 / Limits.getLimitAggregateQueries()) < 50);
  4. System.assert((Limits.getQueries() * 100 / Limits.getLimitQueries()) < 50);
  5. System.assert((Limits.getQueryRows() * 100 / Limits.getLimitQueryRows()) < 50);

Lets take this example,
  1. System.assert((Limits.getQueries() * 100 / Limits.getLimitQueries()) < 50);

In the synchronous transaction, the number of query limit is 100. If it exceeds, then your will get an error like Too many SOQL queries: 101.

  1. Account acc = [SELECT Id FROM Account Limit 1];
  2. System.debug('## Queries Used'+Limits.getQueries());
  3. System.debug('## Total Allowed Queries'+Limits.getLimitQueries());
  4. System.assert((Limits.getQueries() * 100 / Limits.getLimitQueries()) < 50);

Just execute the above snippet in the developer console. We have used one query

## Queries Used --> 1
## Total Allowed Queries --> 100
So as per the assert statement,

(1 * 100 / 100) < 50 --> So the transaction is safe without hitting SOQL limit. Because of query limit is less than 50 out of 100.

References:

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...