You need to sign in to do that
Don't have an account?
Holly Havelka 10
Help With Basic Lightning Component
Hi all,
I have the following custom lightning component, but it is coming up with the following error: Unknown controller action 'getOpps'.
component:
I have the following custom lightning component, but it is coming up with the following error: Unknown controller action 'getOpps'.
component:
<aura:component controller="AcctOppsController" implements="flexipage:availableForRecordHome,force:hasRecordId"> <aura:attribute name="mydata" type="OpportunityContactRole[]"/> <aura:attribute name="mycolumns" type="List"/> <aura:attribute name="recordId" type="Id" /> <aura:attribute name="currentRecordId" type="Id" /> <aura:handler name="init" value="{!this }" action="{! c.doInit }"/> <div style="height: 300px"> <lightning:datatable keyField="id" data="{! v.mydata }" columns="{! v.mycolumns }" hideCheckboxColumn="true"/> </div> </aura:component>controller:
({ doInit: function (cmp, event, helper) { cmp.set('v.mycolumns', [ {label: 'Opportunity Name', fieldName: 'opportunityId', type: 'text'}, {label: 'Contact Name', fieldName: 'contact', type: 'text'}, {label: 'Role', fieldName: 'role', type: 'text'}, {label: 'Amount', fieldName: 'amount', type: 'currency'} ]); var fetchData = { opportunityId: "opportunityId", contact : "Contact.Name", role : "Role", amount : "Opportunity.Amount" }; helper.fetchData(cmp,fetchData); } })helper:
({ fetchData : function(cmp) { var recordId = cmp.get("v.recordId"); var action = cmp.get("c.getOpps"); action.setParams({ "currentRecordId" : recordId }); action.setCallback(this, $A.getCallback(function (response) { var state = response.getState(); if (state ==="SUCCESS") { cmp.set("v.mydata", response.getReturnValue()); } else if (state === "ERROR") { var errors = response.getError(); console.error(errors); } } )); $A.enqueueAction(action); } })apex controller:
public with sharing class AcctOppsController{ @AuraEnabled public String currentRecordId {get;set;} public AcctOppsController(ApexPages.StandardController controller) { currentRecordId = ApexPages.CurrentPage().getparameters().get('id'); } @AuraEnabled public List<OpportunityContactRole> getOpps() { List<OpportunityContactRole> oppresults = [SELECT Contact.name, Role, OpportunityId, Opportunity.Amount, Opportunity.StageName, Opportunity.Type FROM OpportunityContactRole WHERE contact.accountid=:currentRecordId]; return oppresults; } }
Any thoughts on what I am missing?
With a new object : let ob = {}; it is simpler.
<aura:attribute name="mydata" type="Object[]"/> : generic array of objects
All Answers
<aura:attribute name="recordId" type="Id" />
I was able to get it to return the values, and below is my updated code:
component: controller: helper: apex:
The problem I am now facing is that 'Contact.name' 'Opportunity.Name' is not getting values from the parent record.
Any thoughts on pulling in the parent values vs. just the ids for parent fields?
With a new object : let ob = {}; it is simpler.
<aura:attribute name="mydata" type="Object[]"/> : generic array of objects
This object (fetchData) was the pattern object for the conversion probably. We have used "ob" instead.
Probably to avoid the hard coding field by field ( ob.opportunityName = item.Opportunity.Name; )
Using the object fetchData, it is easy for direct field (OpportunityId and Role) to have a generic coding but for the indirect fields ( Contact.Name and Opportunity.Amount ), it is more difficult and I haven't been able to solve this last problem.
By the way, the helper of Salesforce for the official documentation is bizarre:
https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/example
fakerLib = this.mockdataLibrary.getFakerLib();
Only works for their tests or just an error?
The fetching of data by scope is perhaps manage by this strange "FakerLib". It is the first time that I have seen something like that.
Whatever it might be, this sample is quite useless for someone who wants an example that works with data on the server side.
https://developer.salesforce.com/docs/component-library/bundle/lightning:datatable/example
so that I could fully explore what a DataTable has to offer.
I've used much of the advice above to produce the solution included below.
I haven't completed all the buttons for adding records. Feel free to continue and provide a revision.
I struggled and gave in on the field that is a 'Location' type object.
If anyone can suggest a working syntax I'd be grateful. I ended up treating it as text.
I've used a Promise but am still puzzled as to why my result isn't refreshed in my diagnostic <Div> whereas my message, set in the same place, is reported correctly. Again, any answers appreciated.
So here is my solution:
DataTableInAction.cmp
DataTableInActionController.js
DataControllerInActionHelper.js class DataTableInActionController
Hope this helps and I look forward to answers to my outstanding queries and lack of knowledge.
I write this as the world is being attacked by the Corona virus. As Marc Benioff says, "..we’re all connected like never before. We’re called upon to be our best selves, with patience, understanding and compassion".
I wish that you stay safe and well.
https://newstechnologystuff.com/2019/01/01/lightning-datatable-lazy-loading-with-inline-editing-and-actions/