1

I'm trying to load a FieldSet in component's JS file below is the code:

import {LightningElement, api, track} from 'lwc';
import CREATECONTACTSET from '@salesforce/schema/Contact/FieldSet.CREATE_CONTACT_MODAL';

I have also tried

import {LightningElement, api, track} from 'lwc';
import CREATECONTACTSET from '@salesforce/schema/Contact.FieldSet.CREATE_CONTACT_MODAL';

I have looked around for a bit but I haven't come across anything relevant, I'm beginning to think this is not possible.

  • While I am not sure if this is possible or not, but if not, you can instead use Apex Wiring here to load the field set and return as a response? – Jayant Das Feb 21 '19 at 14:55
  • @JayantDas I thought of something similar just seems like too much work when you are dealing with tons of fieldSet. – d_k Feb 21 '19 at 15:01
  • If its not documented, then you won't be able to use it this way. And that may be your only option of going apex route. – Jayant Das Feb 21 '19 at 15:02
2

Going through the documentation, it doesn't seem you can import a FieldSet the way you are attempting to. And if its not documented, its very unlikely that it will ever work.

Import references to Salesforce objects and fields from @salesforce/schema

And the only available syntax in the documentation are as below, where you can only refer an object or a field.

// Syntax
import objectName from '@salesforce/schema/objectReference';
import fieldName from '@salesforce/schema/object.fieldReference';

Your option here would be to utilize an Apex and return the required field set details as response.

  • I guess apex is the only way to achieve this but you can't return a FieldSet directly in Lightning so might have to create a wrapper for FieldSet. – d_k Feb 22 '19 at 4:15
  • The idea is to utilize Apex, and if you cannot return field set directly then yes, you will need to utilize a custom wrapper class or a collection of string or so. – Jayant Das Feb 22 '19 at 13:34
0

As mentioned in the previous responses, fieldset isn't supported in LWC yet.However, it can be accessed using an Apex Class. Below is one example of the apex class which can be used.

    public without sharing class MyFieldSetApexClass {

    public MyFieldSetApexClass() {
    }

    public static List<Schema.FieldSetMember> getFields() {
        return SObjectType.Account.FieldSets.examplefieldset.getFields();
    }

    @AuraEnabled(cacheable=true)
    public static Account getAccountList(id accid) {
        String query = 'SELECT ';
        for(Schema.FieldSetMember f : MyFieldSetApexClass.getFields()) {
            query += f.getFieldPath() + ', ';
        }
        query += 'Id FROM Account WHERE ID=\''+accid+'\'';
        System.debug(query);
        return Database.query(query);
    }
}

and the below is the example of the js file which utilises the MyFieldSetApexClass class

import { LightningElement, wire, track, api } from 'lwc';
import accFieldSet from '@salesforce/apex/MyFieldSetApexClass.getAccountList';

export default class MyFielSetComponent extends LightningElement {
    @track accs;
    @track error;
    @api recordId;

    @wire(accFieldSet, { accid: '$recordId' })
    wiredContacts({ error, data }) {
        if (data) {
            this.accs = data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.accs = undefined;
        }
    }
}

Below is the example HTML code.

<template>
    <lightning-card title="AccountFieldSet" icon-name="custom:custom63">
        <div class="slds-m-around_medium">
            <template if:true={accs}>
                <div class="slds-box slds-box_x-small">
                    <p>Name: {accs.Name}</p>
                    <p>Industry: {accs.Industry}</p>
                    <p>Description: {accs.Description}</p>
                </div>
            </template>
            <template if:true={error}>
                <c-error-panel errors={error}></c-error-panel>
            </template>
        </div>
    </lightning-card>
</template>

This is just an example to support the previous responses. Hope this helps. :)

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.