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
Mahesh GorrepatiMahesh Gorrepati 

Can we invoke a apex class from experience site used in LWC component?

Query:-
 1. We can invoke only method with @WIRE Decorator.
  2. IMperative method invoke is not passible directly

I got the  information using chatGPT


you cannot directly invoke an Apex class from an Experience Site in a Lightning Web Component (LWC). Experience Sites are designed to provide a public-facing website experience for external users and do not have direct access to Apex code.
However, you can leverage Salesforce APIs to interact with your Apex code indirectly. You can create a custom Apex REST or SOAP web service that encapsulates the functionality you want to invoke. Then, from your LWC component hosted on the Experience Site, you can make HTTP requests to that custom web service to perform the desired actions.
Here's a general outline of the steps involved:
Create a custom Apex class that exposes the desired functionality as a REST or SOAP web service.
Configure the necessary access permissions for the custom web service to be accessible from the Experience Site's guest user profile.
In your LWC component, use the standard fetch() or XMLHttpRequest API to make an HTTP request to the custom web service endpoint.
Handle the response from the web service in your LWC component and update the UI accordingly.
By following this approach, you can indirectly invoke the functionality of your Apex class from the LWC component hosted on the Experience Site.

Kindly Help Here

 
Anne ToneyAnne Toney
Hello,

No, you cannot directly invoke an Apex class from an Experience Site in a Lightning Web Component (LWC). However, you can create a custom Apex REST or SOAP web service that encapsulates the functionality you want to invoke and then make HTTP requests to that service from your LWC component. This allows you to indirectly interact with your Apex code. (https://www.targetpayandbenefits.us/)




 
Mahesh GorrepatiMahesh Gorrepati
Thank your response
Is  it  possible in Lighntning Aura Components or not ? 
Arun Kumar 1141Arun Kumar 1141
Hi Mahesh,

yes, you can invoke an apex class from experience site. this code will help you.

Using Wire Method-->
Apex Class----->
public class TestData{
        @AuraEnabled(cacheable=true)
        public static Contact retrieveContact(){
            Contact mycontactList = [Select LastName From Contact limit 1];
            return mycontactList;
        }
    }

LWC JS part---->

   import { LightningElement, wire, api,track } from 'lwc';
import getData from '@salesforce/apex/TestData.retrieveContact';
export default class TestComponent extends LightningElement {
    @track invokedData;
         @wire(getData) wiredData ({ error, data }) {
       if (data) {
           this.invokedData = JSON.stringify(data.LastName); 
      } else if (error) { 
          this.error = error;  
     }   }
}

LWC HTML part---->

<template>
  <lightning-card title="Invoke Data">
   <h1>{invokedData}</h1>
  </lightning-card>
</template>
Using Imperative Method-->
Apex Class----->
public class TestData{
        @AuraEnabled(cacheable=true)
        public static Contact retrieveContact(){
            Contact mycontact = [Select LastName From Contact limit 1];
            return mycontact;
        }
    }

LWC JS part---->

   import { LightningElement, wire, api,track } from 'lwc';
import getData from '@salesforce/apex/TestData.retrieveContact';
export default class TestComponent extends LightningElement {
    @track invokedData;
      handleClick(){
       getData()
       .then(result => {
               this.invokedData = JSON.stringify(result.LastName);
               console.log("this.invokedData--"+this.invokedData);
       })
       .catch(error => {
           console.log('Error:- '+error.body.message);
       });
   }
}

LWC HTML part---->

<template>
  <lightning-card title="Invoke Data">
    <lightning-button
      variant="brand"
      label="Invoke" 
      onclick={handleClick}
    >   
</lightning-button>
   <h1>{invokedData}</h1>
  </lightning-card>
</template>

please mark as the best answer if this will be helpfull.
Thanks
 
Mahesh GorrepatiMahesh Gorrepati
Hii,
Thank you  for your response
i have query with this approach
1. Are you added this component to the Digital Experience site 
2. USing wire decorator ,it is invoking the apex class
3. But, using the Imperative method it is nort invoking the apex class.
if you done, kindly share  the site URL and make the class can be invoke by the guest user profile in DIgital experience
kindly check this. and update me
 It will be very helpful for me