Tuesday, August 30, 2022

Sending Email in Salesforce using Apex

This is a common snippet to send an email using Salesforce Apex:

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new List<String>{'demo@sf.com'});

email.setPlainTextBody('Sample body');
email.setSubject('Sample subject');

List<Messaging.SendEmailResult> results = Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{ email });

for (Messaging.SendEmailResult sr : results) {
    if (!sr.isSuccess()) {
        List<Messaging.SendEmailError> errors = sr.getErrors();
        String errorString = String.join(errors, ', ');
        throw new AuraHandledException(errorString);
    }
}

Sending files with Email

Now assume that we have an order record in Salesforce, and multiple files have been uploaded in Files related list. And we would like to mail a recipient attaching all these files. There is a method setEntityAttachments() on SingleEmailMessage class which accepts an array of ContentVersion Ids.

Make sure that you pass the ContentVersion Ids as a list of String 😐

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setEntityAttachments(entityAttachmentIds);

Save Email as an activity

What if you would like to mail to a recipient but at the same time save the email record as an activity? For this, use setSaveAsActivity() and setWhatId() methods as below:

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
...
...
...
email.setEntityAttachments(new List<String>(contentVersionIds));
email.setSaveAsActivity(true);
email.setWhatId(orderId);

Replace Merge fields in Email Template

Now assume that you have already defined an email template, and there are merge fields (placeholders) both in the body and subject of the email template. When you send the email, these merge fields should get replaced with the Salesforce data from the record. Use renderStoredEmailTemplate() of Messaging class as below:

EmailTemplate emailTemplate = [SELECT Id, Body, Subject FROM EmailTemplate WHERE DeveloperName = :emailTemplateName LIMIT 1];

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

email.setPlainTextBody(Messaging.renderStoredEmailTemplate(emailTemplate.Id, UserInfo.getUserId(), recordId).plainTextBody);
email.setSubject(Messaging.renderStoredEmailTemplate(emailTemplate.Id, UserInfo.getUserId(), recordId).getSubject());

Remember that executing the renderStoredEmailTemplate() counts toward the SOQL governor limit as one query. This is described more in detail here.

There is a lot of information provided in Salesforce documentation, so do refer the Messaging and SingleEmailMessage classes.

And finally, a full fledged example of sending email using Salesforce Apex: https://gist.github.com/iamsonal/3ccd44b319724f4d03cdb4df0bde54d0

Share This:    Facebook Twitter

Custom Salesforce Lightning component to Share Records Manually

There are cases where user manually share records with other users. However, users with whom a record is shared neither gets a notification, nor an email alert. How are users supposed to know a record has been shared with them?

The Share tables also do not support triggers. The only way to handle this use case is to schedule an Apex job and periodically query the table for new shares since the last check.

This is a custom component developed both in Lightning Aura and LWC (the main modal is in Aura, while the supporting components are in LWC). As soon as a record is shared manually with a user, he/she will be notified by email and a Salesforce notification. Currently, this works for practically all the Salesforce standard objects, including Accounts. Additionally, if you want to remove the share entries for a certain record, click the remove icon on the RHS of the table. To utilize this component, make sure to construct a Quick Action.

Since I haven't worked with Aura in a while, I blended LWC and Aura to render this component when it could have been rendered with only LWC. This component can be extended to work with custom objects as well.

To select multiple users, I’ve used a lookup component created by Philippe Ozil. Thanks to him.

Finally, the repo link for this component: https://github.com/iamsonal/share-records

Share This:    Facebook Twitter

Wednesday, July 6, 2022

Reviewing and evaluating different integration patterns available within Salesforce

Approach

Data integration is used to synchronize data between two or more systems. It can be described as combining data from different sources into one cohesive view. The outcome of data integration should be trusted data that is meaningful and valuable to the business process.

Process integration combines business processes from two or more systems to complete a given process task. Process integration requires more robust systems and extended transaction timing to complete the integration.

Virtual integration is used to search, report, and update data in one or more external systems. This type of integration requires real-time access and retrieval of data from the source system.

Timing

Synchronous communication is when one system sends a request to another system and must wait for the receiving system to respond. Synchronous timing is generally expected in real time.

Asynchronous communication occurs when one system sends a request to another system and does not wait for the receiving system to respond. Asynchronous timing does not require real-time communications.

Source, Target, and Direction

Each integration must have a source or sending system and a target or receiving system. Direction can be more than a pointer. Integration can be unidirectional (one-way), bidirectional (two-way), omni-directional (broadcast or one-to-many), etc.

Calling Mechanism

