+ Start a Discussion
Christine_KChristine_K 

Reusable Lightning Component

I'm trying to create a lightning component for a custom object called Comments__c.  Comments__c can be used for Accounts and/or Contacts.

I would like the LC to be dynamic enough where I just have one lightning component for both Accounts and Contacts.  The purpose of the lightning component is to display the comments on the record detail page of either the Account or Contact.  If it's a Contact Record, I would like to display the Account comments as well as the Contact comments.

I'm stuck as to how I can make this lightning component reusable for Contacts.  Here is my code thus far

ListComments.cmp
<aura:component controller = "CommentsController" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="newComment" type="Object"/>
    <aura:attribute name="sObjects" type="sObject[]"/>
    <aura:attribute name="CommentList" type="Comment__c[]" />
    <aura:handler name="init" value="{!this}" action="{!c.myAction}" />
    
    <aura:if isTrue="{!not(empty(v.CommentList))}">
    <lightning:card>
        <aura:set attribute="actions">
            <ui:button label="New Comment" press="{!c.createRecord}"/>
        </aura:set>
        <p class="slds-p-horizontal_small slds-text-align_center">
            <aura:iteration  items="{!v.CommentList}" var="com">
                <div class="uiOutputRichText" linkify="true" >
                    <p><ui:outputRichText aura:id="outputRT" value="{!com.Comment__c}" /></p>
                </div>
            </aura:iteration>
        </p>
    </lightning:card>
    </aura:if>
    
    
</aura:component>

CommentsController.apxc
public with sharing class CommentsController {
@AuraEnabled
    public static list<Comment__c> getRelatedList(Id recordId)
    {
        List<Comment__c> commlist = [Select id, active__c, comment__c,contact__c,createdbyid,createddate from Comment__c where Account__c=: recordId AND Active__c = true ORDER BY CreatedDate ASC];
        return commlist;
    }
}

ListCommentsController.js
({
    myAction : function(component, event, helper) 
    {
        var commList = component.get("c.getRelatedList");
        commList.setParams
        ({
            recordId: component.get("v.recordId")
        });
        
        commList.setCallback(this, function(data) 
        {
             component.set("v.CommentList", data.getReturnValue());
        });
        $A.enqueueAction(commList);
    },
    createRecord : function (component, event, helper) {
        var createRecordEvent = $A.get("e.force:createRecord");
        createRecordEvent.setParams({
            "entityApiName": "Comment__c",
            "navigationLocation" : "LOOKUP",
            "defaultFieldValues":{
                "Account__c": component.get("v.recordId")
            },
            "panelOnDestroyCallback": function(event) {
                var urlEvent = $A.get("e.force:navigateToURL");
                urlEvent.setParams({
                    "url": "/lightning/r/Account/"+component.get("v.recordId")+"/view"
                });
                urlEvent.fire();
            }
        });
        createRecordEvent.fire();
        
    }
})


 
Best Answer chosen by Christine_K
Richard MarRichard Mar
Correction:
 
public with sharing class CommentsController {
    @AuraEnabled
    public static list<Comment__c> getRelatedList(Id recordId)
    {
        Schema.SObjectType sObjType = recordId.getSObjectType();
        String sObjectTypeName = String.valueOf(sObjType);

        if (sObjectTypeName == 'Account') 
            return [Select id, active__c, comment__c,contact__c,createdbyid,createddate from Comment__c where Account__c=: recordId AND Active__c = true ORDER BY CreatedDate ASC]; 
        else if (sObjectTypeName == 'Contact') 
            return [SoQL to get records from comments__c object by Contact Id]; 
        else 
            return new List<Comment__c>();
}


 

All Answers

Richard MarRichard Mar
Update your getRelatedList() aura method as follows:
 
public with sharing class CommentsController {
    @AuraEnabled
    public static list<Comment__c> getRelatedList(Id recordId)
    {
        Schema.SObjectType sObjType = objectId.getSObjectType();
        String sObjectTypeName = String.valueOf(sObjType);

        if (sObjectTypeName == 'Account') 
            return [Select id, active__c, comment__c,contact__c,createdbyid,createddate from Comment__c where Account__c=: recordId AND Active__c = true ORDER BY CreatedDate ASC]; 
        else if (sObjectTypeName == 'Contact') 
            return [SoQL to get records from comments__c object by Contact Id]; 
        else 
            return new List<Comment__c>();
}

 
Richard MarRichard Mar
Correction:
 
public with sharing class CommentsController {
    @AuraEnabled
    public static list<Comment__c> getRelatedList(Id recordId)
    {
        Schema.SObjectType sObjType = recordId.getSObjectType();
        String sObjectTypeName = String.valueOf(sObjType);

        if (sObjectTypeName == 'Account') 
            return [Select id, active__c, comment__c,contact__c,createdbyid,createddate from Comment__c where Account__c=: recordId AND Active__c = true ORDER BY CreatedDate ASC]; 
        else if (sObjectTypeName == 'Contact') 
            return [SoQL to get records from comments__c object by Contact Id]; 
        else 
            return new List<Comment__c>();
}


 
This was selected as the best answer
Christine_KChristine_K
Thanks Richard!  Do you know how I could show the Account comments on the contact page, plus the contact comments?  The account should only show the Account comments but when you're on a contact record, it should show both account and contact comments.
Richard MarRichard Mar
Christine,

    I would update the code as follows assuming all contact records have a lookup to an Account record via a field called AccountId in the Contact Object:
 
public with sharing class CommentsController {
    @AuraEnabled
    public static list<Comment__c> getRelatedList(Id recordId)
    {
        Schema.SObjectType sObjType = objectId.getSObjectType();
        String sObjectTypeName = String.valueOf(sObjType);

        if (sObjectTypeName == 'Account') 
            return [Select id, active__c, comment__c,contact__c,createdbyid,createddate from Comment__c where Account__c=: recordId AND Active__c = true ORDER BY CreatedDate ASC]; 
        else if (sObjectTypeName == 'Contact') {
            Contact c = [SELECT Id, AccountId FROM Contact WHERE Id = :recordId LIMIT 1];
            if (c != null) {
                return [SELECT id, active__c, comment__c,contact__c,createdbyid,createddate FROM Comment__c where (Account__c=: c.AccountId OR contact__c =: recordId) AND Active__c = true ORDER BY CreatedDate ASC]; 
            }
        }
        else 
            return new List<Comment__c>();
}