You need to sign in to do that
Don't have an account?
Christine_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
CommentsController.apxc
ListCommentsController.js
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(); } })
All Answers
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: