2

I have a requirement where I have to display different images stored in Static resource depending on a Lead's custom field's (Profile__c) value. Kindly help me with a sample code which states that if the value of Profile__c is 'A', display /resource/1413133715000/A and If value of Profile__c is 'B' then display /resource/1413133715000/B.

I have this custom formula field on Lead (Profile__c) whose value is being calculated which could be either A, B or C. Now I have created a custom button on Lead detail page which is a URL button where I have mentioned the link of the page which should display different images depending upon Profile__c field's value. Here is the Java script which is being used currently:

 <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;
          if(result=='A')
          {
            url='https://ap1.salesforce.com/resource/A';
            }
              else if(result='B')
                 {
                url='https://ap1.salesforce.com/resource/B';
             }
                 else if(result='C')
            {
            url='https://ap1.salesforce.com/resource/C';}
             window.location.href=url;
                   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, " "));
             }
                     });
             </script>
    </apex:form>
3
  • Where you need to display image. Is it a formula field or visualforce page Commented Oct 14, 2014 at 7:16
  • VisualForce Page Commented Oct 14, 2014 at 7:27
  • But you need to display a field containing the image on a VF Page, or simply the image not contained in any fields? It could be a solution to create a custom field in the way I showed below and then include the field in your VF page.
    – user9959
    Commented Oct 14, 2014 at 7:45

2 Answers 2

2

There are two ways to implement this requirement:

  1. You can use multiple <apex:image> to add image in visualforce page and you can use rendered attribute to check if condition is satisfied, then display this image. Please see sample code below:

    <apex:image id="theImage" value="{!$Resource.myResourceImage1}" width="200" height="200" rendered={!IF(leadObj.Profile__c == 'ABC',true,false)}/> <apex:image id="theImage" value="{!$Resource.myResourceImage2}" width="200" height="200" rendered={!IF(leadObj.Profile__c == 'DEF',true,false)}/>

  2. Second way is that in your controller logic, you can get name of static resource using your logic, then you can display that static resource in visualforce page.

2
  • I tried your VF code but it is not displaying any image. I have a custom formula field on Lead (Profile__c) whose value is being calculated which could be either A, B or C. Now I have created a custom button on Lead detail page which is a URL button where I have mentioned the link of the page which should display different images depending upon Profile__c field's value. This has been tried with java script which is working fine for first 2 values i.e A and B, but its not displaying correct image for C. I want to achieve this functionality through VF code. Kindly help. Commented Oct 14, 2014 at 8:09
  • Below is the Java script which is being used currently: Commented Oct 15, 2014 at 9:14
0

I would suggest creating a visualforce component. The value of formula field can be passed in as component's attribute and component can have multiple image blocks, each image block for each image and would render appropriate image, based on formula field's value.

With component, this can be reused multiple times.

You must log in to answer this question.

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