1

I am getting the correct output in the console query editor but when I use the lightning component in an app the data is not showing. The table which I query I get the name of the object but its look-up relationship remains blank.

enter image description here

enter image description here

I would like to know if I can and where do I add a @wire call to a function in the Salesforce developers console? I'm attempting to solve my problem using something similar to this question but with a @wire call answer

When adding it to Controller JS...

({ 
    @wire (sayHello, {name : ‘Peter’}) helloWorld;
})

...it throws this error:

JsonStreamReader$JsonStreamParseException

I cannot connect via VS code at the moment.

Update

I found out that what I am getting when calling response.getReturnValue() is a list of javascript objects from the database.

As mentioned in this post and others I have read I need to flatten these objects to get access to the look-up relationships.

But I do not know how to implement this code I am new to javascript and have tried implementing some of the solutions using arrow functions but when I log the results undefined is printed.

I am trying to enter the results into a datatable.This is the code I have in my controller and the attribute in my component class which I am using to populate the table as its data.

<aura:attribute name ="Winnings" type="Winning__c"/>
action.setCallback(this, function(response) {
    let state = response.getState();
    if (state === "SUCCESS") {
        console.log(response.getReturnValue());
        
        // some way to flatten the objects
                
        component.set("v.Winnings", response.getReturnValue());
    } else {
        console.log("Failed with state: " + state);
    }
});
$A.enqueueAction(action);   

As I said I dont know javascript to well and how to do this (I need to brush up on my skills) however I can tell you the purpose and what is expected of the function to show I am not just trying to get free code.

I was trying to use an @wire call as this is what I found online most answers where prompting to do but I cant use this call in the Developer Console as it is apart of LWC not aura.

The function should return an array of the JS objects but flattened this would require x amount of new attributes depending on the amount of variables in the look-up relationship which are trying to be accessed in my case it is the name of the contact with the winnings.

[
 {
   "Id": "003a000002PbXXXXXX",
   "Name": "Contact 1",
   "AccountId": "001a000001oeXXXXXX",
   "NewKey": "Winning name contact"
 }
}, 

This would then allow me to access the properties when entering them into the columns for the datatable.

component.set('v.Columns',[
    {
        label:'Name', 
        fieldName:'Name', 
        type:'text'
    },
    { 
        label:'Contact',
        fieldName:'NewKey', 
        type:'text'
    }
]);
3
  • 1
    Welcome to Salesforce Stack Exchange (SFSE)! I don't know the direct answer to your question, but if the quotation marks inside your controller code actually are the ones you used here in this question, that could be your issue. If you copied/pasted the code from that answer or elsewhere, it likely pasted the wrong quotation marks.
    – Moonpie
    Jul 6 at 23:30
  • Yep, they are smart quotes and are not part of the JSON standard. Use "" only. Jul 6 at 23:31
  • This is an X-Y Problem. In order to help you, we'd need to see your JavaScript code. As I alluded to earlier, your data has to be in a specific format. We can help you out better if we could see this code.
    – sfdcfox
    Jul 6 at 23:38

2 Answers 2

3

@wire is an LWC construct. You cannot use it in Aura. You can, however, just call it directly:

({
   doInit: function(component, event, helper) {
     let action = component.get("c.sayHello");
     action.setParams({ name: 'Peter' });
     action.setCallback(function(result) {
       console.log(result.getReturnValue());
     });
     $A.enqueueAction(action);
   }
})

Which you can use the debugger to see what the results are.

1

Yes, you can call a wire method if it's a custom Apex method that you created.

All @AuraEnabled methods are static, so you'd call it like:

String result = YourClass.sayHello('Peter');

Finally, You will definitely need to change those quotes in the @wire method to JSON standard quotes. Use " only.

When you get the results in your LWC controller, they will be in this form:

[
  {
    "Id": "003a000002PbXXXXXX",
    "Name": "Contact 1",
    "AccountId": "001a000001oeXXXXXX",
    "Account": {
      "Name": "Your Account",
      "Id": "001a000001oeXXXXXX"
    }
  },
  {
    "Id": "0035C00000cjeThQAI",
    "Name": "Contact 1",
    "AccountId": "001a000001oeXXXXXX",
    "Account": {
      "Name": "Your Account",
      "Id": "001a000001oeXXXXXX"
    }
  },
  {
    "Id": "003a000002Pc7dTAAR",
    "Name": "Contact 1",
    "AccountId": "001a000001oeXXXXXX",
    "Account": {
      "Name": "Your Account",
      "Id": "001a000001oeXXXXXX"
    }
  }
]

This is for a standard Contact->Account relationship, and as you can see, you will need to parse the data to get it into your datatable.

You COULD just define a couple of calc fields on the contact to short circuit this.

Or you could parse this result in javascript and generate some fake field names eg add another member to each contact - something like:

this.contacts = result.map( contact => {
    contact["AccountName"] = contact?.Account?.Name
    return contact;
});

NOTE the result from the @wire call is likely immutable... I think. Anyway, it's best to assign the result to a completely new object if you want to edit it.

4
  • They are having trouble rendering a field in a datatable--the problem isn't the Apex.
    – sfdcfox
    Jul 6 at 23:39
  • Yep, but that's not what the queston says ;). Anyway, I'm having a look at that in the meantime Jul 6 at 23:49
  • Yeah, it's not their first question. They're trying to figure something out but apparently don't know the right question to ask.
    – sfdcfox
    Jul 6 at 23:52
  • There we go... I think they were just expecting the data to be all nicely formatted as a result of the query. Jul 7 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.