0

I have the following query in a remote action method being called from my visual force page:

List<Contact> contactList = [SELECT Id, Name, Email FROM Contact WHERE Id = :nameQuery];

Where nameQuery is the parameter to the method and it is 0034100000wo3gcaab according to debug log...

When I put that query into the query editor in the dev console, I get one result back as expected. When I run it on my Visual Force page, I don't get anything returned, and the debug log shows that no rows came back from the query...

I don't get what is wrong with the query... How can I search contacts based on their Id if the above query doesn't work in apex?

Edit: (Adding More Code)

VisualForce page calling the remote method, and using the results to update the dataSource of a kendo Widget.

if (e.filter != undefined && e.filter != null) {
                        if (e.filter.value.length >= 3) {
                            console.log(e.filter.value);
                            Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.MeetingRequestController.getContactsAndLeads}', e.filter.value, function(result, event) {
                                result = he.decode(result);
                                console.log(result);
                                var resultArray = JSON.parse(result);
                                //console.log(e);
                                console.log(resultArray);
                                for (var i = 0; i < resultArray.length; i++) {
                                    var person = resultArray[i];

                                    var index = clientSearch.findIndex(function(e) {
                                        if (e.userId == person.userId) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    });
                                    if (index == -1) {
                                        clientSearch.push(person);
                                    }
                                }
                                e.sender.dataSource.data(clientSearch);
                            });
                        }
                    }

My remote action method in my controller, getting all leads and contacts that match the search criteria, which I want to be either a name or an Id

@RemoteAction
global static String getContactsAndLeads(String nameQuery){

    system.debug(nameQuery);
    //nameQuery = nameQuery + '*';
    String wcNameQuery = '%'+nameQuery+'%';
    //system.debug(nameQuery);
    eventResource contactLead = new eventResource();
    List<eventResource> contactsLeadsList = new List<eventResource>();

    //Get list of all contacts

    List<Contact> contactList = [SELECT Id, Name, Email, Account.Name FROM Contact WHERE Name LIKE :wcNameQUery OR Id = :nameQuery];
    //Loop through each contact
    for(Integer i = 0; i < contactList.size(); i++){
        //Set up contactLead, which is an eventOwner object, with data from each contact, if the contact has a name.
        if(contactList[i].Name != null){
            contactLead.name = contactList[i].Name;
            contactLead.alias = contactList[i].Name;
            contactLead.descriptor = contactList[i].Account.Name;
            contactLead.searchName = contactList[i].Name + contactList[i].Id;
            contactLead.userId = contactList[i].Id;
            contactLead.color = '#55b14e';
            contactLead.resourceType = 'Contact';
            contactLead.email = contactList[i].Email;
            //Add contactLead to our list of contacts and leads
            contactsLeadsList.add(contactLead);
        }
        //Set contactLead to a new eventOwner object
        contactLead = new eventResource();
    }

    //Get list of all Leads

    List<Lead> leadList = [SELECT Id, Name, Email, Company FROM Lead WHERE Name LIKE :wcNameQuery OR Id = :nameQuery];
    //Loop through each Lead
    for(Integer i = 0; i < leadList.size(); i++){
        //Set up contactLead, which is an eventOwner object, with data from each lead, if lead has a name
        if(leadList[i].Name != null){
            contactLead.name = leadList[i].Name;
            contactLead.alias = leadList[i].Name;
            contactLead.descriptor = leadList[i].Company;
            contactLead.searchName = leadList[i].Name + leadList[i].Id;
            contactLead.userId = leadList[i].Id;
            contactLead.color = '#c13e3e';
            contactLead.resourceType = 'Lead';
            contactLead.email = leadList[i].Email;
            //Add contactLead to our list of contacts and leads
            contactsLeadsList.add(contactLead);
        }
        //Set contactLead to a new eventOwner object
        contactLead = new eventResource();
    }

    return JSON.serialize(contactsLeadsList);
}
  • Can you add a little more code so that I can see what your visualforce page is doing and what the rest of your code is doing. – Dan Kowalski Aug 31 '17 at 19:14
  • Edited to show relevant code – Tyler Dahle Aug 31 '17 at 19:19
  • Are you logged in as the same user during all the testing? – Jeff Bennett Aug 31 '17 at 20:38
  • Yes I am, with admin privileges. – Tyler Dahle Aug 31 '17 at 20:50
  • Doing a Name LIKE :wcNameQuery where clause works fine, I can type in the name and get results. But Id = :nameQuery where nameQuery is an Id returns nothing... – Tyler Dahle Aug 31 '17 at 20:51
0

Try casting your variable "nameQuery" as an Id before using it in your query:

Id nameQueryId = ( Id )nameQuery;

Then your query might work better. Also consider sharing for Contacts. does your controller run "with sharing"? possibly if you are not the owner it will not show - even with your admin privileges. try also modifying the class to be "without sharing" to test your output and see if you get any results.

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.