About

all about SFDC

Friday 7 June 2019

Import existing Salesforce project in Visual Studio Code

This blog post describes how to import an existing Salesforce projects in Visual Studio Code. Primarily this process will consists 3 major steps such as creating project, authorizing an org and importing all existing components. Before creating project we should have installed salesforce extensions pack for Visual Studio code and also Salesforce CLI and JDK 8 is must in order to work these extensions. The Visual Studio Code can run SFDX commands from its terminal.






After installing the extension pack. The first step is to create project.Always Choose the option to create a project with manifest because when you do create a project with manifest it will populate some files for you such as package.xml with apex classes and triggers and Ltng Components.





Once created the project in local we need to authorize/connect Salesforce Org to the project. To do this type Ctrl+Shift+P  and choose SFDX:Authorize an Org command from command palette and choose instance url either for Sandbox or dev org. Leave the org alias empty and click enter and this action will open the Salesforce login screen in browser and once done





After successful authorization next step is to import all metadata to our local/Visual Studio Code. Check the package.xml file in project tree and then right click on it and choose the command SFDX: Retrieve Source in Manifest from Org and this action can also be done from command palette when package.xml file is open. This is the only action, even just in case we want to import newly created metadata components  by other developers during development  from org  . The command SFDX:Retrieve Source from Org will only refresh the existing meta data components from updated versions in Org and it won't import newly created metadata components such as classes etc to local.






Note: While setting up JDK 8, the JAVA_HOME system variable is necessary.

Resources:
Quick Start: Visual Studio Code for Salesforce Development

Org Development Model

Salesforce Extension Pack

Cheers!!

Wednesday 5 June 2019

Launch/invoke flow from process builder and update child records with Flow

In this post for easier grasping I took the basic example of updating child records when parent updated. To achieve this I took a two checkbox custom fields "Read Only Account" on Account object and another custom field "Read Only Contact" on contact object. So when ever the account's field "Read Only Account" is set to true/checked then the related all contacts' field "Read Only Contact" will get updated to true/checked.

The contact update part is implemented in Flow builder and this flow is invoked from process builder which is fired/invoked when account is updated.



Process builder to invoke the Flow when "Read Only Account" field on Account is checked.



Make sure the "accId" flow variable is set to "allow input" from out side the flow when creating flow.



Created an Auto Launched flow with 3 different actions including getting/query contacts and iterating them, change field "Read Only Contact" to checked/true and then updating them in the final stage after iterating all items in loop. Ensure the loop is closed always after certain operations or assignments completed and then invoke other actions such as updates after last item finished in loop.



The break down of each stage is mentioned  in the below pictures and I skipped few steps such as creating flow variables and choosing data types. However, in this flow we use text, list of contacts and contact data types mostly. It makes sense for us which data type should we choose, when we explore the flow builder more .


In the above stage make sure "accId" flow variable is set take input from outside the flow. Specify fields to query and create a list of contacts variable to hold query returned records.



Iterate contacts and holding each record in a loop variable of contact type


 Setting field "Read Only Contact" on contact to True and adding it to a list. At this point avoid simply adding loop variable to a list for update, this doesn't work. We need to capture the fields need to update into another variable in this case variable {!tempConToUpdate} and then add this variable to a list. Never forget the Id field is required in an update call.




Updating contacts by passing previously populated list from assignment stage.


Cheers!!!

Tuesday 4 June 2019

Embed Lightning component in Flows and passing data between flow and Ltng Component

This blog post explains how to embed/place/use lightning component in Screen Flow. The Lightning component which will be used in Flow must implement interface lightning:availableForFlowScreens. Let's take a basic example with a contact insertion using screen flow. In this example, the lightning component is used a account look up search field.

Screen Flow preview


To demonstrate this approach in a simple way I took lightning data services tag lightning:recordEditForm and to avoid complexity. The attributes specified in design:component will be accessible from flow. When these design attributes are mapped/connected to flow variables, the lightning component even can restore the field values from paused flows.







Once created the Lightning component then create a Screen flow to insert a Contact. Drag and drop the Lightning component created in previous step. Provide mapping for lightning component variables which are from design attribute section, to the Flow variables. The input and output variables mapping in flow will ensures the Lightning component loads the saved data when flow paused. The flow variable {!acRecId} in this case holds the account Id selected in Lightning component and makes it available for Insert contact stage in flow even user comes back to paused flows.

