You need to sign in to do that
Don't have an account?
Allen Manamel
How to retrieve account object through SOQL Query
Hi,
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.
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.
Can you please modify the SOQL as below and try again.
acc = [
SELECT Id, OwnerId
FROM Account
WHERE Id =: contact.accountid
];
contact.ALO__c= acc.OwnerId;
Also, if ALO is custom lookup field then check the API name of ALO field and use that in code.
Regards,
Sagar
All Answers
Can you please modify the SOQL as below and try again.
acc = [
SELECT Id, OwnerId
FROM Account
WHERE Id =: contact.accountid
];
contact.ALO__c= acc.OwnerId;
Also, if ALO is custom lookup field then check the API name of ALO field and use that in code.
Regards,
Sagar
acc = [
SELECT Id, OwnerId
FROM Account
WHERE Id =: contact.accountid
LIMIT 1];
contact.ALO__c = acc.OwnerId;
Error: Error occurred while loading a Visualforce page.
Also would it be public Account acc {get; set;} or
Account acc = [
SELECT Id, OwnerId
FROM Account
WHERE Id =: contact.accountid
LIMIT 1];
contact.ALO__c = acc.OwnerId;
<apex:page controller="Page4Controller" docType="html-5.0" showHeader="false" sidebar="false" standardStylesheets="false" html-lang="en-US">
<apex:commandbutton value="Submit Application" id="theButton" action="{!submit}"/>
Here is my Page4Controller:
public Contact contact {get; set;}
public Account acc {get; set;}
public Page4Controller() {
User currentUser = [SELECT contactId FROM User WHERE Id = :UserInfo.getUserId()];
System.debug('Current user' + currentUser);
if(String.isNotBlank(currentUser.ContactId)) {
contact = [
SELECT firstName, lastName, MiddleName, Nickname__c, Suffix, MailingStreet, MailingCity,
MailingStateCode, MailingPostalCode
FROM Contact
WHERE Id = :currentUser.ContactId
LIMIT 1
];
System.debug('contact information: ' + contact );
application = [
SELECT Id, Contact__c, Attend_College_after_High_School__c
FROM Application__c
WHERE Contact__c =: currentUser.contactId
LIMIT 1];
}
}
public pagereference submit() {
if(application.Attend_College_after_High_School__c == 'No')
{
acc = [
SELECT Id, OwnerId
FROM Account
WHERE Id =: contact.accountid
LIMIT 1];
contact.ALO__c = acc.OwnerId;
}
update contact;
Pagereference Page = new Pagereference('Page_4');
Page.setRedirect(true);
return Page;
}
Note: I am accurately getting contact and application object data.
if(application.Attend_College_after_High_School__c == 'No')
{
acc = [
SELECT Id, OwnerId
FROM Account
WHERE Id =: '001r0000008ZxQp'
];
contact.ALO__c = acc.OwnerId;
}
so something is wrong here
WHERE Id =: contact.accountid
I was not fetching accountid while retrieving the contact object from database.
Following is my final working code
if(String.isNotBlank(currentUser.ContactId)) {
contact = [
SELECT firstName, lastName, MiddleName, Nickname__c, Suffix, MailingStreet, MailingCity,
MailingStateCode, MailingPostalCode, AccountId
FROM Contact
WHERE Id = :currentUser.ContactId
LIMIT 1
];
if(application.Attend_College_after_High_School__c == 'No')
{
acc = [
SELECT Id, OwnerId
FROM Account
WHERE Id =: contact.AccountId
];
contact.ALO__c = acc.OwnerId;
}
https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=9060G0000005XFoQAM