+ Start a Discussion
Brian Sherman 22Brian Sherman 22 

Visualforce page conditional image not working

We have a VF page that displays one company logo in a certain condition, and another in all others.  Code is below:

<apex:image id="Logo" value="{!If(objLeadSite.Site__r.Company__r.Name=="XXXX", "https://c.na24.content.force.com/servlet/servlet.ImageServer?id=0151a000000nGvu&oid=00D1a000000XXXX","https://c.na24.content.force.com/servlet/servlet.ImageServer?id=0151a000000nGvy&oid=00D1a000000YYYY")}" />

Now, when I land on this page from a related record that should match the Company Name, instead I am presented the "else" image.

What am I missing?
anto nirmalanto nirmal
Hi Brian,

Use the render attribute for this funtion:
Try something like the below:
<img id="Logo" src="https://c.na24.content.force.com/servlet/servlet.ImageServer?id=0151a000000nGvu&oid=00D1a000000XXXX" style='display:{!If(objLeadSite.Site__r.Company__r.Name=="XXXX","None","inline-block")};' />
<img id="Logo" src="https://c.na24.content.force.com/servlet/servlet.ImageServer?id=0151a000000nGvy&oid=00D1a000000YYYY" style='display:{!If(objLeadSite.Site__r.Company__r.Name=="XXXX","inline-block","None")};' />
Let me know if this helps.

As a common practice, if your question is answered, please choose 1 best answer.
Additionally you can give every answer a like if that answer is helpful to you.

Regards,
Anto Nirmal
Brian Sherman 22Brian Sherman 22
That seems to be getting me somewhere!  That said, I want to display in the first line if Name='XXXX'.  In the second line I want to display if Name does NOT = XXXX.
Vishwajeet kumarVishwajeet kumar
Try this :
 
<apex:image id="Logo" url="https://c.na24.content.force.com/servlet/servlet.ImageServer?id=0151a000000nGvu&oid=00D1a000000XXXX" rendered="{!objLeadSite.Site__r.Company__r.Name =='XXXX'}"/>

<apex:image id="logo1" url="https://c.na24.content.force.com/servlet/servlet.ImageServer?id=0151a000000nGvy&oid=00D1a000000YYYY" rendered="{!NOT(objLeadSite.Site__r.Company__r.Name=='XXXX')}"/>

rendered attribute is good for conditional display.
Brian Sherman 22Brian Sherman 22
Nope, it is not seeing the Company Name.  I dug in a little and looked at the controller.

        for(Lead_To_Site_Association__c  objLeadToSite : [Select Id,
                                                                  Appt_Requested__c,
                                                                  Wait_List_Requested__c,
                                                                  Lead__c,
                                                                  Lead__r.Name,
                                                                  Site__c,
                                                                  Site__r.Name,
                                                                  Suite_Type__c,
                                                                  Appt_Preffered_Date_Time__c,
                                                                  Site__r.State_Province__c
                                                            From Lead_To_Site_Association__c 
                                                            Where Id=:strRecordId])

I do not see Site__r.Company__r.Name listed here.  I presume that means I cannot grab that field.  Correct me if I am wrong, but if I add that field to the list, it should render, right?

 
Vishwajeet kumarVishwajeet kumar
yes, please add "Site__r.Company__r.Name" in you query field list and see. it should work.
Brian Sherman 22Brian Sherman 22
Ugh.  Controller is updated as follows, still not working.

        for(Lead_To_Site_Association__c  objLeadToSite : [Select Id,
                                                                  Appt_Requested__c,
                                                                  Wait_List_Requested__c,
                                                                  Lead__c,
                                                                  Lead__r.Name,
                                                                  Site__c,
                                                                  Site__r.Name,
                                                                  Suite_Type__c,
                                                                  Appt_Preffered_Date_Time__c,
                                                                  Site__r.State_Province__c,
                                                                  Site__r.Company__r.Name
                                                            From Lead_To_Site_Association__c 
                                                            Where Id=:strRecordId])
Vishwajeet kumarVishwajeet kumar
it will work only if "Lead_To_Site_Association__c " object has a field "Site__C", which has a field "Company__c" and object "Company__c" should have field "Name". try to verify this relationship and see.
Brian Sherman 22Brian Sherman 22
Indeed it does.  Here is another version of this condition in a VF email template, working fine:

<apex:image id="Logo" value="{!If(relatedTo.Site__r.Company__r.Name==...
Vishwajeet kumarVishwajeet kumar
Well, then it should work. 
May be in page variables are not pointing to correct object data or may be contoller method is not getting called before loading the page.
Make sure that method containing query is called before page load.
you can put method in "action" attribute of page and see.
Brian Sherman 22Brian Sherman 22
What about this entry in the controller?  Is this conflicting with what we are doing?

        strImgUrl= '/servlet/servlet.FileDownload?file=';

        for(Document objDocument : [SELECT Id FROM Document WHERE Name='Acme Logo' limit 1])
        {
            strImgUrl += objdocument.Id;
Vishwajeet kumarVishwajeet kumar
no, it should not. it could be something with same name in page as well as in controller.
it could be that a loop variable in page, which is trying to use same variable name as it is in controller.

i am wondering variable "objLeadSite" is a list, so how come it is getting used as a single object instance.
Try this code and see :
 
<apex:image id="Logo" url="https://c.na24.content.force.com/servlet/servlet.ImageServer?id=0151a000000nGvu&oid=00D1a000000XXXX" rendered="{!objLeadSite[0].Site__r.Company__r.Name =='XXXX'}"/>

<apex:image id="logo1" url="https://c.na24.content.force.com/servlet/servlet.ImageServer?id=0151a000000nGvy&oid=00D1a000000YYYY" rendered="{!NOT(objLeadSite[0].Site__r.Company__r.Name=='XXXX')}"/>



 
Brian Sherman 22Brian Sherman 22
objLeadSite should be a record, not a list.  I entered your code above and got the error:

Error: Incorrect parameter type for subscript. Expected Text, received Number

<apex:image id="Logo" url="https://c.cs41.content.force.com/servlet/servlet.ImageServer?id=XXX" rendered="{!objLeadSite[0].Site__r.Company__r.Name =='XXXX'}"/>
            <apex:image id="logo1" url="https://c.cs41.content.force.com/servlet/servlet.ImageServer?id=YYYY" rendered="{!NOT(objLeadSite[0].Site__r.Company__r.Name=='XXXX')}"/>
Vishwajeet kumarVishwajeet kumar
if it is not a list then code should work and my code needs to refined more. we cannot do subscripts on lists in visaulforce.
may be we need write a method to get only first element and use that.

Can you put page and controller full code here, so i can have better idea on it?
Brian Sherman 22Brian Sherman 22
/*
* @ClassName    : SL_RequestAppointment 
* @JIRATicket   : XXXXXX
* @CreatedOn    : 9/July/2015
* @ModifiedBy   : XX
* @Description  : This class will set the value of Appt_Requested__c,Wait_List_Requested__c field on Lead_To_Site_Association__c object to true on Click of Button from VF page
*/
public without sharing class SL_RequestAppointment 
{
    // variable decleration
    public Lead_To_Site_Association__c objLeadSite  {get;set;}
    Public String strImgUrl                         {get;set;}
    Public Boolean isReqType                        {get;set;}
    public Boolean isclicked                        {get;set;}
    Private String  strisApp;                         
    Private string  strRecordId ;  
    public Date viewingDate                             { get; set; }
    public Time viewingTime                             { get; set; }
    
    // Constructor
    public SL_RequestAppointment()
    {
        initialize();
        fetchLogo(); 
        
        //Fetch the URL parameters
        strRecordId = ApexPages.currentPage().getParameters().get('RecId');
        strisApp = ApexPages.currentPage().getParameters().get('reqType');
        
        if(strisApp == 'appt')
            isReqType = true;

        //IF LEAD TO SITE RECORD already exists fetching the LeadName and SiteName
        for(Lead_To_Site_Association__c  objLeadToSite : [Select Id,
                                                                  Appt_Requested__c,
                                                                  Wait_List_Requested__c,
                                                                  Lead__c,
                                                                  Lead__r.Name,
                                                                  Site__c,
                                                                  Site__r.Name,
                                                                  Suite_Type__c,
                                                                  Appt_Preffered_Date_Time__c,
                                                                  Site__r.State_Province__c,
                                                                  Site__r.Company__r.Company_Code__c
                                                            From Lead_To_Site_Association__c 
                                                            Where Id=:strRecordId])
        {                                                           
            objLeadSite = objLeadToSite;
            if((objLeadSite.Appt_Requested__c && isReqType) || (objLeadSite.Wait_List_Requested__c && !isReqType))
                isclicked = true;
        }
    }

    /*
    * @MethodName : initialize 
    * @param      : None 
    * @Description: This Method will initialize the variables
    */
    Private void initialize()
    {
        strRecordId =  strImgUrl = '';
        objLeadSite = new Lead_To_Site_Association__c();
        isReqType = isclicked  = false;
    }
    
    /*
    * @MethodName : fetchLogo 
    * @param      : None 
    * @Description: This Method will fetch the Capreit Logo from Documents.
    */ 
    private void fetchLogo()
    {
        strImgUrl= '/servlet/servlet.FileDownload?file=';

        for(Document objDocument : [SELECT Id FROM Document WHERE Name='Capreit Logo' limit 1])
        {
            strImgUrl += objdocument.Id;
            
        }
    }

    /*
    * @MethodName : btnSetAppointmentRequest 
    * @param      : None 
    * @Description: This Method will set the value of Appt_Requested__c,Wait_List_Requested__c field on Lead_To_Site_Association__c object 
    */ 
    public void btnSetAppointmentRequest()
    {
        isclicked = true;
    
        if(isReqType )
        {
            objLeadSite.Appt_Requested__c = true; 
        }
        else if(!isReqType)
        {
            objLeadSite.Wait_List_Requested__c = true; 
        }
        
        if(viewingDate != null && viewingTime != null)
        {
            objLeadSite.Appt_Preffered_Date_Time__c = DateTime.newInstance(viewingDate, viewingTime);
        }
        
        Upsert objLeadSite;
    }
}

 
Brian Sherman 22Brian Sherman 22
FYI that Company_Code field was a test.  It is set like below:

//IF LEAD TO SITE RECORD already exists fetching the LeadName and SiteName for(Lead_To_Site_Association__c objLeadToSite : [Select Id, Appt_Requested__c, Wait_List_Requested__c, Lead__c, Lead__r.Name, Site__c, Site__r.Name, Suite_Type__c, Appt_Preffered_Date_Time__c, Site__r.State_Province__c, Site__r.Company__r.Name
Vishwajeet kumarVishwajeet kumar
controller looks good and page would have worked corectly.
i am assuming page is getting tested with "RecID" parameter?
Brian Sherman 22Brian Sherman 22
Yes.  URL contains the RecID parameter.
Vishwajeet kumarVishwajeet kumar
Can you try to change constructor(SL_RequestAppointment) as a method (change method name and signature, return void or null pagrefrence) and use it as atttibute in page:
<apex:page action="{!Mehod}"></apex:apge>
and see.
Brian Sherman 22Brian Sherman 22
I believe I may have found the issue.  I tested with multiple record IDs and when I click the submit button I get the note below:

Authorization Required 

You must first log in or register before accessing this page. 
If you have forgotten your password, click Forgot Password to reset it. 

This was never the case previously.
Vishwajeet kumarVishwajeet kumar
well, from your code it doesn't looks like it is designed to work with multiple RecIDs.
Brian Sherman 22Brian Sherman 22
So the way this works is that autoreplies for inquiries go out - each has a link that passes the record ID back to the page as a parameter.
Vishwajeet kumarVishwajeet kumar
ok. well, try to change signature of your constructor by method and use it in action attribute and see.