Showing posts with label sObject. Show all posts
Showing posts with label sObject. Show all posts

Thursday, 27 July 2023

Enhancing Record Processing: Automating Bulk Operation with Salesforce Flow from List Views

Requirement:

In an E-commerce sector project, the client based in Georgia, USA was required to implement bulk operation(To update multiple records simultaneously) from a list view button using salesforce flow.

Challenge:

As we do not have to use the apex class for this requirement, our main challenge was passing all the selected record Ids from the sObject's list view to the salesforce flow.

Solution:


Concisely, To achieve this follow the below steps:
  • Creating a screen flow with a variable named "ids" for delete operation.
  • Creating a List button with a custom URL.

 

  • To Create Screen Flow:
  • Go to the setupQuick Find search box ⇒ Flows.  
  • After that click on New Flow and then select Screen Flow.
  • To create a collection variable go to the toolbox and click on New Resource button.
  • Select Variable as Record Type and Enter values as in below image.

 

  

[Note: API Name of the variable must be "ids".] 

  •  After that create a variable for record count to assign the count of the ids. 

 

 

  •  Add Decision Element and enter values as below.

 


  •  Add Delete Record element for Available outcome and add Screen element for Not Available outcome to display Error message. 

 

 

  • After Delete Record element add Screen element for success message.
  • Have a look on below image for flow reference.

 

 

  • To create a List Button with a custom URL:
  • Go to the Object Manager and open sObject(here it is Account) then click on "Buttons, Links and Actions" Tab.
  • Then click on "Create Button or Link" button and enter values as below in image and click on save button.

 

 

  • Here the custom URL "/flow/Delete_bulk_records?retURL=001/o" includes API Name of flow i.e. Delete_bulk_records and retURL=001/o to return to the Accounts List View page.
  • The value of retURL 001/o includes "001" is from first three character of record Id of Account sObject and "o" to redirect to the list view page.
  • After creating List Button click on List View Button Layout to add the List Button in List View Layout.
  • Edit the List view Layout, Scrolldown the cursor to the Custom Button and Add the created List Button in the Selected button list and save the changes. 

 

Output: 

 


Conclusion:

This is how the Automating Bulk Operation with Salesforce Flow from List Views is implemented which allows you to update multiple records simultaneously, saving time and effort.

  

If you have any questions you can reach out our Salesforce Consulting team here.

Thursday, 28 July 2022

Fill the form and display data into data table without storing it into sObject/database using LWC

SCENARIO


While working on one of the requirements for an Automobile sector project for a client based out of Dallas, Texas, there was a requirement to add data using a form in LWC and display it into the dataTable without storing it into sObject/database.

CHALLENGE


Although the requirement seems easy, we faced challenges while displaying records into the dataTable and reRendering it when a new record is added.
 

APPROACH


To display records without storing them into sObject/database, We used a JSON object that will be responsible to store all the data to be displayed in the dataTable.
Also, we declared that JSON object as a reactive property using the @track decorator. So the table can be reRendered automatically when the value of that variable is changed/updated.

Data.html

<template>
    <lightning-card title="Add data into Datatable">
        <lightning-layout>
            <lightning-input class="slds-p-around_medium" label="Make"  type="string" name="Make"  onchange={makeChangedHandler} required="true"> </lightning-input>
            <lightning-input class="slds-p-around_medium" label="Model" type="string" name="Model" onchange={modelChangedHandler} required="true"> </lightning-input>
            <lightning-input class="slds-p-around_medium" label="Year" type="date" name="Year" onchange={yearChangedHandler} required="true"> </lightning-input>
        </lightning-layout>
        <lightning-button class="slds-m-left_x-small" label="Display" variant="brand" onclick={handleClick}>
        </lightning-button>
        <lightning-datatable key-field="id" id="datatable" data={fields} columns={columns}>
        </lightning-datatable>                 
    </lightning-card>  
</template>

Data.js

import { LightningElement,track } from 'lwc';

export default class Data extends LightningElement {
     columns = [{
        label: 'Make',
        fieldName: 'Make',
        type: 'text',
        sortable: true
    },
    {
        label: 'Model',
        fieldName: 'Model',
        type: 'text',
        sortable: true
    },
    {
        label: 'Year',
        fieldName: 'Year',
        type: 'Date',
        sortable: true
    },
];

    strMake;
    strModel;
    strYear;
    @track fields =[];
    
