1

I am trying to pass a parameter (a record id) from a Salesforce Site URL to an embedded Lightning Web Component to create a pre-filled web form for the end user to access once they click on the unique site link. While I am able to display the recordId in the LWC's HTML markup (using the @api decorator), I am unable to "fully access" it in order to query the record and display its values. The "LWC:else" conditional logic is triggered each time in the HTML file. I am using the standard "getRecord" wire adapter to retrieve the record. To that end, I tried hard-coding the actual record id I want to query and still no results were returned, which was very unexpected. I made sure the "site profile" associated with the guest user of the Salesforce site has access to the Visualforce page that is embedded on the Salesforce site and has full access to the custom object itself. Does any additional code need to be added to the JS file, or is there perhaps an inherent limitation with what I'm trying to achieve? Thank you in advance for any assistance.

Visualforce Page

<apex:page sidebar="false" showHeader="false" applyHtmlTag="false" lightningStylesheets="true">
    the edited url: https://xxxxx.sandbox.my.salesforce-sites.com/oba?Id=a3AEa0000001MsvMAE <br/>
    from visualforce: {!$CurrentPage.parameters.Id}
    <apex:includeLightning />
       <div id="compContainer" />
        <script>
            $Lightning.use('c:obaApp', function() {
                $Lightning.createComponent(
                    "c:obaForm",
                    {
                        Id: "{!$CurrentPage.parameters.Id}"
                    },
                    "compContainer",
                    function(cmp) {
                        console.log('{!$CurrentPage.parameters.Id}');
                    }
                );
            },
         'https://xxxxx.sandbox.my.salesforce-sites.com/oba/'  // Edited Site endpoint             
          );
        </script>
</apex:page>

Aura Application

<aura:application access="GLOBAL" extends="ltng:outApp"
    implements="ltng:allowGuestAccess">
<aura:dependency resource="c:obaForm"/></aura:application>

LWC JS File

import { LightningElement, api, wire, track } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
//import NAME_FIELD from '@salesforce/schema/Outside_Business_Activity__c.Name';
import getObaForm from '@salesforce/apex/outsideBusinessActivity_Handler.getObaForm';


const FIELDS = ['Outside_Business_Activity__c.Name']
    
export default class ObaForm extends LightningElement {
    @api Id;

    obaForm;
    
    /*@wire(getRecord, {recordId: '$Id', fields: FIELDS})
    handleObaFormWire({error,data}){
      if (data){
        console.log('data:'+data);
        return;
      } else if(error){
          console.log('error: '+error);
          return;
        } 
    }*/ //Commented out to test the button click 

    showRecord() {
      getObaForm({recordId: '$Id'})
      .then((result) => {
        this.obaForm = result;
        this.error = undefined;
        console.log('result: '+result);
      })
      .catch((error) => {
        this.error = error;
        this.obaForm = undefined;
        console.log('error: '+error);
      });
    }

    /*get obaName() {
      if(this.connected == true) {
        return this.obaForm.data ? getSObjectValue(this.obaForm.data, NAME_FIELD) : '';
      }
  }*/
}

LWC HTML File

<template>
    <p>from lwc: {Id}</p>

    <br/>

    <div class="slds-var-m-around_medium">
        <lightning-button label="Show OBA Form" onclick={showRecord}></lightning-button>
    </div>

    <div lwc:if={obaForm}> 
        <p>connected: <lightning-formatted-text value={obaName} ></lightning-formatted-text></p>
    </div>
    <div lwc:else>
        obaForm.data is not shown.
    </div>
</template>

Apex Class

public class outsideBusinessActivity_Handler {
    @AuraEnabled(cacheable=true)
    public static Outside_Business_Activity__c getObaForm(String recordId) {

        Outside_Business_Activity__c obaForm = [SELECT Id, Name
                                                            FROM Outside_Business_Activity__c
                                                           WHERE Id = :recordId];
        return obaForm;

    }
}
1
  • Doesn't the application itself need to expose an aura attribute to then pass to the LWC? Commented Jul 16, 2023 at 23:46

1 Answer 1

0

This is more a way of debugging than a full answer, but if you use a method handler for your wire adaptor, you will be able to display the error you are getting.

@wire(getRecord, {recordId: '$Id', fields: NAME_FIELD})
handleObaFormWire({error,data}){
  if (error){
    console.log(error);
    return;
  } 
  this.obaForm["data"] = data;
}

Because the handler destructures the result into data and error you can actually just save your data object and directly reference that in your getters.

4
  • Thank you for the debugging tip. This is the error message that is getting returned: "This page has an error. You might just need to refresh it. Error in $A.getCallback() [Error during LWC component connect phase: [Cannot read properties of undefined (reading 'data')]] Failing descriptor: {ltng:outApp}"
    – Mike
    Commented Jul 17, 2023 at 0:14
  • Have you defined class member - obaForm = {"data":{}};. Also, try disabling the getFormName method as it could be causing the error. Commented Jul 17, 2023 at 0:53
  • Thank you, I was able to render the site page without errors, however the "getRecord" wire adapter still was not returning a record. To troubleshoot, I added a lightning-button to the HTML file and created/imported an Apex Class that retrieves the record when the button is clicked. When the button is clicked, Chrome returns "error: [object Object]" in the console. I setup a debug log on the site user and the Apex method is returning "System.QueryException: List has no rows for assignment to SObject." Even after I hardcode the recordId in the SOQL, the system error still appears. I'm stumped.
    – Mike
    Commented Jul 17, 2023 at 3:23
  • Try defining your class as public without sharing class Commented Jul 17, 2023 at 7:04

You must log in to answer this question.

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