pegros / PEG_MISC

Small package of standalone LWC components for the App Builder, addressing simple Use Cases in a very easy to configure approach.
MIT License
4 stars 3 forks source link

[feat] sfpegMediaPlayCmp - Use standard related list instead of custom field #2

Open tprouvot opened 1 year ago

tprouvot commented 1 year ago

Is your feature request related to a problem? Please describe. Using a text custom field to store the id can compromise data integrity if the contentDocument is deleted. To avoid this, it would be great if we could use the related list relation name instead of a custom field to store the ContentDocumentId

Describe the solution you'd like We can have parameters on the LWC to store the relatedList relation name and a filter.

image

Describe alternatives you've considered

Use wire api to retrieve relatedList records

// Related list Data Fetch
    error;
    records;
    @wire(getRelatedListRecords, {
        parentRecordId: "$recordId",
        relatedListId: 'ListEmails',
        fields: ['ListEmail.Id'],
        where: "{ and: [{ Name: { like: \"Bob%\" }}] }"
    }) listInfo({ error, data }) {
        console.log('wiredRelated: wired');
        if (data) {
            this.records = data.records;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.records = undefined;
        }
    }

Use apex soql to retrieve the related list record

@AuraEnabled(cacheable=true)
    public static Id getContentDocumentId(Id sobjectId, String relationName, String fieldName, String title){

        String sobjectName = sobjectId.getSobjectType().getDescribe().getName();

        String query = 'SELECT Id, (SELECT ' + fieldName + ' FROM ';
        query+= relationName +  ' WHERE Title LIKE \'' + title + '\') FROM ' + sobjectName + ' WHERE Id=:sobjectId';
        SObject sobj = Database.query(query);
        List<SObject> sobjList = sobj.getSObjects(relationName);
        return objList.get(0).get(fieldName);
    }

Additional context WARNING: All the related list are not available through the wire service, for example AttachedContentDocuments on Campaign

pegros commented 1 year ago

This is an interesting option @tprouvot but, unfortunately, the Files related list is not supported by this mechanism and would therefore require implementing a custom Apex controller, which was not the initial design option (LDS first). Thanks for pointing out to this standard LDS capability. 🙏

When this requirement is confirmed, I would rather implement a new sfpegMediaListPlayCmp Component enabling to: