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
sunfishettesunfishette 

Image insert into VF page based on User log in

I have started working with a clip of code from a different post to work in an image to a visual force page that is based on the credentials of the current user.

 

I have stored the image in the "Notes and Attachements" section of the contact object, the title of the file is "Contact.jpg" ...

Here is the javascript :

 

 

<script src="/soap/ajax/15.0/connection.js"  type="text/javascript">
        function doit() {
            var ids = sforce.connection.query("SELECT id FROM Attachment WHERE ParentId = '{!Contact.Id}' AND Name='Contact.jpg' limit 1");
            if(ids == NULL) {   
                document.getElementById('displayDiv').innerHTML = '<br />No Image Found.<br />';
            } else {
                var srv = ("{!Scontrol.URL}".split(/salesforce.com/))[0] + 'salesforce.com/services/SRedirect?u=';
                var url = '/servlet/servlet.FileDownload?file=' + ids.records.Id + '&retURL=/' + '{!Contact.Id}';
                document.getElementById('displayDiv').innerHTML = '<img src=' + srv + url + ' alt="Unable to Display Picture/>';
            }    
        }

 

 

Here is my call :

   <body onload="doit()">

 

Here is my error:

ERROR: index line 10, column 92 The reference to entity “retURL” must end with the ‘;’ delimiter

ERROR: The reference to entity “retURL” must end with the ‘;’ delimiter

 

(LINE 10 =)

                var url = '/servlet/servlet.FileDownload?file=' + ids.records.Id + '&retURL=/' + '{!Contact.Id}';

 

I think the error lies in the URL itself that I am trying to assemble, but I have gone around this issue so much today, I think my eyeballs are going to fall out soon.   Can anyone spot the error and help me out?  Thank you!!!

 


Best Answer chosen by Admin (Salesforce Developers) 
sunfishettesunfishette

Here is what I have done to solve the issue for anyone looking to do this in the future:

public String getImageName() { 
       String file = [SELECT Name 
                       FROM Account 
                       WHERE Id IN 
                            (SELECT AccountId 
                               FROM user 
                              WHERE username=:UserInfo.getUsername()
                             )
                       LIMIT 1].Name;
        
	file = file.replace(' ', ''); String foldername = 'Logos/'; 
	String extension = '.jpg'; 
	String fullImageName = foldername + file + extension; 
	return fullImageName;       
    }  

 I ran a query to get the name of the company based on the users login.  Then I stripped out the white spaces, and sandwiched it between the folder name and the image extension.

 

EXAMPLE:

Charlie Brown logs into his partner portal.  He works for Brown Ltd.  The query fetches "Brown Ltd" and returns it to the string "file".  Then the white space is stripped out to make it file = "brownltd"

then I append my foldername, file, and extension together to make one string as such:

"Logos/brownltd.jpg"

and I return that to my Visualforce page.

 

Works like a charm.  Now I have one common file for company Logos and I can display them based on who logs in.

All Answers

sfdcfoxsfdcfox

Why are you doing this in JavaScript? Why not do this as pure Visualforce?

 

<apex:outputText value="No image found" rendered="{!isnull(imageurl)}"/>
<apex:image url="{!imageurl}"/>
public string getimageurl() {
  for(attachment a:[select id from attachment
where parentid = :contact.id and name = 'contact.jpg' limit 1]) { return String.format('/servlet/servlet.FileDownload?file={0}',
new String{ String.valueof(a.id) } ); } return null; }

Of course, this is just one possible means of doing so, but the idea here is that you don't need to waste an API call (!) just to get data that is otherwise "free." That is, calling the API counts against the 5000 per day per user API call limit, while doing the same in the controller costs nothing. You can even use @RemoteAction so you don't get charged the API call if you want to use actual JavaScript, but I don't see why this is necessary.

sunfishettesunfishette

Using this solution, I get an error telling me there is unexpected token "{" here -->

 

 new String{ String.valueof(a.id) } );

 

I did try modifiying the line using parens, but that didnt work.

I tried removing the curly brackets, that did not work.

I am not sure what to try next.

 

Anyone have thoughts on why this is not working?

 

Thanks!

sfdcfoxsfdcfox

My apologies, it should be:

 

new String[] { String.valueOf(a.id) } );

 

sunfishettesunfishette

Here is what I have done to solve the issue for anyone looking to do this in the future:

public String getImageName() { 
       String file = [SELECT Name 
                       FROM Account 
                       WHERE Id IN 
                            (SELECT AccountId 
                               FROM user 
                              WHERE username=:UserInfo.getUsername()
                             )
                       LIMIT 1].Name;
        
	file = file.replace(' ', ''); String foldername = 'Logos/'; 
	String extension = '.jpg'; 
	String fullImageName = foldername + file + extension; 
	return fullImageName;       
    }  

 I ran a query to get the name of the company based on the users login.  Then I stripped out the white spaces, and sandwiched it between the folder name and the image extension.

 

EXAMPLE:

Charlie Brown logs into his partner portal.  He works for Brown Ltd.  The query fetches "Brown Ltd" and returns it to the string "file".  Then the white space is stripped out to make it file = "brownltd"

then I append my foldername, file, and extension together to make one string as such:

"Logos/brownltd.jpg"

and I return that to my Visualforce page.

 

Works like a charm.  Now I have one common file for company Logos and I can display them based on who logs in.

This was selected as the best answer