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
krishna casukhela 7krishna casukhela 7 

compare two fields in salesforce

Hello
I have a urgent requirement as follows.
There is historical data in account object , it has 2 million records.
Account object has two fields with same data type say shipcode and billcode and I need to compare values in these two fields.
I am thinking of batch apex but if the matching rows are say 1Lakh then I cannot store in a list variable.
somebody told me about formula field but I am not sure how to go about this.
The aim is to create a report by including the formula field, but I am assuming about 1Lakh matching can turn up,
Please advise , elaborate wherever possible

Thanks
Krishna

 
Sridhar NarayansaSridhar Narayansa
Did you check this. The solution is readily available.

https://developer.salesforce.com/forums/?id=906F00000008wuFIAQ
Akhil AnilAkhil Anil
Hi Krishna,

You can definitely use a formula field for this. The formula can compare the values in these two fields and give an output. However, can you elaborate more on what exactly do you want to compare and what should be the outcome so that we can help you build the formula ?
krishna casukhela 7krishna casukhela 7
Hi akhil
a) First , The idea is to build a report by including the formula field in the report.

so can u help me frame the formula field?

b) alternatively I could do something like this
    use a loop , if the fields match capture them into a list variable , provided number of rows returned are less than 50000 to avoid hitting governor
    limit, once in list variable then some other logic

c) pls clarify for me
   assume I have 2 million historical records already
   Now creating a new formula field won't help because then only new rows values can be added not the old ones

   also if number of rows returned is greater than 50000 what should be the approach


Thanks
Krishna



 
EldonEldon
Hi krishna,

You can update all records by executing code in anonymous window or batch and also for records above 50000 you can put the query in the For loop like below,
 
For(account a : [select id,field1,field2,Resultfield from account]){
   //do your manipulations and store it in a list and update the list outside loop
}
There is no limit on the number of items a collection can hold. However, there is a general limit on heap size.

You can get more than 50,000 using a Query Locator.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_batch.htm
This documentation says you can query up to 50 million records:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm
however, query locators have some risks where are well documented here:https://help.salesforce.com/apex/HTViewSolution?id=000004410&language=en_US

Regards