Add other text fields such as email and phone of contact to the flow screen and provide mapping in the final contact insert stage as mentioned below screenshots.



Adding Lightning component and other text fields in screen flow.


Drag and drop the create records action from flow actions and provide mapping for inserting contact record from flow input fields and variables.





Cheers!!!


Sunday 2 June 2019

Navigate users to record detail after finishing Screen Flow

This blog post demonstrates how to navigate a user to record detail automatically after finishing the flow. This can be achieved with a a custom Lightning Component which can accessible in side the flow. The standard Lightning interfaces force:lightningQuickAction and lightning:availableForFlowActions will make the lightning component is accessible from core actions section in Flow builder.




Let's create a Lightning Component that can be accessible inside the flow and can be accessible for core actions in flow. Create a lightning component as specified below. The invoke method in the controller js will be fired automatically when flow invokes the lightning component.





Let's create flow now, To avoid complexity let's consider a basic contact insertion with a screen flow. 


A new contact screen in flow builder. Let's build a form with flow builder with contact first name, last name. mobile and phone. First Drag and drop a screen component from flow builder and add all text components for above mentioned fields in new contact form.




In the next step, drag and drop the create records action from flow builder and select contact object for insertion and also map all the text fields values(no flow variables required) from previous screen flow to contact object fields in current screen. Also create a variable to hold record Id of contact after insertion as in below screenshot {!contactId}. While creating this variable make sure you did not provided any default value for variable and avoid "Available for input" option in flow variable screen or else flow will throws an error.





In the final step, drag and drop the Core Actions action from flow builder and choose the lightning component that we created initially and provide mapping for record Id in the Lightning component from the flow variable contactId which consists a inserted contact Id.  


We can preview this screen flow from app builder. Just go to app builder from Lightning Quick find side bar in setup and create an App Page from app builder and drag and drop your screen flow. Once created an app page we can access it from app launcher search bar.

Using trigger events / context variables with enum System.TriggerOperation

In summer'18 release Salesforce has first introduced enum System.TriggerOperation which consists specific set of trigger context variables AFTER_DELETE, AFTER_INSERT, AFTER_UNDELETE, AFTER_UPDATE, BEFORE_DELETE, BEFORE_INSERT,BEFORE_UPDATE.

This enum makes life easy for developer from using different context variables like triger.isAfter and trigger.isInsert with a bunch of AND statements and IF statements. The new Trigger context variable Trigger.operationType  will returns an enum System.TriggerOperation during trigger context. With this enum we can easily identify the current trigger context and can invoke our logic from handlers. This enum can also be used with in a trigger handler class.

The simple snippet below demonstrates using this enum with a switch statement.

Result in debug logs when updated a record on Account object.








Happy coding, cheers!!!


Resources:
Releases notes Spring'19
TriggerOperation Enum




Sunday 25 March 2018

Lightning Component for Sending Email for List View Selected Records and using Email Templates

Hi there, At the time of this blog post, There is no standard Lightning component available for Email composer, So I created a custom component for sending Email and mimic of the standard email composer and also for sending Listview selected records.

I assumed sending email for person accounts (person accounts by default not available in salesforce you have to rise a case to enable them) of Opportunities. Please choose different object for sending(from contact List view)  since if you don't have person accounts enabled you will get compile errors.

You also need to create a rich text field in any object since we don't have standard richtext tags in lighting at the time of posting this blog post. We need rich text field to mimic the email composer screen and also to render the text review of template selected.

I have used a List view button of type visualforce in opportunities List view and in visualforce page i have placed Lightning Component.

The list view selected records will be sent to Lightning Component.

Get the snippets below to image.





Visualforce Page:


Controller Extension:
EmailComp:
Controller.js:
Helper.js:
EmailComp Apex Class:



I did not mentioned the snippet for the component "c:reUsableMultiSelectLookup" here. Because it is a custom look up component that can be available from many places like salesforce forums and blogs. All you need to do is to tweak the init section of your look up component by passing the selected items to display them automatically. 

Formula Field to calculate hours from Date Time field in Salesforce

Hey there, This is sample formula field to get difference in hours from date time field to current time.

I have considered Event standard field ActivityDateTime to calculate hours in this example

See formula below screenshot.



formula:




Powered by Blogger.

Visualforce

Apex

Lightening UI