I have been working on developing salesforce community using community builder with lightning components. I am using rich textbox field on Contact Object to store profile picture which is indeed refering to an attachment on Contact. I need to show this Attachment on profile page for logged in community user.
Here is my SOQL query
Select Id, Name, ContentType from Attachment
where parentid=:ContactId and ContentType in ('image/png', 'image/jpeg', 'image/gif')
I was using to get an attachment. It's working fine when I executed this on developer console. But it's returning null value when it get executed for community user, hence can't access attachment on lightning page.
Here is the apex controller method that I was calling to get an attachment for lightning component:
public static List<Attachment> getContactImage(){
User lstUser = [Select Id, contactId, Name, Email, Phone, username, companyname, title, mobilephone from User where Id=:userinfo.getuserId()];
List<Attachment> attachments = new List<Attachment>();
if(lstUser.ContactId !=null)
{
System.debug('##contactId: '+lstUser.ContactId);
attachments = [select Id, Name, ContentType from Attachment
where parentid=:lstUser.ContactId and ContentType in ('image/png', 'image/jpeg', 'image/gif') order by createddate desc limit 1];
if(!attachments.isEmpty()){
return attachments;
}
}
return null; }
On VF page I was executing same query in constructor of that Apex controller. Below is the code:
public CommunityContactDetailPageController(){
lstUser = [Select Id, contactId, Name, Email, Phone, username, companyname, title, mobilephone from User where Id=:userinfo.getuserId()];
if(lstUser.contactId!=null){
contactObj =[select id, name, mailingstreet , fax, mailingcity, mailingcountry,mailingcountrycode, mailingstate,mailingstatecode, mailingpostalcode, phone, email, accountId, title, mobilephone from contact where id=:lstUser.contactId];
//get contact Image
System.debug('##contactId: '+lstUser.ContactId);
attachments = [select Id, Name, ContentType from Attachment
where parentid=:lstUser.ContactId and ContentType in ('image/png', 'image/jpeg', 'image/gif') order by createddate desc limit 1];
System.debug('##Attachments: '+attachments ); } }