the following Salesforce article says that:
When working with SOQL queries, maps can be populated from the results returned by the SOQL query. The map key should be declared with an ID or String data type, and the map value should be declared as an sObject data type.
Assume on the Account object I have a field of type text that is unique called uniquetext__c, how can this be achieved:
Map<string, Account> map_acc = new Map<string, Account>([select uniquetext__c, name, customField1, customField 2 from Account limit 10]);
My expectation is to have a map between uniquetext__c and the Account sObject, rather than the ID and the Account sObject
Unfortunately, you can't set what field will be the key when constructing a Map from a SOQL query (AFAIK). But to do it manually all you have to do is...
Map<String, Account> map_acc = new Map<String, Account>();
for(Account a : [SELECT uniquetext__c, name, customField1, customField 2 from Account limit 10]){
map_acc.put(a.uniqueText__c, a);
}
Related
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.
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);
}
I have a trigger for insert on Opportunity:
trigger OpportunityInsertTrigger on Opportunity (before insert) {
System.debug('Triggered on insert of opportunity. Updating blockchain...');
for (Opportunity opp : Trigger.new) {
List<DNBCompany__c> retrievedCompany = [SELECT Id, Name, Duns__c FROM DNBCompany__c WHERE Id = :opp.DNBCompany__c];
DNBCompany__c company = retrievedCompany.get(0);
String duns = (String) company.get('Duns__c');
}
}
I have a custom object called DNBCompany that is linked via a Lookup relation (DNBCompany__c) in Opportunity.
How can I retrieve the DNBCompany associated with opp?
[EDITED to reflect answer below]
You could do something like this :
for (Opportunity opp : Trigger.new)
{
DNBCompany__c retrievedCompany = [SELECT Id, Name FROM DNBCompany__c WHERE Id = :opp.DNBCompany__c];
}
Since the lookup field will be the Id of the company you want to retrieve, you can use an inline SOQL query to get the associated object back.
Keep in mind, you'll have to reference any additional fields you wish to process on in your SOQL query.
I have custom objects Team member and Employment, and there's a lookup relationship from employment(many) to team member(one), plus another lookup record on team member called current employment.
The employment record may have attachments.
I want a SOQL query, to run in an APEX class, which will return attachments information for specific team members.
So far I have this:
SObject[] results = [select id,(select id,name from Attachments) from Employment__c where id in (select Current_Employment__c from Team_Member__c where id=:id)];
Wnen I run the query in the schema browser, it works OK and I'm able to drill-down to the attachments, but when I run it in Apex (Anonymous), the result set does not contain the attachments:
for (SObject result : results) {
System.debug(result);
}
I can only see the Employment id in the results.
How can I get attachments in APEX?
You do following to get list of attachment related to that object.
Employment__c[] results = [select id,(select id,name from Attachments) from Employment__c where id in (select Current_Employment__c from Team_Member__c where id=:id)];
for (Employment__c result : results) {
if(result.Attachments!=null){
List<Attachment> AttachmentList=result.Attachments;
}
}
i am getting the following error
Error: Compile Error: No such column 'Owner' on entity 'Lead'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. at line 8 column 17
trigger Lead_Assignment on Lead (after insert) {
List<Lead> le=trigger.new;
User us=le.get(0).CreatedBy;
List<User> u=[SELECT id from USER WHERE At_Work__c=true];
List<integer> ii=new List<Integer>();
for(User uu:u){
Integer xx= [SELECT count() from Lead WHERE Owner=:uu];
}
}
there is a field with name Owner in Lead Object why i am getting this error please help in solving this error.
I think you would be better off with a query similar to below:
Integer xx= [SELECT count() from Lead WHERE OwnerId=:uu.Id];
The Owner field on the lead object is actually a reference to the owner user object. Typically this gets populated if you do a query similar to below:
[SELECT id, Owner.Name, Owner.Email FROM Lead]
And to access the Owner fields you would access the lead object owner property:
Lead myLead = [SELECT Id, Owner.Name, Owner.Email FROM Lead limit 1];
System.debug(myLead.Owner.Email);
However, there is a larger issue here. It isn't best practice to query within a loop like this. You are better off using an aggregate query like:
AggregateResult[] results = [SELECT COUNT(ID), OwnerId FROM LEAD GROUP BY OwnerId WHERE OwnerId IN :listOfUserIds]