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! Commented Jul 12, 2016 at 4:43

3 Answers 3

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! Commented Jul 12, 2016 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. Commented Jul 11, 2016 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. Commented Jul 11, 2016 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. Commented Jul 11, 2016 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? :) Commented Jul 11, 2016 at 14:46
  • @ShaileshPatil want to make sure the code is same for all records that's it. Commented Jul 11, 2016 at 14:52
  • and that is what is requirement Commented Jul 11, 2016 at 15:17
  • Who ever down voted. please reconsider :) Commented Jul 11, 2016 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. Commented Jul 11, 2016 at 15:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.