Thursday, May 16, 2019

Activate/Deactive Trigger method using configuration


During development ,we often come across a scenario where we need to activate/deactivate certain method of apex trigger/classes.To do this we can make good use of custom setting.

Step 1:Create custom setting:


Step 2:Create custom setting Record:


We have created custom setting with name Trigger Deactivation in step 1 and in step 2 we have created custom setting record with SendAccountUsingRestApi because we want to acivate/deactivate this method.

Use below code snippet to achieve this:

We have check value of  SendAccountUsingRestApi  custom setting's is Method active field on line no.7.When this flag is active,method is get called otherwise this logic is equivalent to commented out.


You can see below debug logs in two different scenario:

  • When Is Method Active is True

  • When Is Method Active is False





Retrieve Managed Package component in Salesforce ANT tool/Workbench

During deployment we often come across situation where we need to retrieve and deploy component from managed package.

Use below package.xml  to retrieve and deploy component from managed package.


<?xml version="1.0" encoding="UTF-8"?>

<package xmlns="http://soap.sforce.com/2006/04/metadata">

    <types>

     <members>Contact-Contact Layout</members>  

    <members>NameSpacePrefix__ObjectName__c-NameSpacePrefix__Price List Layout</members>

      <name>Layout</name>

    </types>

 <version>43.0</version>

</package>

Example:

Namespace Prefix is : Apptus_config2 Object API:PriceList__c Layout Name : Price List Layout
then use below package.xml to retrieve and deploy component from managed package.
<?xml version="1.0" encoding="UTF-8"?> <package xmlns="http://soap.sforce.com/2006/04/metadata">     <types>     <members>Contact-Contact Layout</members>   <members>Apptus_config2__PriceList__c-Apptus_config2__Price List Layout</members>       <name>Layout</name>     </types> <version>43.0</version> </package>

Filter SOQL Queries Using WITH SECURITY_ENFORCED (Beta)

Apex generally runs in system context; that is, the current user's permissions, field-level security, and sharing rules aren’t taken into account during code execution. Although performing field- and object-level security checks was possible in earlier releases, this clause technical complexity in query operations. 

Using WITH SECURITY_ENFORCED clause enable checking for field- and object-level security permissions on SOQL queries, including subqueries and cross-object relationships.

Example 1

If field access for either LastName or Description is hidden, this query throws an exception indicating insufficient permissions.

   List act1 = [SELECT Id, (SELECT LastName FROM Contacts),

                 (SELECT Description FROM Opportunities)

                 FROM Account WITH SECURITY_ENFORCED]

Example 2

If field access for Website is hidden, this query throws an exception indicating insufficient permissions.

   List<Account> act2 = [SELECT Id, parent.Name, parent.Website 
                        FROM Account WITH SECURITY_ENFORCED]

Example 3

If field access for Type is hidden, this aggregate function query throws an exception indicating insufficient permissions.

    List<AggregateResult> agr1 = [SELECT GROUPING(Type) 
                                 FROM Opportunity WITH SECURITY_ENFORCED 
                                 GROUP BY Type]
Note

The WITH SECURITY_ENFORCED clause is only available in Apex.
Using WITH SECURITY_ENFORCED in Apex classes or triggers with an API version earlier than 45.0 is
 not recommended.