0

I have the following Apex Method which is called in the Experience Cloud under unauthenticated guest user:

@AuraEnabled(cacheable=true)
public static string test(String ref) {
    try {
        Some_Object__c o = [Select Id, Blah__c from Some_Object__c where Blah__c =: ref];
        return o.Id;
    } catch (Exception e) {
        System.debug(e.getMessage());
        throw new AuraHandledException(e.getMessage());
    }
}

Which is called by this LWC:

import { LightningElement, wire, track } from 'lwc';
import { CurrentPageReference } from "lightning/navigation";
import getTest from "@salesforce/apex/SomeController.test";
export default class HelloWorld extends LightningElement {
    @track ref;
    @track blah;

    @wire(CurrentPageReference)

    getStateParameters(currentPageReference) {
        if (currentPageReference) {
            const urlValue = currentPageReference.state.ref;
            if (urlValue) {
                this.ref = urlValue;
            }
        }
    }

    connectedCallback(){
        this.loadData;
    }

    get loadData(){
         getTest({ref: this.ref})
            .then(result => {
                this.blah = result;
            })
            .catch(error => {
                console.log(error);
            })
     }
}

The important part of this code is the loadData() method.

Calling SomeController.Test() from this LWC results in List has no rows for assignment to SObject. If I run this query in the developer console, I can see the value I'm passing in is there (and matches). If I run this same method anonymously, I recieve the correct result.

The parameter is populated (with some debug logs I can see it's there). If I attempt to return all rows, the list has a size of 0 (like so: List<Focus_Application__c> applications = [Select Id, Reference_Id__c from Focus_Application__c];). I've triple checked that the custom object is definitely populated in this org.

This perhaps points to permissions on the object? But I can't see anything out of place.

3
  • 1
    why are you using a getter??
    – glls
    Nov 23 '21 at 13:29
  • I'm new to LWC development, is this not the recommended approach?
    – Jessica
    Nov 23 '21 at 13:57
  • For reference, this is why I didn't use a wire: salesforce.stackexchange.com/a/321023/97550 - I have to call this after the initial url parameter has been read.
    – Jessica
    Nov 23 '21 at 14:15
2

Don't use a getter for this purpose. They can, and will, be called multiple times in a semi-unpredictable order. You can call the method directly from your wire handler:

@wire(CurrentPageReference)
getStateParameters(currentPageReference) {
    if (currentPageReference) {
        const urlValue = currentPageReference.state.ref;
        if (urlValue) {
            this.ref = urlValue;
            getTest({ref: this.ref})
            .then(result => {
                this.blah = result;
            })
            .catch(error => {
                console.log(error);     
            })
        }
    }
}
1
  • Thanks for the tip, I'll keep this in mind and will refactor my code. Unfortunately this doesn't solve the wider problem of not having access to the custom object.
    – Jessica
    Nov 23 '21 at 15:31
1

The problem is with Guest User access.

The topic started has deleted her answer:

This was due to field level security on the guest user:

To fix:

  1. Click the gear icon, then Setup.
  2. Go to Digital Experiences > All Sites > Workspaces.
  3. View the guest profile for this site.
  4. Administration > Pages > Go to Force.com > Public access settings.
  5. Scroll down to field level security.
  6. Add access to the specific fields you need.

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.