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
NishanNishan 

Custom list button for searching records

Hi Everyone,
                      I am creating a custom list button for an object. The record Id is entered in the prompt window after the button is pressed. Is it possible to display the result in the default salesforce list itself using javascript? If not how can I display the results since visualforce is not enabled in Force.com ?

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

Hi Nishan,

 

I found one javascript in which we can run SOSL query to search records, it works fine. But I dont able to find any method to set this result to list view page.

 

function findText() { 
    sforce.connection.sessionId = '{!$Api.Session_ID}';
    var who=prompt("Enter the text");
    // These special characters must be preceded by a backslash
    // before they can be used in a SOSL query.
    who = who.replace(/([\&\|\!\(\)\{\}\[\]\^~\:\\\+\-])/g, "\\$1");

    var sstr = "find {" + who + "} in NAME FIELDS RETURNING " +
               "Lead (id, firstname, lastname), " +
               "Contact(id, firstname, lastname), " +
               "Account(id, name)";

    // Issue the SOSL query using the AJAX Toolkit.
    var sr = sforce.connection.search(sstr); //result of sosl query
    var m = document.getElementById('main');  

    // Write out the results.
    if (sr) { 
        var list = sr.getArray('searchRecords');
        for (var i = 0; i < list.length; i++ ) { 
            m.innerHTML += "<p>Search results : " + 
            list[i].toString();
        }
    } else { 
        m.innerHTML += "</p><p>No search results";
    }
} 

 

All Answers

Vinita_SFDCVinita_SFDC

Hello,

 

In force.com sites we use visualforce pages only, are you refering to  site.com sites?

NishanNishan

Hi Vinita,

                I am sorry for the mistake. When I said visualforce pages I was referring to Apex. I added the same question to Saleforce community thats why I mentioned about Force.com. Is it doable with just Javascript ?

Vinita_SFDCVinita_SFDC

Hi Nishan,

 

Sorry i am not able to understand your requirement, is this like you have a force.com site and on that site you have a visualforce page and then you have a button and when the button is click the records should be shown in default salesfoce list.

 

Now what does default salesforce list means, do you mean List Views?

 

OR

There is no involvement of sites and its within your Org?

Dhaval PanchalDhaval Panchal

Hi Nishan,

 

Actually I am not able to understand your requirement properly. But as per my understanding let me give you one idea.

 

Create on List button (javascript)

 

and use below javascript

 

{!requireScript("/soap/ajax/26.0/connection.js")}

var url = parent.location.href;

var id=prompt("Enter the Id");

var redirectUrl = location.protocol+'//'+location.host+'/' + id;
if(id == null || id == '' || id == 'undefined'){
   redirectUrl = url;
}
parent.location.href = redirectUrl;

 

Add this button on list view page. Click on that button give record id and click on ok. It will redirect you to that record standard page.

 

Let me know if you want something different.

NishanNishan

Hi Dapanchal,

                          My requirement is somewhat similar to what you have suggested. But instead of redirecting to the record standard page I want to view the details of records that matches my query in the list view itself. Also I need to search for records with criteria other than the record Id. I know it can be done by editing the list view itself. Is there any way to do this with a button to make it easier to search for records.

 

 

NishanNishan

Hi Vinita,

                I hope my problem is now clear to you after reading Dapanchal's suggestion. Don't worry about visualforce or orgs, I just want to display records based on some input from a button (via prompt in Javascript) in the list view of an object. This function can be easily obtained by editing the list view. I just want to create a button to make it easier to search for records.

Dhaval PanchalDhaval Panchal

Hi Nishan,

 

I found one javascript in which we can run SOSL query to search records, it works fine. But I dont able to find any method to set this result to list view page.

 

function findText() { 
    sforce.connection.sessionId = '{!$Api.Session_ID}';
    var who=prompt("Enter the text");
    // These special characters must be preceded by a backslash
    // before they can be used in a SOSL query.
    who = who.replace(/([\&amp;\|\!\(\)\{\}\[\]\^~\:\\\+\-])/g, "\\$1");

    var sstr = "find {" + who + "} in NAME FIELDS RETURNING " +
               "Lead (id, firstname, lastname), " +
               "Contact(id, firstname, lastname), " +
               "Account(id, name)";

    // Issue the SOSL query using the AJAX Toolkit.
    var sr = sforce.connection.search(sstr); //result of sosl query
    var m = document.getElementById('main');  

    // Write out the results.
    if (sr) { 
        var list = sr.getArray('searchRecords');
        for (var i = 0; i < list.length; i++ ) { 
            m.innerHTML += "<p>Search results : " + 
            list[i].toString();
        }
    } else { 
        m.innerHTML += "</p><p>No search results";
    }
} 

 

This was selected as the best answer
NishanNishan

Hi Dhaval,


                    Thanks for the link. I will check it out.