How to retrieve account object through SOQL Query - salesforce

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.

Related

__r.Name from Account is empty

In APEX class we have SOQL:
Case carrierList= [SELECT Id, CaseNumber, Current_Quoted_By_Carrier__c, Current_Quoted_By_Carrier__r.Name, Quoted_By_Carrier__c, Quoted_By_Carrier__r.Name FROM Case WHERE Id = :con.Id];
When we run SOQL in Workbench or Developer Console for specific recordId the Name is populated, but in APEX SOQL returns record Id, but no Name (DEBUG|Quoted by Carrier Name = null).
Case.Quoted_By_Carrier__c is a lookup on Account.
Check to make sure the profile has access to the account record and that the profile has read permissions on the account name field.

Get Contact Emails of Currently Active Account as List

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);
}

Salesforce SOQL query to retrieve related accounts for an contact

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);
}

Salesforce Query to Check the Existing Contact with Account id

I already have contact by using Accountid, so before creating the new contact I want to verify the existing contact with email and id, can you please help me with this salesforce query to check if the Contact already exist with Contact Email and AccountId?
I already tried using the below query but it was throwing exceptions:
SELECT Id, Name , email
FROM Contact
WHERE email='XXX#Email.com'
AND Id IN (SELECT ContactId
FROM AccountContactRelation
WHERE AccountId = 'XXXX')
Got the below error:
INVALID_TYPE: and Id IN (SELECT ContactId FROM AccountContactRelation
WHERE AccountId
^
ERROR at Row:1:Column:121 sObject type 'AccountContactRelation' is not
supported. If you are attempting to use a custom object, be sure to
append the '__c' after the entity name. Please reference your WSDL or
the describe call for the appropriate names.
Can you please share the correct Salesforce Query ?
The below Query worked like charm :)
SELECT Id, Name , email
FROM Contact
WHERE email='XXXXXXXXX#XXX.com'
AND AccountId='YYYYY#yyy.com'

No such column 'Owner' on entity 'Lead'

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]

Resources