    makeChangedHandler(event){
        this.strMake = event.target.value;  
    }
    modelChangedHandler(event){
        this.strModel = event.target.value;
    }
    yearChangedHandler(event){
        this.strYear = event.target.value;
    }

    handleClick(){
        if(this.strMake  &&  this.strModel  && this.strYear)
        {
            this.fields = [...this.fields,{'Make' : this.strMake, 'Model' : this.strModel, 'Year' : this.strYear}];   
        }
        this.strMake ='';
        this.strModel ='';
        this.strYear ='';
        //code to clear field once values are entered
        this.template.querySelectorAll('lightning-input').forEach(element => {
        element.value = null;     
        });
    }
}

There are 3 input fields in the form. Whenever the values will be entered in these fields, it will get stored in their respective variables (strMake, strModel, strYear).

On click of Display button, these 3 inputs will be validated and pushed into an array, which is then used to display data in the data table. Also, it will clear these 3 input fields and allow users to enter another record.

As the array is declared as a reactive property, it will reRender the table automatically to display the newly created record.



OUTPUT




CONCLUSION


By using the above-mentioned approach & code, We will be able to add data into the data table without using sObject or saving data into a database in LWC.

If you have any questions you can reach out to our Salesforce Consulting team here.

Thursday, 3 February 2022

Retrieve Custom Metadata Type Records Using Static Methods

With salesforce Spring21 release , there is no need to write a Salesforce Object Query Language (SOQL) to access Custom Metadata Type in Apex. Salesforce introduces new methods as similar to accessing custom settings. 

Before Spring21 release, To access Custom Metadata Type Record only using SOQL in Apex like below,

// Before Spring 21
List<Country_Code__mdt> listCountryCode = [SELECT Id,Label,MasterLabel,Country_Code__c,DeveloperName from Country_Code__mdt];
      
for(Country_Code__mdt c : listCountryCode){
           System.debug('Label ->'+ c.Label + ',' + 'Country code ->' + c.Country_Code__c + ',' +'Devloper name ->' + c.DeveloperName);
      }
/* OUTPUT
 * Label -> CANADA,Country code ->CAN,Devloper name ->CANADA
 * Label -> INDIA,Country code ->IND,Devloper name ->INDIA
 * Label -> UNITED STATES,Country code ->UNS,Devloper name ->UNITED STATES
*/

Salesforce spring21 release provides an option to access Custom Metadata Type record using static methods without SQOL.

Using Apex getAll(), getInstance(recordId), getInstance (qualifiedApiName), and getInstance(developerName) methods are used to retrieve information from custom metadata type records faster. These methods don’t rely on the SOQL engine and return the sObject details directly by the call. Below are few benefits of these methods.
  • It removes need of Salesforce Object Query Language (SOQL).
  • Eliminate any SOQL limits.
  • Build the code cleaner and faster.
Let's see all methods with example.
  • getAll()
It returns a map containing custom metadata records for the specific custom metadata type. The map's keys are the Record ID and the map’s values are the sObjects record.

List<Country_Code__mdt> listCountryCode = Country_Code__mdt.getAll().values();
      
for(Country_Code__mdt c : listCountryCode){
           System.debug('Label ->'+ c.Label + ',' + 'Country code ->' + c.Country_Code__c + ',' +'Devloper name ->' + c.DeveloperName);
      }
/* OUTPUT
 * Label -> CANADA,Country code ->CAN,Devloper name ->CANADA
 * Label -> INDIA,Country code ->IND,Devloper name ->INDIA
 * Label -> UNITED STATES,Country code ->UNS,Devloper name ->UNITED STATES
*/
  • getInstance(recordId)
It returns a single custom metadata type sObject record for a specified record ID.

Country_Code__mdt CountryCodeRecord = Country_Code__mdt.getInstance('m022w000000lPl8AAE');
System.debug('Label ->'+ CountryCodeRecord.Label + ',' + 'Country code ->' + CountryCodeRecord.Country_Code__c);
/*OUTPUT
 * Label ->CANADA,Country code ->CAN
*/
  • getInstance(developerName)
It returns a single custom metadata type sObject record for a specified developerName field of the custom metadata type object.

Country_Code__mdt CountryCodeRecord = Country_Code__mdt.getInstance('CANADA');
System.debug('Developer Name ->'+ CountryCodeRecord.DeveloperName + ',' + 'Country code ->' + CountryCodeRecord.Country_Code__c);
/*OUTPUT
 * Developer Name->CANADA,Country code ->CAN
*/
  • getInstance(qualifiedApiName)
