0

I have an apex class which is the controller for a lightning component. In the class I want to pull through several different parts of different objects and return it in to the JS controller as one object. Now I am getting the errors: Non static AuraEnabled methods may not have parameters and AuraEnabled methods must be named with a prefix 'get'.

public with sharing class EBC_RelatedListsApx {

public class TableRow{
    @AuraEnabled public String type      {get;set;}
    @AuraEnabled public String body      {get;set;}
    @AuraEnabled public String dateString     {get;set;}
    @AuraEnabled public String user   {get;set;}
}

@AuraEnabled public List<TableRow> RowList {get; set;}

@AuraEnabled
public list<TableRow> WrapperDemoController(string processId){

    RowList = new List<TableRow>();
    TableRow tr;

    //ACTION
    for(arcusregserv__RS_Action__c action : [select arcusregserv__Action_Type__c, arcusregserv__Actual_Date__c, CreatedBy.Name, id from arcusregserv__RS_Action__c where arcusregserv__Activity__c =: processId order by CreatedDate desc]){

        tr = new TableRow();
        tr.type = 'Action';
        tr.body = action.arcusregserv__Action_Type__c;
        tr.dateString = string.valueOfGmt(action.arcusregserv__Actual_Date__c);
        tr.user = action.CreatedBy.Name;

        /*Add the TableRow to the List then and there*/
        RowList.add(tr);
    }

    //Chatter

    for(feedItem chatter : [select body, CreatedDate, CreatedBy.name from feedItem where parentId =: processId order by CreatedDate desc]){

        tr = new TableRow();
        tr.type = 'Chatter';
        tr.body = chatter.body;
        tr.dateString = string.valueOfGmt(date.newInstance(chatter.createdDate.year(), chatter.createdDate.month(), chatter.createdDate.day()));
        tr.user = chatter.CreatedBy.Name;

        /*Add the TableRow to the List then and there*/
        RowList.add(tr);
    }

    //ContentDocumentVersion
    List<id> contentDocumentIdList = new List<id>(new Map<id, contentDocumentLink>([select contentDocumentId from contentdocumentlink where linkedentityid =: processId]).keySet());

    String SOQL_IDs = '';
    for (Id id : contentDocumentIdList) {
        String id_in_quotes = '\''+id+'\'';
        if (SOQL_IDs!='') { SOQL_IDs+=','; }  //  add a comma if this isn't the first one
        SOQL_IDs += id_in_quotes;
    }
    String SOQL = 'select title, CreatedDate, Createdby.Name from contentVersion where contentDocumentId in ('+SOQL_IDs+')';

    for(contentVersion CV : database.query(SOQL)){

        tr = new TableRow();
        tr.type = 'CV';
        tr.body = CV.title;
        tr.dateString = string.valueOfGmt(date.newInstance(CV.createdDate.year(), CV.createdDate.month(), CV.createdDate.day()));
        tr.user = CV.CreatedBy.Name;

        /*Add the TableRow to the List then and there*/
        RowList.add(tr);
    }

    //Requisite

    for(arcusregserv__RS_Requisite__c requisite : [select arcusregserv__Status__c, CreatedDate, CreatedBy.name, id  from arcusregserv__RS_Requisite__c where arcusregserv__Process__c =: processId order by CreatedDate desc]){

        tr = new TableRow();
        tr.type = 'Requisite';
        tr.body = requisite.arcusregserv__Status__c;
        tr.dateString = string.valueOfGmt(date.newInstance(requisite.createdDate.year(), requisite.createdDate.month(), requisite.createdDate.day()));
        tr.user = requisite.CreatedBy.Name;

        /*Add the TableRow to the List then and there*/
        RowList.add(tr);
    }

    //Financial transaction

    for(arcshared__Financial_Transaction__c FT : [select arcshared__Status__c, CreatedDate, CreatedBy.name, id  from arcshared__Financial_Transaction__c where arcusregserv__Related_Process__c =: processId order by CreatedDate desc]){

        tr = new TableRow();
        tr.type = 'FinancialTransaction';
        tr.body = FT.arcshared__Status__c;
        tr.dateString = string.valueOfGmt(date.newInstance(FT.createdDate.year(), FT.createdDate.month(), FT.createdDate.day()));
        tr.user = FT.CreatedBy.Name;

        /*Add the TableRow to the List then and there*/
        RowList.add(tr);
    }

    return RowList;
}

}

5
  • Are you saying that the exact code that you posted generates a compilation error of "Variable does not exist: processId"? I don't see why it would.
    – Keith C
    Commented Mar 23, 2018 at 10:58
  • Oh woops sorry slight amendment. Instead of public list<TableRow> WrapperDemoController(string processId) it should be public STATIC list<TableRow> WrapperDemoController(string processId)
    – Smadger
    Commented Mar 23, 2018 at 11:02
  • In javascript check the paremeter.It should be "processId" ...check action.setparams
    – Annappa PH
    Commented Mar 23, 2018 at 11:06
  • Am an idiot it shouldn't be static. Now when I remove static I get AuraEnabled methods may not have parameters and AuraEnabled methos must be named with a prefix 'get'
    – Smadger
    Commented Mar 23, 2018 at 11:06
  • Have amended the question to my current problem.
    – Smadger
    Commented Mar 23, 2018 at 11:09

2 Answers 2

2

Fix by:

// Delete this line
@AuraEnabled public List<TableRow> RowList {get; set;}

// Strange name for a method: change it?
@AuraEnabled
public static list<TableRow> WrapperDemoController(string processId){

    List<TableRow> RowList = new List<TableRow>();
    TableRow tr;

    ...

The methods have to be static to make it clear that there is no mechanism for preserving the state of class properties between requests. Each method must take parameters from the page, do work (e.g. queries) and then return the result back to the page.

1
  • Has saved correctly. Just testing then shall mark as correct! Massive thanks!!!
    – Smadger
    Commented Mar 23, 2018 at 11:21
1

Your @AuraEnabled methods should be static. For that reason, they also cannot depend on instance variables (static methods aren't associated with an instance).

Your accessor method here should declare, populate, and return a List<TableRow> without referring to the instance variable RowList, which you can simply move into the method body.

Additionally, naming this method WrapperDemoController() makes it look like it's a constructor for a class called WrapperDemoController, which is unnecessarily confusing.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .