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
bannubannu 

Ratings in Salesforce

hi,

i need to store the ratings given for a partiuclar image.

but im not getting the out put.

i have reference the code from http://blog.tquila.com/2012/03/13/recipe-to-add-ajax-dynamic-ratings-in-salesforce-using-jquery/#comment-255.

please help me.

 

apex class:

public with sharing class Rating {
   
 
  public Rating(ApexPages.StandardController controller) {
  Complete_Rating__c co = (Complete_Rating__c)controller.getRecord();
   System.debug('adadaad'+cr_id);
   cr_id = co.id;
  }
   
 
  public string cr_id = ApexPages.currentPage().getParameters().get('Id');
 
  private integer current_rating;
  private Rating__c ratingObj = new Rating__c();
  private integer stored_val;
 
   
  public integer storedVal{
  get{
  try {
  Complete_Rating__c crObj = [SELECT Average_Ratings__c FROM Complete_Rating__c WHERE Id = : cr_id];
 
  stored_val = Integer.valueOf(crObj.Average_Ratings__c);
  }catch(QueryException e) {
  stored_val = 0;
  }
  if(stored_val == null)
  stored_val = 0;
   
  return stored_val;
 }
  }
   
 
  public integer currentRating{
  get {
  if (current_rating == null) {
  try {
  ratingObj = [SELECT Scale__c, Rated_By__c FROM Rating__c WHERE Complete_Rating__c = : cr_id AND Rated_By__c = :UserInfo.getUserId()];
  current_rating = Integer.valueOf(ratingObj.Scale__c);
  }catch(QueryException qe){
  current_rating = 0;
  ratingObj.Rated_By__c = UserInfo.getUserId();
  ratingObj.Complete_Rating__c = cr_id;
  }
  }
  return current_rating;
  }
  set {
  current_rating = value;
  }
  }
 
   
  public void submit() {
  ratingObj.Scale__c = currentRating;
  upsert ratingObj;
  }
  }

 

vf page:

<apex:page standardController="Complete_Rating__c" extensions="Rating" >
  <apex:form >
  <apex:outputPanel id="mainPanel">
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
      <script>
      jQuery(document).ready(function($) {
          var storedValue = {!storedVal};
          setThumbs(storedValue); // load the average rating first.
   
          // When clicked, get the position of the thumb and submit it accordingly
          $(".thumb").click(function() {
              var rating = $(this).attr('rating');
              submitRating(rating);
          });
          // Hovering function to show colored and black&white thumbs depending on the cursor position.
          $(".thumb").hover(
              function() {
                  var rating = $(this).attr('rating');
                  setThumbs(rating);
              },
              function() {
                  var storedValue = {!currentRating};
                  setThumbs(storedValue);
              });
      });
   
      function setThumbs(rating){
          turnOnThumbs(rating);
          turnOffThumbs(rating);
      }
   
      function turnOnThumbs(rating){
          for(i=1; i<=rating; i++){
              var thumbId = '#thumb' + i;
             $(thumbId).attr('src', '{!$Resource.Likes_Thumb}');
          }
      }
      function turnOffThumbs(rating){
          for(j=5; j>rating; j--){
              var thumbId = '#thumb' + j;
              $(thumbId).attr('src', '{!$Resource.Dislike_Thumb}');
          }
      }
   
      </script>
      <div align="left">
      <img src="{!$Resource.Likes_Thumb}" width="25" height="25" id="thumb1" rating="1"/>
      <img src="{!$Resource.Likes_Thumb}" width="25" height="25" id="thumb2" rating="2"/>
      <img src="{!$Resource.Likes_Thumb}" width="25" height="25" id="thumb3" rating="3"/>
      <img src="{!$Resource.Likes_Thumb}" width="25" height="25" id="thumb4" rating="4"/>
      <img src="{!$Resource.Likes_Thumb}" width="25" height="25" id="thumb5" rating="5"/>
       </div>
   </apex:outputPanel>
   
   <apex:actionFunction name="submitRating" action="{!submit}" reRender="mainPanel">
       <apex:param name="firstParam" assignTo="{!currentRating}" value=""/>
   </apex:actionFunction>
   
  </apex:form>
  </apex:page>

iffuiffu

The code looks okay so far, Did you happen to do the "console.log" from jquery to debug if there is a problem on the javascript side?

 

Can you add console.log(' ratings : ' + rating); in click function and check it from the chrome browser by "inspecting element" and you can use Firebug plugin from mozilla.

 

If its not giving any value, then its probably not passing values from UI. Let me know how you get on?

bannubannu

no its not working.

 

i have tried.

in debug it is showing the null value as i kept it in constructor.

please check it out once

bannubannu

hi irfan

i have tried i didnt get it

but using java script the ratings values are getting changed but here problem is how to store those ratings using scripting?

or else tell me in jquery