0

From the below code i can get the all Accounts linked to that contact(community user).but here the problem is i'm using the email id as static how can i make this dynamic.once the community user login it it display all accounts linked with him.

public class Test{

 @AuraEnabled
    public static List<Opportunity> getAccounts(){

    id[] accountids = new id[]{};
     for (Contact con : [select accountid from Contact where mail='xxxxx.com']){
      con.add (opp.accountids );}
account[] acclist = [select name, (select email from contacts) from account where id in :accountids ];
System.debug(acclist);}
}
0

The Community User will have an associated Contact/Account, so if you query the User table for the AccountId, you should be able to get the details you're after.

Given you want all Accounts where there is a Contact with the email address of the community user, you can do the following:

// get the community user's email address
String communityUserEmail = [SELECT Email FROM User WHERE Id = :UserInfo.getUserId()].Email;

// get all accounts that have contacts with this email address
List<Id> accountIds = new List<Id>();
for(Contact theContact : [SELECT AccountId FROM Contact WHERE Email = :communityUserEmail])
{
    accountIds.add(theContact.AccountId);
}

// query the accounts
List<Account> = acclist = [select name, (select email from contacts) from account where id = :communityUserAccountId];

I haven't tested this as I don't have a community set up.

You'll probably want to put in some more robust error handling as well.

  • @thanks nick for giving the reply but here Id communityUserAccountId = [SELECT AccountId FROM User WHERE Id = :UserInfo.getUserId()].AccountId; in this line i'm getting only one account id of community user but my requirement is if the same user has also on the other account.for that i'm using email is common then how can i retrieve all contacts – Raj Nov 20 '17 at 2:11
  • @Raj updated accordingly. – Nick Cook Nov 20 '17 at 2:56
  • thanks for updating the answer but i changed as you suggested but once user login still it shows one account only. – Raj Nov 20 '17 at 3:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.