function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KevanMKevanM 

LWC public property not working in Community

Hello,

I am encountering an issue with LWC where is it working fine on a record page in Lightning but does not work on Community. 

I am using a public api and in the design property, I am using a datasource which is basically looking for all STRING field on the current object. On a record page in lightning, if I am on an account, it is working fine and the entity context is returning 'Account' but when I am on community and try to add the LWC on the record page, it is returning a null entity context.

Below is a simple lwc to reproduce this issue. 

Drag and drop on an Account page in lightning, in the design you should be able to see a list of field. Do same on community, no field will appear and you will have an error in the debug. Attempt to de-reference a null object on this line : dynamicPicklistValues = new DynamicPicklistValues(String.valueOf(context.entityName), 'STRING');
 
public with sharing class DynamicPicklistValues {

    private Map<String, String> fieldNamesToLabels;
    private String defaultField;

    public DynamicPicklistValues(String objectApiName, String fieldType) {
        Map<String, Schema.SObjectField> objectFieldsDescribe = Schema.getGlobalDescribe().get(objectApiName).getDescribe().fields.getMap(); 
        defaultField = null;
        fieldNamesToLabels = new Map<String, String>();
        
        for (Schema.sObjectField fieldObject: objectFieldsDescribe.values()) {
            Schema.DescribeFieldResult fieldDescription = fieldObject.getDescribe();
            if (fieldType(fieldDescription) == fieldType) {
                fieldNamesToLabels.put(fieldDescription.getName(), fieldDescription.getLabel());
                if (String.isBlank(defaultField)) {
                    defaultField = fieldDescription.getName();
                }
            }
        }
    }

    public VisualEditor.DataRow getDefaultValue() {
        VisualEditor.DataRow defaultValue = new VisualEditor.DataRow('', '');
        if (!String.isBlank(defaultField)) {
            defaultValue = new VisualEditor.DataRow(defaultField, defaultField);
        }
        return defaultValue;
    }

    public VisualEditor.DynamicPickListRows getValues() {
        VisualEditor.DynamicPickListRows pickListRows = new VisualEditor.DynamicPickListRows();
        for (String fieldApiName : fieldNamesToLabels.keySet()) {
            pickListRows.addRow(new VisualEditor.DataRow(fieldApiName, fieldApiName));
        }
        return pickListRows;
    }

    private string fieldType(Schema.DescribeFieldResult fieldDescription) {
        if (String.valueOf(fieldDescription.getType()) == 'string' && !fieldDescription.isCalculated()) { return 'STRING'; }
        if (String.valueOf(fieldDescription.getType()) == 'url') { return 'URL'; }
        if (String.valueOf(fieldDescription.getType()) == 'textarea' && fieldDescription.getLength() >= 256 && fieldDescription.isHtmlFormatted()) { return 'RICHTEXT'; }
        return null;
    }
}

public with sharing class FieldPicklistDatasource  extends VisualEditor.DynamicPickList {

    private DynamicPicklistValues dynamicPicklistValues;
    private VisualEditor.DynamicPickListRows pickListRows;
    private List <VisualEditor.DataRow> dataRows;
    private VisualEditor.DataRow defaultValue;

    public FieldPicklistDatasource(VisualEditor.DesignTimePageContext context) {
        defaultValue = new VisualEditor.DataRow('None', 'None');
        dynamicPicklistValues = new DynamicPicklistValues(String.valueOf(context.entityName), 'STRING');
        pickListRows = dynamicPicklistValues.getValues();
        dataRows = pickListRows.getDataRows();
        pickListRows = new VisualEditor.DynamicPickListRows();
        pickListRows.addRow(defaultValue);
        for (VisualEditor.DataRow dataRow : dataRows) {
            pickListRows.addRow(dataRow);
        }
    }

    public override VisualEditor.DataRow getDefaultValue() {
        return defaultValue;
    }

    public override VisualEditor.DynamicPickListRows getValues() {
        return pickListRows;
    }
}
 
// LWC SimpleButton
-------------------------------TEMPLATE--------------------------------------------------

<template>
    <button class="slds-button slds-button_brand slds-button_stretch slds-p-vertical_xx-small">{selectedField}</button>
</template>

---------------------------------CONTROLLER-------------------------------------------------

import { LightningElement, api } from 'lwc';

export default class SimpleButton extends LightningElement {

    @api selectedField
}

---------------------------------DESIGN-------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>47.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__RecordPage, lightningCommunity__Default">
            <property name="selectedField" label="Select a field" description="" type="String" datasource="apex://FieldPicklistDatasource"/>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

If anyone has any idea on how this can be resolve, let me know in the comment. 

Regards,
Kevan

 
Alina BalanAlina Balan
Did you find any fix for it ? We are facing the same issue, context is null on community pages. Thank you !
Beth EvansBeth Evans
Any update for a fix on this? I have the same issue.
Nicolas SilaNicolas Sila
The property name must be all lowercase to be accepted on the community page (ex: selectedField must be selectedfield)