2

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 );  } }
  • I think that the problem here may be the user licence type and not lightning component. Try running this soql query on a VF page controller from this community user, and see if you get the same error... – Itai Shmida Sep 18 '16 at 4:46
  • I have tried running same soql query on VF page as well and it did't worked for that community user. – user3524041 Sep 21 '16 at 12:49
  • How do you run this query on lightning and how on VF page? please post both codes so we can see exactly what is executing. – Itai Shmida Sep 22 '16 at 4:44
  • I have posted the code sections used for lightning and for the VF page as well. Please have a look. – user3524041 Sep 22 '16 at 7:16
  • and do you get a good log from the line System.debug('##contactId: '+lstUser.ContactId) on both executes? – Itai Shmida Sep 22 '16 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.