Wednesday, 21 August 2013

Predictive search on a field in standard page with home page components


I had a requirement to add a predictive search (auto complete) on a field in standard page. The exact requirement was like whenever the user types some characters on a field then automatically a suggestion drop down should come and that should list all the values currently in the system for that field.

The requirement can be easily fulfilled by home page component, little java script and jQuery.

I am not going to discuss how can we add home page component to a standard page, all the documentation is available from sales force.

I have used the following code in my home page component

<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="/soap/ajax/15.0/connection.js" type="text/javascript"></script>
<script src="/soap/ajax/15.0/apex.js" type="text/javascript"></script>
<script>
var url = document.URL;
if(url.indexOf('006')!=-1)    
{        
        var sid = document.cookie.match(' sid=([^;]*)')[1];        
        sforce.debug.trace=true;        
        sforce.connection.sessionId = sid;        
        var stages = sforce.apex.execute("opportunitySearch", "getFieldValues", {});        
        var staheArray = stages.toString().split("+");        
        $ = jQuery.noConflict();       
        $(function()             
        {                
            var availableTags = staheArray;               
            $( "#00N90000006ikQJ" ).autocomplete({source: availableTags});           
        }        );    
}
</script>

I have created the below apex class for the above code to work.
global class opportunitySearch{ 
   webService static String getFieldValues()
   {
       String pickValues='';
       for( Schema.PicklistEntry f : Opportunity.StageName.getDescribe().getPicklistValues())
       {
           pickValues = pickValues +f.getValue()+'+';
       }         
       return pickValues ;
   }
}



EXPLANATION

Since sales force allows us to execute java script code from home page component we can use JavaScript or jQuery to implement some functionality in a standard page through home page component. Here I need to fetch the data from back end and populated it on the page I should be able to execute some query/apex code to do that. In the above example I fetch all the opportunity stage values from a pick list and populated on another field on the standard page.

To execute the apex class from java script I cannot use the java scrip remoting since it is not supported in the home page component. So I used sales force ajax toolkit to establish the connection between home page component and apex class.


The below lines of code is required for the Ajax tool kit to work.


<script src="/soap/ajax/15.0/connection.js" type="text/javascript"/>

<script src="/soap/ajax/15.0/apex.js" type="text/javascript"/>
 



Then I took the sales force session id from cookie through java script , the below line of code does that

document.cookie.match(' sid=([^;]*)')[1];


As we got the seesion id we need to set it on the sales force connection. Once we set the session id for sfoce we can execute the apex method from a class using the below piece of code

sforce.apex.execute(“Give apex class name here”,”Give apex methos here “,{“give apex method parameters here”});
The apex class should be declared as global and we need to have a webservice method also be there in the class for java script to execute. In the apex class I have got all the picklist values and I made it as a string separated with + sign. So the method returns all the picklist values separated with + symbol.
So in the java script I filter each status value with reference to the + symbol and I moved them into an array (stageArray).

From now I am using the jQuery to do the autocomplete job through autocomplete () function on a particular element by using the element id.
 

