Posts

Showing posts from May, 2021

Salesforce Fact #92 | testSetup after testMethod

Image
As part of test class best practice, it is advised to write the testSetup method as the first method of test class.  But even if we write it after any testMethod, the test class logic will work as usual i.e first the testsetup methods will be invoked and after that the testMethods will be invoked.  Attached are the screenshots.  

Salesforce Fact #91 | Illegal forward reference error

Image
Salesforce throws illegal forward reference error when a variable is referenced in code before it is declared, similar to Java. Attached are the screenshots.

Salesforce Fact #90 | ISCHANGED()

ISCHANGED() function cannot be used in formula fields.  It is available in: Assignment rules,   Validation rules, Field updates, Workflow rules 2nd criteria, formula criteria in process builder. Collected from:  https://help.salesforce.com/articleView?id=sf.customize_functions_i_z.htm&type=5

Salesforce Fact #89 | IN is =

Image
We can use the equal to operator in place of IN operator in SOQL query. The result would still be the same. Attached is the screenshot.  

Salesforce Fact #88 | Standard validations in WF field update

Image
Custom validations do not work in case of workflow field update. But standard validation rules are respected. In this example, we are setting the account name field which is a required field to a blank value from workflow field update and we can see the standard validation rule getting invoked. Note: this scenario is only for demonstration purpose. Attached are the screenshots.  

Salesforce Fact #87 | At least one test method

Image
We need to add at least one test method for our test class to run. We cannot add only testsetup method. The class would get saved but will throw runtime error. This could be helpful in case when we have setup the required data but not yet decided on what to test exactly. Attached are the screenshots.        

Salesforce Fact #86 | Check first, then make the SOQL query

Image
Unlike DML, if we fetch data using SOQL against an empty list or set in filter condition, it is still counted towards as 1 SOQL in governor limit. And also the resultant list is empty. Therefore, before making the query we can check whether the list or set is empty or not and then query accordingly. This would save 1 SOQL query count in governor limit. Attached is the screenshot.

Salesforce Fact #85 | Record Id in quick action flow

Image
Just like we can pass record Id in a flow which is present in record detail page, the same way we can pass record Id in a flow launched as a quick action. For this one also, we need to define a text variable with the name 'recordId' and select the checkbox 'available for input'. Attached are the screenshots.  

Salesforce Fact #84 | Change set unavailability

A change set becomes unavailable in any of the below situations: The change set expires. The change set is deleted from the source org. After the change set is deployed from a source sandbox to a target org, the source sandbox is deleted or refreshed Collected from:  https://help.salesforce.com/articleView?id=sf.changesets_inbound_view_unavailable.htm&type=5

Salesforce Fact #83 | Prior value in validation rule

Image
PRIORVALUE() function also takes into consideration the value while creating a record. So we need to be careful while working with validation rule. In this example, the validation rule checks whether the prior value of account rating is 'Hot'. Attached are the screenshots.  

Salesforce Fact #82 | DML on empty list

Image
DML statement on empty List is not counted towards governor limit. Attached is the screenshot.

Salesforce Fact #81 | Configure quick action from edit page

Image
With dynamic action enabled, we can specify the quick actions right from the edit page option using app builder, no need to go to the page layout in object manager. Attached are the screenshots.

Salesforce Fact #80 | Another beauty of Safe navigation

Image
Safe navigation can be used in dynamic SOQL. Attached is the screenshot.

Salesforce Fact #79 | Date comparison in Flow

Image
Suppose there is a use case that we need to fetch the account records created in last 7 days inside lightning flow. Now we need to put our filter considering CreatedDate field. Now, the createddate field is of type datetime. However, it can be compared against a date value if we use a formula value. For this scenario, we would create a date type formula variable which will store: {!$Flow.CurrentDate}-7 as we need to fetch last 7 days records. This value can be used in the filter condition of GetRecords element and records can be retrieved successfully. Attached are the screenshots.    

Salesforce Fact #78 | Count the data

Image
We can use countQuery() function of Database class instead of aggregate query to get the count of records. This is basically dynamic SOQL in comparison with conventional static approach like [SELECT Count() FROM Account]; Collected from: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_database.htm Attached is the screenshot.  

Salesforce Fact #77 | Account & Contact together

