Given: A Salesforce user is viewing an account page.
Desired Output: All Emails of Contacts related to the Account currently viewed as a List object.
My code:
SELECT Email FROM Contact WHERE Id IN (SELECT ContactId FROM AccountContactRelation WHERE AccountId = ApexPages.CurrentPage.getParameters().get('id'))
This does not retrieve any results. When using a fixed number instead of ApexPages.CurrentPage.getParameters().get('id'), the Emails are correctly returned.
I'm sort of new to Apex. Could anyone point out what I am doing wrong?
To achieve the desired output you need to use SOQL variable injection.
You can do this by creating a variable first and then referencing the variable inside the SOQL using : and the variable name:
String theAccountId = ApexPages.CurrentPage.getParameters().get('id');
List<Contact> theContacts = [SELECT Email FROM Contact WHERE Id IN (SELECT ContactId FROM AccountContactRelation WHERE AccountId = :theAccountId)];
You can use a static query with a bind variable to retrieve the correct results.
Additionally, the Contact object contains an AccountId field of its own. Therefore, depending on your setup, you may be able to eliminate your subquery. You may also want to filter out Email fields that are empty, since Email is not a required field.
The complete result could look something like this:
String accountId = ApexPages.CurrentPage.getParameters().get('id');
List<Contact> accountContactsEmailList = [
SELECT
Email
FROM
Contact
WHERE
Email != ''
AND AccountId = :accountId
];
for (Contact contact : accountContactsEmailList) {
System.debug(contact.Email);
}
Related
In Salesforce I would like to retrieve AccountIds where its related Contact FirstName must have values in list of String. List of string is {'Test','User'}. I would need to find all Accounts where Contact first Name has both Test and User as related Contacts,
I am trying as below but the below query will show accounts where even 1 value matches as Contact First Name.
List<String> names = new List<String>{'Test','User'}; List<Account> accountList = [ Select Id from Account Where Id IN (Select AccountId FROM Contacts where FirstName LIKE :names)];
Please help
SELECT Id, Name
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact WHERE LastName = 'Test')
AND Id IN (SELECT AccountId FROM Contact WHERE LastName = 'User')
But it'll work only 2 times, you can't write 3rd "IN" like that (see here).
If you need a more generic solution that can take lists of any size you'd have to run "my" queries in loop, 2 names at a time, save results to some Set<Id> or Map, play with functions like myset.retainAll(idsFromCurrentLoopQuery)... Of course query in a loop is bit evil too.
I have an account field(lookup field to account) and a user lookup field called ALO on contact object.What I want to do is to find out the account name field value on contact object, traverse that account, fetch the owner id and then assign that to the ALO field on the contact object.
This is what I have written in my apex controller but I am getting some syntax error maybe because of the API names that I am using. Can anybody help, please?
public Account acc {get; set;}
acc = [ SELECT Id, OwnerId
FROM Account
WHERE Id =: contact.Account
];
contact.ALO= acc.OwnerId;
where 'contact' is the current contact instance.
Since ALO is a custom field you will have to append __c to the API name to access it. It would be contact.ALO__c = acc.OwnerId.
Also, you should be querying the AccountId field on Contact. Otherwise you will be getting an "No such column 'Account' on entity 'Contact'." error.
I would like to retrieve the related account for an contact resource from saleforce API. I have tried the below query to retrieve account which has contact Id. But not able to get my requirement.
SELECT+Id,Name+from+Contact+where+Id+IN+(SELECT+AccountId+FROM+Contact+where+Email='xxx#gmail.com')
Anyone , please help.
You are actually trying to correlate Id from Contact object and Id from Account object. You can use the relations :
select account.name, account.id from contact where email='xxx#gmail.com'
You are trying to retrieve Account for Contact. You have Email id based on you want Account details.
If you want to Account id then use AccountID field also in your Query:
Your query will like this:
List<Contact> contactList = [Select id, name, AccountID
from contact where Email = 'test#gmail.com'];
for(Contact con : contactList) {
System.debug('Account id : ' + con.AccountID);
}
How can I Query all Accounts a given user has read permission for?
I tried the following but, returned the error "semi join sub selects can only query id fields, cannot use: 'RecordId'"
User u = new User();
Account[] account = [SELECT Name FROM Account a
WHERE Id IN
(
SELECT RecordId
FROM UserRecordAccess
WHERE RecordId = :a.Id
AND UserId = :u
AND HasReadAccess = true
)
];
The code is being executed as part of a scheduled batch job run as system so use of "with sharing" is not applicable.
Thanks
The salesforce documentation says that RecordID is a picklist field which seems odd. If it is a picklist field that might explain why it does not work in a subquery.
You could try building a set of RecordIDs first and then using this to filter the Account query.
Set<ID> sRecordIDs = [SELECT RecordID FROM UserRecordAccess WHERE UserId = :u AND HasReadAccess = True];
Account[] accs =[SELECT ID,Name FROM Account WHERE Id in :sRecordIDs];
This might fall over governor limits if the number of records in UserRecordAccess is high.
If you are only looking to do this for Account you could try this:
User u = new User();
list<Account> accs = [select Id, Name from Account where Id in (select AccountId from AccountShare where UserOrGroupId = :u.Id) or OwnerId = :u.Id];
That should give you all of the account records that the user has access to or owns. In the general case each sObject has its own 'Share' sObject that represents visibility against it, unless it is degenerate in some way (i.e. it is the detail in a master-detail).
i have SOQL query which queries for some records based on a where condition.
select id, name,account.name ... <other fields> from opportunity where eventname__c='Test Event'
i also need to get the related contact details for the account in the opportunity. ie i need to add the email ids of contact who all are part of the account in the opportunity.
For each opportunity, i need to get all the contacts emailids who are associated with the account in opportunity.
I cant really figure out how to approach this.
referring the documentation i can get the contact info of a account using the query
SELECT Name,
(
SELECT LastName
FROM Contacts
)
FROM Account
How can i use this along with opportunity?
Thanks
The problem is that you are trying to traverse up from opportunity to its parent (account) and then back down to the children (contacts).
I think you will have to do it in two stages, e.g. roughly like:
id[] accountids = new id[]{};
for (opportunity opp : [select accountid from opportunity where eventname__c='Test Event'])
{
accountids.add (opp.accountid);
}
account[] acclist = [select name, (select email from contacts) from account where id in :accountIds];