function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dave The RaveDave The Rave 

compare 2 the value of standard fields and count records

As a an admin, I am using the Developer Console to execute an SOQL query. The aim is to count the number of records on the lead object where the text fields  "Name = firstname and lastname" and "Company" have the same values.

I wrote this query, but got an error

SELECT Id, firstname, lastname FROM lead WHERE [lastname + firstname = Company]

After searching Google, it seems that the goal is not achievable using SOQL, can anyone confirm this? is there another simple solution using Apex run from the developer console?

For example firstname & lastname = "Peter Smith" AND Company = "Peter Smith",  so this is one record where the name and company are the same.

Thanks,

Dave
Best Answer chosen by Dave The Rave
Abhishek BansalAbhishek Bansal
Hi Dave,

You can run the below script in Execute Anonymous window and than check the debug logs to get the count:
Integer countUniqueRecords = 0;
for(Lead ld : [Select Name, Company from Lead]) {
	iF(ld.Name == ld.Company) {
		countUniqueRecords++;
	}
}
system.debug('Number of Unique Records =='+countUniqueRecords);

Let me know if you need any other help on this.

Thanks,
Abhishek Bansal.​​​​​​​

All Answers

Abhishek BansalAbhishek Bansal
Hi Dave,

You can run the below script in Execute Anonymous window and than check the debug logs to get the count:
Integer countUniqueRecords = 0;
for(Lead ld : [Select Name, Company from Lead]) {
	iF(ld.Name == ld.Company) {
		countUniqueRecords++;
	}
}
system.debug('Number of Unique Records =='+countUniqueRecords);

Let me know if you need any other help on this.

Thanks,
Abhishek Bansal.​​​​​​​
This was selected as the best answer
Dave The RaveDave The Rave
Hi Abhishek, Thanks for your help this works like a dream. But in the log how can I just view a list of records that meet the criteria and not see every record in the system. So a list of all records where Name = Company. Even better would be to get this list in a text or excel file, I am not sure if you can get debug logs out the system. Regards, Dave
Abhishek BansalAbhishek Bansal
Hi Dave,

There can be many options to get the list in excel:
  1. You can send an email from console by adding all the records in a file and than adding this file in email.
  2. You can get the ids of the records that meets the criteria and after that you can generate a report on those ids.
If you got the solution than please close this question by choosing a best answer. Let me know if you need any other help on this.

Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102