0

How to show this elements in html. I am new in salesforce and programming

public with sharing class StockMarketSharesWrapper {
    @AuraEnabled(cacheable=true)

    public static List<Wrapper> getAllCompanys(){
        // Define the list
        List<Wrapper> com = new List<Wrapper>(); 

        // Create account sObjects
        Wrapper w = new Wrapper();
        w.Ticker = 'U';
        w.Name = 'Unity Software Inc';
        Wrapper w1 = new Wrapper();
        w.Ticker = 'UAL';
        w.Name = 'United Airlines Holdings, Inc.' ;
        Wrapper w2 = new Wrapper();
        w.Ticker ='DAOOU' ;
        w.Name = 'Crypto 1 Acquisition Corp Unit';
        Wrapper w3 = new Wrapper();
        w.Ticker = 'GLLIU';
        w.Name ='Globalink Investment Inc. Unit' ;

        // Add accounts to the list
        com.add(w);
        com.add(w1);
        com.add(w2);
        com.add(w3);

        return com;
    }

    public class Wrapper{
        @AuraEnabled
        public String Name {get; set;}
        @AuraEnabled
        public String Ticker {get; set;}
    }
}

I want to show the list of "//Create account sObjects" in HTML with wrapper how should I do it?


UPDATE I tried:

APEX

public with sharing class StockMarketSharesWrapper {   
    @AuraEnabled(cacheable=true)
    public static List<Wrapper> getAllCompanys(){
        // Define the list
        List<Wrapper> com = new List<Wrapper>(); 

        // Create account sObjects
        Wrapper w = new Wrapper();
        w.Ticker = 'U';
        w.Name = 'Unity Software Inc';
        Wrapper w1 = new Wrapper();
        w1.Ticker = 'UAL';
        w1.Name = 'United Airlines Holdings, Inc.' ;
        Wrapper w2 = new Wrapper();
        w2.Ticker ='DAOOU' ;
        w2.Name = 'Crypto 1 Acquisition Corp Unit';
        Wrapper w3 = new Wrapper();
        w3.Ticker = 'GLLIU';
        w3.Name ='Globalink Investment Inc. Unit' ;

        // Add accounts to the list
        com.add(w);
        com.add(w1);
        com.add(w2);
        com.add(w3);

        return com;
    }

    public class Wrapper{
        @AuraEnabled
        public String Name {get; set;}
        @AuraEnabled
        public String Ticker {get; set;}
    } 
}

JS

import { wire, LightningElement } from 'lwc';
import getAllCompanys from '@salesforce/apex/StockMarketSharesWrapper.getAllCompanys';

export default class wrapperList extends LightningElement {
  @wire(getAllCompanys, {}) companys;
  columns = [{ label: 'Name', fieldName: 'Name' }, { label: 'Ticker', fieldName: 'Ticker' }]
}

JS v2

    Ticker= '';
    Name='';

    @wire(getAllCompanys, { })
    fetchCompanies(data, error){
        alert('is in ????');
        if (error){
            this.Ticker= JSON.stringify(error);
            this.Name= JSON.stringify(error);
            return;
        }
        if(data.data){
            this.Ticker =data.data.Ticker;
            this.Name =data.data.Name
        }
        
    }

HTML

<template>
    {Ticker}
    {Name}
    {Companys}
  </template>

nothing works ;/

1
  • 1
    Welcome to Salesforce Stack Exchange (SFSE)! Among other issues with your code, which hopefully someone with more time at the moment can address, please note that the code keeps overwriting the values of w.Ticker and w.Name - you do not use w1, w2 or w3 after you create them.
    – Moonpie
    Commented Dec 16, 2021 at 13:00

2 Answers 2

1

There's an example of this in the lwc-recipes repository published by salesforce.com.

Your JS should look like:

import { wire, LightningElement } from 'lwc';
import getAllCompanys from '@salesforce/apex/StockMarketSharesWrapper.getAllCompanys';

export default class MyLwcComponent extends LightningElement {
  @wire(getAllCompanys, {}) companys;
  columns = [{ label: 'Name', fieldName: 'Name' }, { label: 'Ticker', fieldName: 'Ticker' }]
}

And your HTML can look like:

<template>
  <lightning-datatable data={companys.data} columns={columns} key-field="Name">
  </lightning-datatable>
</template>

See also the lightning-datatable documentation.

Edit: Your Apex is reusing the same variable, despite creating new wrappers; this is why most of your data would be blank.

    Wrapper w = new Wrapper();
    w.Ticker = 'U';
    w.Name = 'Unity Software Inc';
    Wrapper w1 = new Wrapper();
    w1.Ticker = 'UAL';
    w1.Name = 'United Airlines Holdings, Inc.' ;
    Wrapper w2 = new Wrapper();
    w2.Ticker ='DAOOU' ;
    w2.Name = 'Crypto 1 Acquisition Corp Unit';
    Wrapper w3 = new Wrapper();
    w3.Ticker = 'GLLIU';
    w3.Name ='Globalink Investment Inc. Unit' ;

Edit 2: There was a .data missing in the template. I build the entire thing in to a gist.

10
  • I tryed copy paste your code from js and html but that is not working. Do i have correct writed my apex class ?? What schould I do ?
    – 1skora
    Commented Dec 16, 2021 at 15:02
  • @1skora As commented on earlier by Moonpie, you used w for all your assignments, instead of w1, w2, etc. I'd expect your current output to have several blank lines where data should be.
    – sfdcfox
    Commented Dec 16, 2021 at 15:05
  • 1
    @1skora Fret not; we all started out at the beginning. Would you like to send me a copy of your code by email, I can review this offline, or you can edit your code into the question, I'll take a look at it for you.
    – sfdcfox
    Commented Dec 16, 2021 at 15:33
  • 1
    @1skora wrapperList is incorrect; the first letter of the class name must be uppercase: WrapperList. Aside from that, the first version should work. Give me a few minutes, I'm writing up a full example for you.
    – sfdcfox
    Commented Dec 16, 2021 at 15:45
  • 1
    @1skora Was missing a few characters from the template. I made a gist for you, this should set you straight. Also, it should be noted that you had a number of technical problems in your code, which is why you were having a hard time of it. You were returning a list, but trying to use it as if it were a single variable. Your wire handler also read (data, error) instead of ({ data, error }). Don't worry, you'll get it one day, it just takes some practice.
    – sfdcfox
    Commented Dec 16, 2021 at 16:12
-2

You have to stringify the data in Apex and return to js side and then store it to object and iterate that object in html

1
  • 1
    No, you don't have to stringify/serialize the data.
    – sfdcfox
    Commented Dec 16, 2021 at 13:09

You must log in to answer this question.

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