Salesforce To Salesforce External Sharing related list in Lightning

Salesforce To Salesforce External Sharing related list in Lightning

Sample Code:

Apex Class:
public class ExternalSharingController {
    
    @AuraEnabled( cacheable = true )
    public static List < ConnectionWrapper > fetchConnections( String recId ) {
        
        List < ConnectionWrapper > listConnections = new List < ConnectionWrapper >();
        List < PartnerNetworkRecordConnection > listPNRCs = [
            SELECT Id, ConnectionId, StartDate, EndDate, Status
            FROM PartnerNetworkRecordConnection WHERE LocalRecordId =: recId
        ];
        
        if ( listPNRCs.size() > 0 ) {
            
            Set < Id > setConnectionIds = new Set < Id >();
            Map < Id, String > mapConnectionName = new Map < Id, String >();
            
            for ( PartnerNetworkRecordConnection objPNRC : listPNRCs )
                setConnectionIds.add( objPNRC.ConnectionId );
            
            for ( PartnerNetworkConnection objPNC : [ SELECT Id, ConnectionName FROM PartnerNetworkConnection WHERE Id IN: setConnectionIds ] )
                mapConnectionName.put( objPNC.Id, objPNC.ConnectionName );
            
            for ( PartnerNetworkRecordConnection objPNRC : listPNRCs ) {
                
                ConnectionWrapper wrap = new ConnectionWrapper();
                wrap.connectionName = mapConnectionName.get( objPNRC.ConnectionId );
                wrap.startDate = objPNRC.StartDate;
                wrap.endDate = objPNRC.EndDate;
                wrap.status = objPNRC.Status;
                listConnections.add( wrap );
                
            }
            
        }
        
        return listConnections;
        
    }
    
    public class ConnectionWrapper {
        
        @AuraEnabled
        public String connectionName;
        @AuraEnabled
        public DateTime startDate;
        @AuraEnabled
        public DateTime endDate;
        @AuraEnabled
        public String status;
        
    }

}

HTML:
<template>  
    <lightning-card>
        <template if:true={displayBool}>
            <lightning-datatable
                    key-field=”Id”
                    data={records}
                    columns={columns}
                    hide-checkbox-column=”true”>
            </lightning-datatable>
        </template>  
        <template if:false={displayBool}>
            Record is not transferred to the other org(s).
        </template>
        <template if:true={error}>
            {error}
        </template>
    </lightning-card>
</template>

JavaScript:
import { LightningElement, wire, api } from ‘lwc’;
import fetchConnections from ‘@salesforce/apex/ExternalSharingController.fetchConnections’;
const COLUMNS = [
    { label: ‘Connection Name’, fieldName: ‘connectionName’ },
    {
        label: ‘Start Date’, fieldName: ‘startDate’, type: “date-local”,
        typeAttributes: {
            month: “2-digit”,
            day: “2-digit”
        }
    },
    {
        label: ‘End Date’, fieldName: ‘endDate’, type: “date-local”,
        typeAttributes: {
            month: “2-digit”,
            day: “2-digit”
        }
    },
    { label: ‘Status’, fieldName: ‘status’ }
];
export default class ExternalSharing extends LightningElement {
    
    @api
    recordId;
    records;
    error;
    columns = COLUMNS;
    displayBool = false;

    @wire( fetchConnections, { recId: ‘$recordId’ } )  
    wiredRecords( value ) {
        
        console.log( ‘Data received ‘ + JSON.stringify( value ) );
        const { data, error } = value;

        if ( data ) {
            
            this.records = data;
            this.error = undefined;

            if ( this.records.length > 0 )
                this.displayBool = true;

        } else if ( error ) {

            this.error = error;
            this.records = undefined;
            this.displayBool = false;

        }

    }
}

JS-meta.xml:
<?xml version=”1.0″ encoding=”UTF-8″?>
<LightningComponentBundle xmlns=”http://soap.sforce.com/2006/04/metadata”>
    <apiVersion>51.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>

Output:

Leave a Reply