It returns a single custom metadata type sObject record for a qualified API name specified as parameter.

Country_Code__mdt CountryCodeRecord = Country_Code__mdt.getInstance('INDIA');
System.debug('Label ->'+ CountryCodeRecord.Label + ',' + 'Country code ->' + CountryCodeRecord.Country_Code__c);
/*OUTPUT
 * Label->INDIA,Country code ->IND
*/
 

If you have any questions you can reach out our Salesforce Consulting team here.

Thursday, 17 June 2021

Salesforce Custom Metadata Type

In Salesforce Custom Metadata type is similar to creating custom object or custom setting. A custom metadata type's records are metadata, not data, in and of themselves. In contrast to Custom Settings, where only metadata is eligible for migration, Custom Metadata Type and its records can be migrated from one org to another during deployment. Custom Metadata is usually deployable, package-able, customizable, and upgradeable. The key benefit of using Custom Metadata is that it does not count against the SOQL query limit for each APEX transaction.

Custom metadata types can be used for


  • Mappings— Make connections between objects, such as a custom metadata type that allocates cities, states, or provinces to specific countries & regions. 
  • Business rules— Custom functionality can be combined with configuration records. To route payments to the correct destination, use custom information types and Apex code.
  • Master data— Assume your organization utilizes a basic accounting system. Create a special metadata type for custom charges such as customs and VAT rates. Subscriber orgs can refer to the master data if this type is included as part of an extension package. 
  • Whitelists— Keep track of lists like approved contributors and pre-approved vendors. 
  • Secrets— Protected custom metadata types within a package can be used to store information such as API keys. 

Field Manageability


Field manageability is used to manage custom fields when they are created under the Custom Metadata Type. Custom metadata type supports the following custom field types: 
  • Metadata Relationship 
  • Checkbox
  • Date
  • Date and Time 
  • Email
  • Number
  • Percent
  • Phone
  • Picklist
  • Text
  • Text Area 
  • URL

Access Custom Metadata Type Records


There are new ways to access Custom Metadata Types in the Salesforce Spring-21 pre-release orgs and similar to those for Custom Settings. It is no longer necessary to query them using SOQL, and the contribution to the Query Rows limit is reduced. To access information from custom metadata type records faster, use the Apex getAll(), getInstance(recordId), getInstance(qualifiedApiName), and getInstance(developerName) methods. These methods don't use the SOQL engine and return the sObject information from the call directly.

1.Access the Custom Metadata Type records before Spring 21.

2.Access the Custom Metadata Type records after Spring 21.

A. getAll() to get Custom Metadata Type Records.

The following example uses the getAll() method. The custom metadata type named Ticket has a field called TicketType 

B. getInstance() to get specific Custom Metadata Type Record.

What about if you don’t need to get all records and need to access only a single record? In that case, you can use the getInstance() method. 

Advantages of Custom Metadata:

  • It's possible to distribute metadata! There will be no more time-consuming post-deployment configuration, as there will be with custom settings. To create your default custom setting records, you don't need to develop Apex classes.
  • Change sets or the force.com migration tool can also be used to deploy custom metadata records with metadata type definitions (ANT). The records in custom settings are uploaded after the definition of the custom setting is deployed. 
  • ListViews, Page Layouts, and Validation Rules can be created on the Custom Metadata Types.
  • Metadata Relationships are a thing of beauty! Lookups between Custom Metadata objects are possible. You may also perform an Object Definition lookup.
  • With custom metadata types, you can issue unlimited Salesforce Object Query Language (SOQL) queries for each Apex transaction. 
  • Custom metadata type is visible in test class without using “SeeAllData”. 
  • Custom Settings has the same permissions to edit records and configure the system. The “Configure Application” permission allows you to do both. You can edit records with Custom Metadata's "Configure Application," but you'll need "Author Apex" to update the configuration.

Limitations


The following are the limitations of Custom Metadata Type:
  • Custom metadata records cannot exceed the size of 10MB.
  • It does not support formula field data type.
  • It cannot be updated through Apex. The only way to edit custom metadata types is by leveraging metadata API
  • 100 custom metadata types can be created per salesforce org. 
  • We can create only 100 fields per custom metadata type. 
  • Global picklists are not supported. 

Summary


By creating custom metadata, we can create a static set of data and reuse it in our applications, triggers, apex class, test class, Aura components, etc. Click here for more details. 

If you have any questions you can reach out our Salesforce Consulting team here.