• KevanM
  • NEWBIE
  • 0 Points
  • Member since 2015
  • Salesforce Architect
  • SharinPix


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 2
    Replies
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

 
  • January 14, 2020
  • Like
  • 0
If you have a lightning component on a layout of a subtab in Lightning service console and you change from one primary tab and came back to the previous one, the Lightning component on the layout refreshes by itself. Can we prevent this behaviour? 
My score got reset to zero on trailhead. I had already completed about 47%.
If you have a lightning component on a layout of a subtab in Lightning service console and you change from one primary tab and came back to the previous one, the Lightning component on the layout refreshes by itself. Can we prevent this behaviour? 

Hi,

I'm trying to reproduce in APEX this PHP example :

<?php 
$key
="0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF";
$binkey
= pack("H*", $key);
echo strtoupper
(hash_hmac('sha512',"ABC", $binkey));
?>

 

or this Java example :

privateString generateHMAC(String datas )
   
{

       
//                final Charset asciiCs = Charset.forName( "utf-8" );
       
Mac mac;
       
String result ="";
       
try
       
{
            finalSecretKeySpec secretKey = newSecretKeySpec( DatatypeConverter.parseHexBinary(CONSTANTS.KEY),"HmacSHA512");
            mac
=Mac.getInstance("HmacSHA512");
            mac
.init( secretKey );
           
finalbyte[] macData = mac.doFinal( datas.getBytes());
           
byte[] hex =newHex().encode( macData );
            result
=newString( hex,"ISO-8859-1");
       
}

       
return result.toUpperCase();

   
}

 

This is my APEX code :

private String hmacFunction(String str, String k) {
    Blob mac = Crypto.generateMac('hmacSHA512', Blob.valueOf(str), Blob.valueOf(k));
    return EncodingUtil.convertToHex(mac).toUpperCase();
}

 

This is no good, for the moment : encrypted strings match between Java and PHP, not with APEX.

The result for ABC should be :

100A6A016A4B21AE120851D51C93B293D95B7D8A44B16ACBEFC2D1C9DF02B6F54FA3C2D6802E52FED5DF8652DDD244788A204682D2D1CE861FDA4E67F2792643

 

Obviously, the difficulty lies in the pack('H*, key) conversion which seems to be different from the Blob.valueOf(key) call.

 

 

Can anyone help ?

 

Rup