I have a formula field on Lead (Profile__c) which gets updated with different values depending on the another field (Weight__c) given. Below is the formula:

IF (AND(Weight__c> 0, Weight__c <= 17), "A", 
IF (AND(Weight__c>= 18, Weight__c <= 32), "B", 
IF (Weight__c>= 32, "C",  "")))

Now I have a button on Lead which displays 3 different images depending on Profile field's value. I am achieving this with Java script. Everything is working fine except for whenever the value of Profile__c is C, its displaying the image related to option B and not the image related to option C of Profile. In addition to this, if the Profile__c field is blank and I click on the button, its still displaying the image related to option B which shouldn't be happening. Kindly help.

If possible, kindly help me with a VF sample code to replace the same functionality which is happening through Javascript. Thanks in advance.

Here is the Java script

   <apex:page standardController="Lead">
   <apex:form >
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

  <script type="text/javascript">
     $(document).ready(function () {
     var result=getParameterByName('lId');
      var url;
        else if(result='B')
                 else if(result='C')
             function getParameterByName(name) {
             name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
                 var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                    results = regex.exec(location.search);
               return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));




1 Answer 1


The reason you're always getting 'B' is because you're not using a condition but instead have written a statement.

You must change this :

else if(result='B') //this will always evaluate to true as this is a statement

to :

else if(result == 'B')

You can use switch - case for this type of functionality.

    case 'A':
          url = '/resource/A';
    case 'B':
          url = '/resource/B';
    case 'C':
          url = '/resource/C';
    default :
          url = '';

If you can paste in your code and explain a little more on where the button is and how you're calling this piece of code from the button (as I do not see any click handler in your code) then we can see if this could be done without going for javascript.

  • Thanks a bunch for your help :) ...." else if(result == 'B') " this worked. Now when Im clicking on the button keeping Profile field as blank, its showing me an undefined page. Oct 16, 2014 at 5:52
  • That's because you have not handled what should happen if the profile is blank. So basically the url variable is undefined in case if the result is null / blank and that's the reason you get undefined page error.
    – Sam
    Oct 16, 2014 at 8:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .