No such column 'Owner' on entity 'Lead' - salesforce

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]

Related

Who to access field of nested relationship in soql where first relation is simple and next is polymorphic

I want to access custom field of Owner (User) fields where Owner is polymorphic relation it can be either User or Group.
I tried following query:
SELECT Id, Email, Name, Lead.TYPEOF Owner WHEN User THEN custom__c FROM CampaignMember WHERE CampaignId='xxxxxx'
Relationship stack is:
Lead > Owner (User,Group) > custom__c.
SELECT field1__c,
field2__c
FROM Lead
WHERE Owner.type = 'User'
AND CampaignID = 'xxxxxx'
EDIT:
Leaving my original answer in case it helps someone else.
I believe what you are looking for is this:
SELECT Id, Email, Name,
TYPEOF Lead.Owner
WHEN User THEN custom__c
END
FROM CampaignMember
WHERE Field != 'value'

How to retrieve account object through SOQL Query

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.

Attachments in Apex SOQL Subquery

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

Salesforce - Apex - query accounts based on Activity History

Hey Salesforce experts,
I have a question on query account information efficiently. I would like to query accounts based on the updates in an activityHistory object. The problem I'm getting is that all the accounts are being retrieved no matter if there's "complete" activeHistory or not. So, Is there a way I can write this query to retrieve only accounts with activeHistory that has status="complete" and Type_for_reporting='QRC'?
List<Account> AccountsWithActivityHistories = [
SELECT
Id
,Name
,( SELECT
ActivityDate
,ActivityType
,Type_for_Reporting__c
,Description
,CreatedBy.Name
,Status
,WhatId
FROM ActivityHistories
WHERE Status ='complete' and Type_for_Reporting__c = 'QRC'
)
FROM Account
];
You have a WHERE clause on the histories but you still miss one on the Account level.
For example this would return only Accounts that have Contacts:
SELECT Id, Name
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact) // try with NOT IN too
With Activities it's trickier because they don't like to be used in WHERE in that way.
http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select.htm
The following objects are not currently supported in subqueries:
ActivityHistory
Attachments
Event
EventAttendee
Note
OpenActivity
Tags (AccountTag, ContactTag, and all other tag objects)
Task
Additionally the fine print at the bottom of ActivityHistory definition is also a bit discouraging.
The following restrictions on users who don’t have “View All Data” permission help prevent performance issues:
In the main clause of the relationship query, you can reference only
one record. For example, you can’t filter on all records where the
account name starts with ‘A’; instead, you must reference a single
account record.
You can’t use WHERE clauses.
You must specify a limit of 499 or fewer on the number of rows returned in the list.
You must sort on ActivityDate in ascending order and LastModifiedDate in descending order; you can display nulls last. For
example: ORDER BY ActivityDate ASC NULLS LAST, LastModifiedDate DESC.
Looks like you will need multiple queries. Go for Task (or Event, depending for which the custom field is visible), compose a set of AccountIds and then query the Accounts?
Or you can manually filter through list from your original query, copying accounts to helper list:
List<Account> finalResults = new List<Account>();
for(Account a : [SELECT...]){
if(!a.ActivityHistories.isEmpty()){
finalResults.add(a);
}
}

which value should pass to Lookup Relationship field?

I create a Employee object where I want to show Lookup Relationship field for Lead. Employee records are coping from Lead. so how to do ?
Actully I wrote in class,
emp.Source_Lead__c = lead.id; // emp is Employee object & lead is Lead object.
but it shows me Name, CompanyName in lookup so it wont access.
like :
Source Lead : eabc exyz, ecompany
Help me....
If you're talking about new employee / edit employee form - yes, you pass an Id. You're doing it right. The tricky part is that you can't immediately use "dots" to show fields from Lead.
After establishing the link and saving you will have to query for lead fields. Something like this (your question isn't too clear, I don't know if this is on one page or 2).
Employee__c emp = new Employee__c();
// ... fill fields as needed
emp.Source_Lead__c = lead.Id;
insert emp;
// And later you can query
emp = [SELECT Id, Name, Source_Lead__c, Source_Lead__r.Name, Source_Lead__r.Custom_Field__c
FROM Employee__c WHERE Id = :emp.Id];
System.debug(emp.Lead_Source__r.Name); // now it will work

Resources