2

I am retrieving records in the list and I want to check if the field Code__c is same for all the records. If not I want to give an error and make sure the field value is same for all the records in the related list.

Can any one please help me with this ?

Class :

List<String> code = new List<String>();

List<Object__c> ObjList = [Slect Id,Name,Codes__c from Object__c];

for(Object__c ob : ObjList )
{
    code.add(ob.Codes__c); 
}

//here i want to write logic

if(/*code Contains different values*/){
    ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Error');
}
1
  • See my updated answer, Lesser heap and CUP time! Jul 12 '16 at 4:43
4

Just cache the first one and then compare equality. It's worth noting that this approach as written below performs a case-insensitive comparison. If you want case-sensitivity, you're going to need to add in some logic to handle null values, so I elected to show the simpler pattern.

List<MyObject__c> records; // = someValue;
if (records.isEmpty()) { /* edge case */ }
final String code = records[0].Code__c;
for (MyObject__c record : records)
{
    if (record.Code__c != code)
    {
        // ApexPages.addMessage...
        break;
    }
}

You do not really need to store any any Boolean flags to indicate the result, let alone any of the actual codes in a collection. And you can stop looping as soon as you hit a match.

1
  • See my updated answer, Lesser heap and CUP time! Jul 12 '16 at 4:43
2

I feel below would be more optimized solution:

    Set<String> code = new Set<String>();

    List<Object__c> ObjList = [Slect Id,Name,Codes__c from Object__c];

    for(Object__c ob : ObjList )
    {

     code.add(ob.Codes__c);    
    if(code.size()>1){
    /*Code size will be more than 1 if there are different values in codes*/
    ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Error');
    break;
    }
}
3
  • Hard to say it's optimized when you don't have a break statement. Also you don't really need to cache anything at all.
    – Adrian Larson
    Jul 11 '16 at 15:02
  • Yes I understand that. Ideally we should break the loop as soon as code size grows to 2. But just thought that code list may be getting used somewhere. Revising answer. Jul 11 '16 at 15:06
  • @AdrianLarson Thanks for your response I really appreciate your help but the answer given by AtulRajguru9 is working that's why I accepted his answer,but anyways for optimized solution will try the logic you provided.
    – Learner
    Jul 11 '16 at 15:20
2

If i understand it correctly then you can use following code:

List<String> code = new List<String>();
Boolean ErrorFlag = flase;
List<Object__c> ObjList = [Slect Id,Name,Codes__c from Object__c];

for(Object__c ob : ObjList )
{    
 code.add(ob.Codes__c); 

}

//here i want to write logic
If(code.size() > 0){
    String first = code[0];
    for(String next: code){
        if(first != next) 
            ErrorFlag = true;
    }
}

if(ErrorFlag){
    ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Error');
}

EDIT: If you really need optimized solution for this specific problem defined above then i would do something like this:

list<AggregateResult> result = [select name, count(id) from account group by name having count(id) >1];
if(result.size() > 0){
    System.debug('>>> There is error');    
}else{
    System.debug('>>> No error!');    
}

When i check it with developer console analysis it took 15.42 milliseconds and heap used was 1430.

enter image description here

When I run code provided by Adrian Larson it took 21.69 milliseconds & heap used was 60732. enter image description here

PS: Following points need to to be considered
1) The index on the column will affect the performance
2) You may not be able to use result variable for showing data for example rendering table with row details
3) The result may vary depending on your schema and number of records

6
  • How would you know if code[0] is the correct code? :) Jul 11 '16 at 14:46
  • @ShaileshPatil want to make sure the code is same for all records that's it.
    – Learner
    Jul 11 '16 at 14:52
  • and that is what is requirement Jul 11 '16 at 15:17
  • Who ever down voted. please reconsider :) Jul 11 '16 at 15:19
  • This is just really wasteful of heap and inefficient. I guess saying it's "not useful" is a little strong, but the solution could be much improved.
    – Adrian Larson
    Jul 11 '16 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.