0

I've read all the documentation I could find, and tried several variations of code, but nothing has produced results. I'm working on a pre-chat form for Live Chat, and what I want to do is search all existing contacts to see if any match an email. If not, I'll then search for contacts that match the last name, and instead of creating a new contact, show the agent a list of the contacts that match. If there is only 1 match, I will assign that contact to the new case. If no contacts match, I'll create a new one.

First, here's the code I am testing with (which for now just returns the first 5 contacts for testing purposes):

Apex Class:

global with sharing class RemoteController {
    public RemoteController() {}
    
    @RemoteAction
    global static Contact[] getContacts() {
        Contact[] contacts = [SELECT Email FROM Contact LIMIT 5];
        
        return contacts;
    }
}

Apex Page:

<apex:page showHeader="false" standardStylesheets="false" sidebar="false" docType="html-5.0" controller="RemoteController">
    <!-- ... Pre-Chat form page content ... -->
  
    <!-- Search contacts -->
    <script>
        function searchContacts() {
            Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.RemoteController.getContacts}', function(result, event) {
              if (event.status) {
                console.log("Result:", result);
              }
            }, { buffer: false, escape: true, timeout: 30000 });
        }
        
        // DEBUG: Run it right away for now.
        searchContacts();
    </script>
</apex:page>

So what I should see in the JS console is an array of 5 contacts, but what I actually see is an empty array. I tried this in the Developer Console on the Salesforce side, by running the same SQL query that I have in the controller, and I get 5 results. So why am I not getting the same results when I use JS Remoting from the pre-chat form?

3
  • Did you try by removing 'With sharing' and using 'without sharing' keyword on the class. I am assuming its a guest user which is running the code on the prechat form and it might not have access to those contact records. – maniac coder Sep 3 '20 at 2:00
  • THANK YOU!!!!! Can you please add this as an answer so I can mark it accepted?! – Justin Szczurowski Sep 4 '20 at 15:08
  • I have added the comment as answer – maniac coder Sep 5 '20 at 2:45
0

Can you please try by removing 'With sharing' and using 'without sharing' keyword on the class. I am assuming its a guest user which is running the code on the prechat form and it might not have access to those contact records.

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.