If you have a product record / a training course / a booking link or any record which you have an external like want to share to social media (Facebook, Twitter, LinkedIn), This simple solution perhaps is for you. In this article, we are going to create an LWC which can allow you to share any record to LinkedIn, as soon as the object has a “Share_URL__c” field. Also, you can clone it to create LWCs to share to other platforms.

- Create a Share_URL__c field in target object. This can be a string / url / formula for you need.

- Create an Apex helper to query the Share_URL__c field of any given object:
//ShareSocialHelper to query Share_URL__C field of any object by ID
public with sharing class ShareSocialHelper {
public static final string fieldAPI = 'Share_url__c';
@AuraEnabled
public static string getShareUrlField(string recordId){
//not an ID return '';
if(!(recordId instanceOf Id)) return '';
Id recId = Id.valueOf(recordId);
//Get Describes Object Result with Id
Schema.DescribeSObjectResult od = recId.getSObjectType().getDescribe();
string objectAPI = od.getName();
//Get all sObjectField
Map<string,Schema.SObjectField> mfields = od.fields.getMap();
if(!mfields.containsKey(fieldAPI)){
return '';
}
//get specific field with field API
Schema.SObjectField field = od.fields.getMap().get(fieldAPI);
string query = generateSOQL(fieldAPI,objectAPI,recId);
List<SObject> sObj = Database.query(query);
if(sObj.isEmpty()) return '';
return (String) sObj.get(0).get(fieldAPI);
}
private static string generateSOQL(String fieldName, string objectAPI,string recId){
string rtn = 'select '+fieldName+' from '+ objectAPI + ' where Id=\''+recId+'\'';
return rtn;
}
}
- Create a shareLinked LWC to allow it to be added to any record page. I used an apex helper to query record because I want it can be placed in any record page.
<!--shareLinked.html-->
<template>
<lightning-card title="Share To Linkedin">
<span onclick={handleClick}>
<img src={linkedLogo}>
</span>
</lightning-card>
</template>
//shareLinked.js
import { LightningElement,api } from 'lwc';
import socialIcons from '@salesforce/resourceUrl/socialIcons';
import getShareUrl from '@salesforce/apex/ShareSocialHelper.getShareUrlField';
import {convertObject2Params} from 'c/utils'
let LINKEDIN_SHARE_ARTICLE = 'https://linkedin.com/shareArticle'
export default class ShareLinked extends LightningElement {
@api recordId;
sharedUrl = 'https://leeseifer.wordpress.com';
linkedLogo = socialIcons +'/icons8-linkedin-96.png';
connectedCallback(){
getShareUrl({recordId:this.recordId})
.then((result)=>{
this.sharedUrl = result;
}).catch((error)=>{
console.log('error ', error);
});
}
handleClick(){
window.open(LINKEDIN_SHARE_ARTICLE +
convertObject2Params({mini:false,url:this.sharedUrl}),
'_blank',
"toolbar=no,scrollbars=no,top=500,left=500,width=660,height=470"
)
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>48.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
</targets>
</LightningComponentBundle>
//Utils component
//utils.js
const convertObject2Params = (obj) => {
return ('?' +
Object.keys(obj)
.filter(key => !! obj[key])
.map(key => `${key}=${encodeURIComponent(obj[key])}`)
.join('&')
);
}
export {
convertObject2Params
}
- Add the component to Lightning Page Layout and Test.