Salesforce has several ways to initiate integrations, including triggers, controllers, workflows, processes, flows, platform events, and batch processes.

  • Apex callouts
  • Bulk API
  • Canvas
  • Chatter REST API
  • Email
  • External objects
  • Metadata API
  • Middleware
  • Outbound messages
  • Platform event
  • Push notifications
  • RESTful API
  • SOAP-based API
  • Streaming API
  • Tooling API

Error Handling and Recovery

Integration patterns react to errors and perform rollbacks in different ways. The approach used to manage error handling and recovery is critical in selecting and managing a given integration pattern.

Idempotent Design Considerations

An operation is idempotent when it produces the same result whether you execute it once or multiple times. The most common method of creating an idempotent receiver is to search for and track duplicates based on unique message identifiers sent by the consumer.

Security Consideration

Salesforce recommends two-way SSL and appropriate firewall mechanisms to maintain the confidentiality, integrity, and availability of integration requests.

State Management

The use of primary and unique foreign keys allows different systems to maintain the state of data synchronization. If Salesforce is the master, the remote system must store the Salesforce ID, and if the remote system is the master, Salesforce must store the unique remote ID.

Integration patterns supported by Salesforce.

Request and Reply: As a requesting system, Salesforce invokes a remote system call for data and waits for the integration process to complete.

Fire and Forget: As a requesting system, Salesforce invokes a remote system call for data, is acknowledged by the remote system, and does not wait to complete the integration process.

Batch Data Synchronization: Either Salesforce or a remote system invokes a batch data call or published event to synchronize data in either direction using a third-party ETL solution or Salesforce Change Data Capture.

Remote Call-In: As a target system, Salesforce receives a remote system call to create, retrieve, update, or delete data by a remote system.

UI Update Based on Data Changes: As a requesting system, Salesforce listens for a PushTopic (CometD protocol) and updates the user interface (UI) to represent the received change.

Data Virtualization: As a requesting system, Salesforce establishes a virtual connection using Salesforce Connect to create an external object to access real-time data.

Share This:    Facebook Twitter

Monday, December 13, 2021

Javascript: Working with Arrays using Asynchronous operations

Let’s say we have a list of names. For each name, we want to make an API call and get some information, and then keep a new list with this collection of information. A typical approach may look like this.

Refactoring to using map or forEach is not that straightforward here. The callbacks provided to map or forEach (or any of the array-methods) are not awaited, so we have no way of knowing when the full collection of information is done and ready to use. However, there is still a way we can write this in a nice way. Using the Promise.all method.

Awesome! Now, map returns a list of promises, and the Promise.all method takes a list of promises and resolves them in parallel. Not only did the code become much more nice and clean - but we also benefit from the fact that the promises are not resolved sequentially, which speeds up the process and increases performance.

Share This:    Facebook Twitter

Total Pageviews

My Social Profiles

View Sonal's profile on LinkedIn

Tags

__proto__ $Browser Access Grants Accessor properties Admin Ajax AllowsCallouts Apex Apex Map Apex Sharing AssignmentRuleHeader AsyncApexJob Asynchronous Auth Provider AWS Callbacks Connected app constructor Cookie CPU Time CSP Trusted Sites CSS Custom settings CustomLabels Data properties Database.Batchable Database.BatchableContext Database.query Describe Result Destructuring Dynamic Apex Dynamic SOQL Einstein Analytics enqueueJob Enterprise Territory Management Enumeration escapeSingleQuotes featured Flows geolocation getGlobalDescribe getOrgDefaults() getPicklistValues getRecordTypeId() getRecordTypeInfosByName() getURLParameters Google Maps Governor Limits hasOwnProperty() Heap Heap Size IIFE Immediately Invoked Function Expression Interview questions isCustom() Javascript Javascript Array jsForce Lightning Lightning Components Lightning Events lightning-record-edit-form lightning:combobox lightning:icon lightning:input lightning:select LockerService Lookup LWC Manual Sharing Map Modal Module Pattern Named Credentials NodeJS OAuth Object.freeze() Object.keys() Object.preventExtensions() Object.seal() Organization Wide Defaults Override PDF Reader Performance performance.now() Permission Sets Picklist Platform events Popup Postman Primitive Types Profiles Promise propertyIsEnumerable() prototype Query Selectivity Queueable Record types Reference Types Regex Regular Expressions Relationships Rest API Rest Operator Revealing Module Pattern Role Hierarchy Salesforce Salesforce Security Schema.DescribeFieldResult Schema.DescribeSObjectResult Schema.PicklistEntry Schema.SObjectField Schema.SObjectType Security Service Components Shadow DOM Sharing Sharing Rules Singleton Slots SOAP API SOAP Web Services SOQL SOQL injection Spread Operator Star Rating stripInaccessible svg svgIcon Synchronous this Token Triggers uiObjectInfoApi Upload Files VSCode Web Services XHR
Scroll To Top