34 comments:

  1. Thanks for sharing this.

    A couple of quick comments:
    1. On line #9 in your homepage component, you are tying yourself to a specific org by hardcoding the id value to "006" ---- if(url.indexOf('006')!=-1). You should consider getting the id value dynamically otherwise this may not work for all your orgs)

    2. You could perhaps look into a SOSL query to get the list of possible values for a given field as you type using a controller.

    Just sharing experiences here.

    ReplyDelete
    Replies
    1. I have read your blog its very attractive and impressive. I like it your blog.


      JavaEE Training in Chennai JavaEE Training in Chennai

      Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

      Java Online Training Java Online Training Core Java 8 Training in Chennai Java 8 Training in Chennai

      Delete
    2. Great.. Tutorial is just awesome..It is really helpful for a newbie like me...

      JavaScript Online Training JavaScript Online Training JQuery Online Training JQuery Online Training
      JavaScript Course | HTML5 Online Training

      Delete
    3. JQuery Training in Chennai JQuery Training in Chennai HTML5 Training in Chennai HTML5 Training in Chennai JavaScript Training in Chennai JavaScript Training in Chennai MEAN Developer Training in Chennai MEAN Developer Training in Chennai JavaScript Training in CHennai

      Delete
  2. Thanks for your comments....

    for the first point it wont get change since account id always start with 001 and contact 003, opportunity 006 like it goes. So no impact on any particular instance

    ReplyDelete
  3. Thanks for sharing this information. This really help for any salesforce developer when make the pages.

    ReplyDelete
  4. Your information is really nice and very informative. Thanks for sharing this great article.

    Salesforce Course in Chennai

    ReplyDelete
  5. The information you have deliver here is really useful to make my knowledge good. Thanks for your heavenly post. It is truly supportive for us and I have accumulated some essential data from this blog.
    Salesforce training in Chennai | Salesforce training

    ReplyDelete
  6. Hi, I wish to be a regular contributor of your blog. I have read your blog. Your information is really useful for us. I did Software Testing Training at Fita training and placement academy which offer best Software Testing Course in Chennai with years of experienced professionals. This is really useful for me to make a bright career.

    ReplyDelete
  7. Hi friends, This is Rebeka from Chennai. I am a technology freak. Your technical information is really useful for me. Keep update your blog.
    Regards..
    Oracle Training in Chennai

    ReplyDelete
  8. Your blog is really nice and informative. Thanks for sharing this post. Keep posting..

    Regards..
    Salesforce Developer Training in Chennai




    ReplyDelete
  9. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog. Recently I did Software Testing Course in Chennai at a reputed training institutes.

    ReplyDelete
  10. Hi, Your blog is really very informative and useful for me. Thanks for sharing this valuable blog.
    Regards..
    Unix Training Institutes in Chennai

    ReplyDelete
  11. Java is one of the popular technologies with improved job opportunity for hopeful professionals. Java Training in Chennai helps you to study this technology in details.If you are looking for best Java Training Institutes in Chennai reach Fita academy.


    ReplyDelete
  12. There are lots of information about latest technology and how to get trained in them, like Hadoop Training Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies(Hadoop Training in Chennai). By the way you are running a great blog. Thanks for sharing this. FITA chennai reviews

    ReplyDelete
  13. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
    Selenium Training in Chennai | QTP Training in Chennai

    ReplyDelete
  14. Thanks for Information Oracle Apps Technical is a collection of a bunch of collected applications like accounts payables, purchasing, inventory, accounts receivables, human resources, order management, general ledger and fixed assets, etc which have its own functionality for serving the business
    Oracle Apps Training In Chennai

    ReplyDelete
  15. Oracle Training in chennai | Oracle D2K Training In chennai
    This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

    ReplyDelete
  16. Internship & Recruitment Program for MCA students
    Webtrackker also provide the 6 Month/ weeks industrial training / Internship & Recruitment Program for MCA students in Java, dot net, Web designing, web developments, Angular.js, Node.js, Hybrid apps, computer networking, Plc Scada, Auto cad, All modules in ERP sap, sap mm, sap fico. Php, Oracle Dba, networking etc for MCA, BCA, B.Tech Students.
    Webtrackker Technologies
    B-47, Sector- 64
    Noida- 201301
    Phone: 0120-4330760, 8802820025
    Email: Info@Webtrackker.Com
    Web: www.webtrackker.com

    ReplyDelete
  17. Best SQL Query Tuning Training Center In Chennai This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

    ReplyDelete

  18. Web designing Training Institute in noida - with 100% placement support - web trackker is the is best training institute for web designing, web development in delhi. In you are looking web designing Training in noida, web designing Training Institute in Noida, web designing training in delhi, web design training institute in Ghaziabad, web designing training institute, web designing training center in noida, web designing course contents, web designing industrial training institute in delhi, web designing training coaching institute, best training institute for web designing training, top ten training institute, web designing training courses and content then Webtrackker is the best option for you.

    ReplyDelete
  19. Linux Training Institute in Noida
    Best Linux & UNIX Training Institute In Noida, Delhi- Web Trackker Is The Best Linux & Unix Training Institute In Noida, Top Linux & Unix Coaching In Noida Sector 63, 53, 18, 15, 16, 2, 64 Providing The Live Project Based Linux & Unix Industrial Training.

    ReplyDelete
  20. SAS Training Institute in noida
    Best SAS training in Noida- with 100% placement support - Fee Is 15000 Rs - web trackker is the best institute for industrial training institute for SAS in Delhi, Ghaziabad, if you are interested in SAS industrial training then join our specialized training programs now. SAS Training In Noida, SAS industrial training in noida, SAS training institute in noida, SAS Training In ghaziabad, SAS Training Institute in noida, SAS coaching institute in noida, SAS training institute in Ghaziabad.

    ReplyDelete
  21. Java training institute in noida-webtrackker is best java training institute in noida witch also provides real time working trainer, then webtrackker best suggestion of you and better carrier if you are looking the"Java Training in Noida, java industrial training, java, j2ee training courses, java training institute in noida, java training center in delhi ncr, java training institute in ncr, Ghaziabad, project based java training, institute for advance java courses, training institute for advance java, java industrial training in noida, java/j2ee training courses in ghaziabad, meerut, noida sector 64, 65, 63, 15, 18, 2"Webtrackker is best otion for you.

    ReplyDelete
  22. 1800-640-8917 Norton antivirus technical support phone number, Norton customer support toll free number, NORTON antivirus customer Support number, 1800-640-8917 NORTON antivirus tech support number, Norton antivirus technical support phone number, 1800-640-8917 Norton antivirus technical support number, 1800-640-8917 Norton antivirus technical support toll free number, Norton technical support number.

    ReplyDelete
  23. Webtrackker Technologies- Webtrackker is an IT company and also provides the project based industrial training in Java, J2EE. Webtrackker also provide the 100% job placement support. JAVA Training Institute in Meerut, Best J2EE training Institute in Meerut, best JAVA Training Institute in Meerut, best JAVA Training Institute in Meerut, project JAVA Training in Meerut, JAVA Training on live project based in Meerut, Java Training Courses in Meerut, Summer Training Program in Meerut, Summer Training Program on java in Meerut.

    ReplyDelete
  24. Paris airport transfer - Parisairportransfer is very common in Paris that provides facilities to both the businessmen and the tourists. We provide airport transfers from London to any airport in London and also cruise transfer services at very affordable price to our valuable clients.

    Paris taxi
    Paris airport shuttle
    paris hotel transfer
    paris airport transfer
    paris shuttle
    paris car service
    paris airport service
    disneyland paris transfer
    paris airport transportation
    beauvais airport transfer
    taxi beauvais airport
    taxi cdg airport
    taxi orly airport

    ReplyDelete
  25. This comment has been removed by the author.

    ReplyDelete
  26. This idea is mind blowing. I think everyone should know such information like you have described on this post. Thank you for sharing this explanation.Your final conclusion was good. We are sowing seeds and need to be patiently wait till it blossoms.
    SEO Web Design Company Chennai

    ReplyDelete
  27. Finding the time and actual effort to create a superb article like this is great thing. I’ll learn many new stuff right here! Good luck for the next post buddy..
    Digital Marketing Company in Chennai

    ReplyDelete