2

I have created an Apex class with an SOQL query to get OpportunityLineItems. Testing the SOQL in query editor returns the expected response. All of the fields render in the LWC data table except for Product2.Name.

@AuraEnabled(Cacheable = true)
public static List<OpportunityLineItem> getOpportunityLineItem(){
    return [SELECT Id, Product2Id, Product2.Name, Quantity, UnitPrice, ProductCode FROM OpportunityLineItem];
}

Columns from LWC


    const columns = [
    {
        label: 'Product Name',
        fieldName: 'product2.Name',
        type: 'text',
        cellAttributes: { alignment: 'left' }


    }, {
        label: 'Quantity',
        fieldName: 'Quantity',
        type: 'number',
        editable: true,
        cellAttributes: { alignment: 'left' }

    }, {
        label: 'Sales Price',
        fieldName: 'UnitPrice',
        type: 'currency',
        editable: true,
        cellAttributes: { alignment: 'left' }
    }, {
        label: 'Product Code',
        fieldName: 'ProductCode',
        type: 'text',
        cellAttributes: { alignment: 'left' }

    }

];
2
  • Thanks it looks like this is the same type of question. So far I have not been able to successfully map the field.
    – Jordan
    Nov 15, 2019 at 16:54
  • I can't remember if data-table is case-sensitive, try fieldName: 'Product2.Name', Nov 15, 2019 at 16:59

2 Answers 2

3

What you can do is to transform the resulting list of OpportunityLineItem into another array of objects using the map function like below.

@track error;
@track records;

@wire(getOpportunityLineItem)
    wiregetOpportunityInfo(result) {
        const { data, error } = result;
        if(data) {
            this.records= data.map(row => { 
                return {...row, productName: row.product2.Name } 
            })
            this.error = null;
        }
        if(error) {
            this.error = error;
            this.records= [];
        }
   }

@track columns = [
        {
            label: 'Product Name',
            fieldName: 'productName',
            type: 'text',
            cellAttributes: { alignment: 'left' }


        }, 
        {
            label: 'Quantity',
            fieldName: 'Quantity',
            type: 'number',
            editable: true,
            cellAttributes: { alignment: 'left' }

        }, 
        {
            label: 'Sales Price',
            fieldName: 'UnitPrice',
            type: 'currency',
            editable: true,
            cellAttributes: { alignment: 'left' }
        }, 
        {
            label: 'Product Code',
            fieldName: 'ProductCode',
            type: 'text',
            cellAttributes: { alignment: 'left' }

        }

    ];

the html file

<template>
    <template if:true={records}>
        <lightning-datatable key-field="id" data={records} columns={columns} onrowaction={rowAction}
            hide-checkbox-column="true" resize-column-disabled="true">
        </lightning-datatable>
    </template>
</template>

2

You cannot access the fields of related objects by traversing with . on the JS side. You can just use Opportunity Product's / Line Item's name, by just using Name instead.

{
    label: 'Product Name',
    fieldName: 'Name',
    type: 'text',
    cellAttributes: { alignment: 'left' }
}
5
  • Thanks for your answer. Simply using name doesn’t work since the value in the SOQL query is Product2.Id. Name is not a field on OpportunityLineItem so I am not a able to query this.
    – Jordan
    Nov 15, 2019 at 16:51
  • 1
    You are querying OpportunityLineItem which has Opportunity Product Name field name with Name API name. If your Opportunity Product's name is same or similar as Product, you can use this field instead.
    – Atlas Can
    Nov 15, 2019 at 16:53
  • Ok. I have now tried this. But name is different so I will need to use Product2.Name. :/
    – Jordan
    Nov 15, 2019 at 17:17
  • Try creating a formula field on Opportunity Product with this formula PricebookEntry.Product2.Name And use this formula field in your query and put the column name as this formula field. Let me know if your problem is solved.
    – Atlas Can
    Nov 15, 2019 at 17:21
  • Thanks. That would work. But, I am going to first try and solve this without needing to create a new field.
    – Jordan
    Nov 15, 2019 at 17:47

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