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
Amol_NikamAmol_Nikam 

add image

Hi all ,

I have one scenario in which we want to provide facility to user to add image in custom object.is it possible?

if yes then how we implement this as user will add content and image in salesforce object and we want to show this on our site fom object. we want to store content and image in the object

 

thanks,

AMOL

Sagar PareekSagar Pareek

Hi Anmol, You can upload image as an attacment to the object as follows

 

To show the photo you have use a formula field on that object and then expose that field on the vf page as normally we do  , use  formula

IMAGE("https://c.ap1.content.force.com/servlet/servlet.FileDownload?file=" & AttachmentId__c , "No Attachments",100,100)

the link is hardcoded link which will point to the location where the salesforce stores the attachment

where 100,100 are the size of image

 

you can upload the image as follows , its the complete code you will find the upload code in it.

----Controller-----

public class insertmyCandidateController {
public Candidate__c candidate { get; private set; }
public blob Photo {get;set;}
public String contentType {get; set;}
public String fileName {get; set;}
public insertmyCandidateController() {
Id id = ApexPages.currentPage().getParameters().get('id');

candidate = (id == null) ? new Candidate__c() :[SELECT email__c FROM Candidate__c WHERE Id = :id];
}
public PageReference save(){
try {
upsert(candidate);
} catch(System.DMLException e) {

return null;
}
//for the upload
if (Photo != null) {
Attachment attach = new Attachment();
attach.Body = Photo;
attach.Name =candidate.email__c+'.jpg';
attach.ContentType = contentType;
attach.ParentId = candidate.id;



try {
insert(attach);
candidate.attachmentid__c=attach.Id;
update(candidate);
} catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
}   
           PageReference page = new PageReference('/apex/profile?id='+candidate.id);
        page.setRedirect(true);
        return page;
}
}