Sunday, 29 October 2023

How to include a screen flow in a Lightning Web Component

 Hi,

Assume  you have a flow called "Quick Contact Creation" and API Name for the same is "Quick_Contact_Creation".

To include the above flow in your component ,  set the lightning-flow component's flowApiName attribute to the name of the flow that you want to use. The component includes navigation buttons (Back, Next, Pause, and Finish), for users to navigate within the flow.

Eg:

<template>
    <lightning-flow
        flow-api-name='Quick_Contact_Creation'>
    </lightning-flow>
</template>

Saturday, 28 October 2023

HTML Template Directives (lwc:if|elseif={expression} and lwc:else)

 Hi,

A directive is a special attribute that adds dynamic behavior to an HTML template. You can use certain directives on a root <template> tag, a nested <template> tag, or an HTML element such as a <p> tag. Some directives are supported for use with different tags.

As part of this post we discuss the directives lwc:if | elseif={expression} and lwc:else

Before introducing lwc:if | elseif={expression} and lwc:else we had if:true and if:false

The if:true and if:false directives are no longer recommended. They may be deprecated and removed in the future. Use lwc:iflwc:elseif, and lwc:else instead.

if:true|false={expression} conditionally renders DOM elements in a template, calling the expression for each of if:true and if:false. In cases where you chain if:true and if:false directives, they are not as performant nor as lightweight as the lwc:iflwc:elseif, and lwc:else directives. See Render DOM Elements Conditionally.

The expression can be a JavaScript identifier (for example, person) or dot notation that accesses a property from an object (person.firstName). The engine doesn’t allow computed expressions (person[2].name['John']). To compute the value of expression, use a getter in the JavaScript class.

lwc:if|elseif={expression} and lwc:else 

Conditionally render DOM elements in a template. lwc:iflwc:elseif, and lwc:else supersede the if:true and if:false directives.

Use the conditional directives on nested <template> tags, <div> tags or other HTML elements, and on your custom components tags like <c-custom-cmp>.

Both lwc:elseif and lwc:else must be immediately preceded by a sibling lwc:if or lwc:elseif.

Eg:1

<template lwc:if={expression}></template>

<template lwc:else></template>

Both lwc:if and lwc:elseif must evaluate an expression. However, lwc:else must not have an attribute value.

Eg:2

<template lwc:if={expression}></template>

<template lwc:elseif={expression_elseif1}></template>

<template lwc:elseif={expression_elseif2}></template>

<template lwc:else></template>

The expression passed in to lwc:if and lwc:elseif supports simple dot notation. Complex expressions like !conditionobject?.property?.condition or sum % 2 === 1 aren't supported. To compute such expressions, use a getter in the JavaScript class.

Note

lwc:if, lwc:elseif, and lwc:else can't be applied to the same element and they cannot be combined with the if:true and if:false directives.


Reference:

https://developer.salesforce.com/docs/platform/lwc/guide/reference-directives.html?q=html+directives#directives-for-html-elements

Tuesday, 27 June 2023

Due Date calculation formula for excluding weekends in flow

 Hi ,

The following formula field calculates the Due Date provided the Today() date and numberOfDays to be added to the Today() date excluding Saturdays and Sundays in the flow.

CASE(

MOD(TODAY()  - DATE(1900, 1, 7), 7),

0, (TODAY() ) + numberOfDays + FLOOR((numberOfDays-1)/5)*2,

1, (TODAY() ) + numberOfDays + FLOOR((numberOfDays)/5)*2,

2, (TODAY() ) + numberOfDays + FLOOR((numberOfDays+1)/5)*2,

3, (TODAY() ) + numberOfDays + FLOOR((numberOfDays+2)/5)*2,

4, (TODAY() ) + numberOfDays + FLOOR((numberOfDays+3)/5)*2,

5, (TODAY() ) + numberOfDays + CEILING((numberOfDays)/5)*2,

6, (TODAY() ) - IF(numberOfDays>0,1,0) + numberOfDays + CEILING((numberOfDays)/5)*2,

null)


Here numberOfDays is flow variable.


Example Scenario:

When an Account is created, a task should be created with a due date set to 5 days from today.

Reference:

https://help.salesforce.com/s/articleView?id=sf.formula_examples_dates.htm&type=5

Monday, 26 June 2023

Invoking(Launching) a Flow from Apex

 Hi,

We can launch a flow from Apex in the following ways


Invoking the flow from Apex Statically:

Without Namespace:

  Map<String, Object> inputs = new Map<String, Object>();

  inputs.put('AccountID','001XXXXXXX');

  inputs.put('OpportunityID','006XXXXXXX');

  Flow.Interview.Calculate_discounts myFlow = 

  new Flow.Interview.Calculate_discounts(inputs);

  myFlow.start();


With Namespace:

  Map<String, Object> inputs = new Map<String, Object>();

  inputs.put('AccountID','001XXXXXXX');

  inputs.put('OpportunityID','006XXXXXXX');

  Flow.Interview.myNamespace.Calculate_discounts myFlow = 

  new Flow.Interview. sftech.Calculate_discounts(inputs);

  myFlow.start();

Invoking the flow from Apex Dynamically:

Without Namespace:

Map<String, Object> inputs = new Map<String, Object>();

  inputs.put('AccountID','001XXXXXXX');

  inputs.put('OpportunityID','006XXXXXXX');

Flow.Interview myFlow = Flow.Interview.createInterview('Calculate_discounts', inputs);

myFlow.start();

With Namespace:

  Map<String, Object> inputs = new Map<String, Object>();

  inputs.put('AccountID','001XXXXXXX');

  inputs.put('OpportunityID','006XXXXXXX');

  Flow.Interview myFlow = Flow.Interview.createInterview('sftech', 'Calculate_discounts', inputs);

  myFlow.start();

Here Calculate_discounts  is the name of the flow.

AccountID and OpportunityID are input variables of the flow.

sftech is the namespace

How to get variable values from flow to Apex?

system.debug('My Output Variable: ' + myFlow.getVariableValue('varName'));


Note: We can invoke flow from Apex if the flow type is "Autolaunched Flow (No Trigger)" only







Reference:

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/flow_interview_class.htm#apex_Flow_Interview_methods






Monday, 19 June 2023

Manage Apex Access for Package Version Creation Tests

 Sometimes the Apex tests that you write require a user to have certain permission sets or permission set licenses. Use the apexTestAccess setting to assign permission sets and permission set licenses to the user in whose context your Apex tests get run at package version creation.

Its very important step to include "apexTestAccess" if we are not using System.runas() in test methods for generating 2GP packages.



"packageDirectories": [

    {

        "path": "force-app",

        "package": "TestPackage", 

        "versionName": "ver 0.1",

        "versionNumber": "0.1.0.NEXT",

        "default": true, 

        "unpackagedMetadata": {

            "path": "my-unpackaged-directory"

        },

        "apexTestAccess": {

               "permissionSets": [

                   "Permission_Set_1",

                   "Permission_Set_2"

               ],

               "permissionSetLicenses": [

                   "SalesConsoleUser"

               ]

           }


    }, 

]

Reference:

https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_unlocked_pkg_unpackaged_md.htm

What’s the difference between Einstein Article Recommendations and Suggested Articles?

How Does Einstein Article Recommendations Work? Einstein Article Recommendations helps support agents resolve customer cases efficiently by ...