Image
Creating an account along with a related contact in a single statement. This is super cool. :) Attached is the screenshot.  

Salesforce Fact #76 | Invoke Workflow during update only

Image
All the workflow evaluation criteria have 'Created' in common i.e. they are invoked during record creation. But what if we want to call the workflow only during specific change in the record. We can use the function PRIORVALUE in workflow only for the evaluation criteria type 'Created and every time it's edited'. And using PRIORVALUE we can make it work only during the specific change. In this example, the workflow is called only when the account rating is changed from 'Hot' to 'Cold'. Attached is the screenshot.

Salesforce Fact #75 | % in SOQL

Image
We use escape character(\) in SOQL to handle special characters in query. If we need to check for the % sign itself in the query, we need to specify the same along with escape character. In this example, we are fetching the account records which have two consecutive % symbol anywhere in the Name field. So we need to escape each of the % symbol which are to be considered as part of the text. Attached are the screenshots. .

Salesforce Fact #74 | Reference check

Image
We can refer a field in two ways. one way is we can directly refer the field name using dot operator. And other way is referring the field dynamically. However, we should follow the approach to directly refer the field as it is counted as a reference to the field, whereas in dynamic approach it is not counted as a reference. It is especially helpful when we are tracking in which places exactly the field has been been used. Attached is the screenshot.

Salesforce Fact #73 | Save your work

Image
We can use savepoints to return to a previous state in the transaction.  In this example, we are inserting list of Accounts and Contacts in the try block. For some reason if contact insert fails, we want to rollback the account inserts also. For this we can use savepoint. Attached is the screenshot.

Salesforce Fact #72 | Contact Name in trigger

Image
Contact Name field is directly not accessible in trigger. We need to fetch it using SOQL to get the data. EDIT: Another option is we can access the firstname and lastname fields and concatenate. This would save from making an SOQL query. Attached are the screenshots.          

Salesforce Fact #71 | Beware of NULL in DML

Image
Whenever we are doing DML on bulk records using List, if any of the element is NULL in the list the entire operation will fail. It even cannot be partially completed using the Database methods and setting the allOrNone flag to false. So we need to be careful. Attached are the screenshots.

Salesforce Fact #70 | html input in aura

Image
In Aura framework, whenever we use input html then to get the value from the input field we need to use getElement() function. If we associate the value with an aura attribute it won't track the changed value. Attached are the screenshots.  

Salesforce Fact #69 | ES6 Spread Operator

Image
In LWC, whenever we need to modify the array and render the same in UI, we need to use ES6 spread notation. Simple array.push() won't work. Attached are the screenshots.

Salesforce Fact #68 | DML+Safe navigation+ternary in one statement

Image
We can use the ternary operator, safe navigation and DML in a single statement. Attached is the screenshot.  

Salesforce Fact #67 | contains NULL

Image
Just like we use contains function to check whether a list contains a particular value, we can use the same to find NULL values also. Attached is the screenshot.  

Salesforce Fact #66 | Flow output in lightning component

Image
We can access the output value from a flow in lightning component. For this, we need to declare the variable in the flow as 'available for output'. In this example, there is a simple auto-launched flow which is launched from lightning component, increments the input value by 1 and the output value is accessed in lightning component. Attached are the screenshots.

Salesforce Fact #65 | trigger.new in SOQL

Image
We can use trigger.new and trigger.old in SOQL bind variable to find records based on the matching Id. Although trigger.new and trigger.old are list of SObject records, when we are matching them with recordId field it will consider the corresponding Id field with the same column name. In this example, we are fetching the related contacts of all the account records present in trigger.new . Collected from: One of the courses by David Liu in Pluralsight Attached is the screenshot. 

Salesforce Fact #64 | trigger old and new values

Image
Attached are the different values of new and old trigger list and map for all the events. Hope it will be handy and helpful for the interview.  

Salesforce Fact #63 | test class tip

Image
It is always a best practice not to use org data in test class. Instead we should create our own data for testing. However, if we need to get a particular record during our testing we can get that using SOQL. But the records will be like read only. Any changes made to the record in the test class would not be reflected in the original record. In this example, we are fetching one account named 'test' in the test class. Now, although we are changing the name of the account and also updating the same, also the assert is successful, the original record is unchanged. Attached are the screenshots.