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
Carter85Carter85 

Need Help Comparing User Input to a Table of Values

I have a custom object called Car_Class__c and in it I have used a multi-select picklist to create a table of values in a field called Make__c for different vehicle makes. (i.e. Buick; Chevrolet; Chrysler; Dodge; Fiat; Ford; GMC; Honda; Hyundai; Isuzu; Jeep; Mazda; Mercury; Mitsubishi; Nissan; Oldsmobile; Plymouth; Pontiac; Ram; Saturn; Scion; Suzuki; Toyota;)

The same Car_Class__c object has fields called Class_c, Product_Group__c, and Insurer_c, and the Class_c field is meant to be based on the vehicle make for certain situations, like if Insurer = VS AND Product Group = MPP then Buick = Class 1, and in that same situation if it was an Acura then = Class 2, etc.).  What I'm having trouble with is taking user input from a field called Vehicle_Make__c from a drop down list and comparing it with the values in the multi select list, so if anyone can offer a suggestion on how to fix this I would be grateful.  This is what I have been trying so far, where the 'I' and "prod' variables are taken from earlier calculations involved in other things so I know they're right and the cl and cl2 are public lists of the Car_Class__c object:

Public String getClass(){
        cl = Database.query('SELECT Make__c FROM Car_Class__c WHERE Insurer__c =:I AND Product_Group__r.Name =: prod');
        List<String> avC = New List<String>();
        for(Integer i=0; i < cl.size(); i++){
        avC.add(cl[i].Make__c);
        }
        String strList = String.valueOf(avC);
        List<String> c = strList.splitByCharacterTypeCamelCase();
        if (strList != null){
        cl2 = [SELECT Class__c FROM Car_Class__c WHERE (Insurer__c =:I) AND (Product_Group__r.Name =: prod) AND (Make__c IN:c)];
        contract.Class__c = string.Valueof(cl2[0].Class__c);
        }
        return contract.Class__c;    
                            }

 So far this returns an List index out of bounds:0 error.

Alex.AcostaAlex.Acosta
can you point out the line in which you are getting an out of bound exception?
Carter85Carter85

I'm not entirely sure, because when I set up errors to trigger if any of the lists were empty they didn't display properly, but I think it's an issue with this line

cl2 = [SELECT Class__c FROM Car_Class__c WHERE (Insurer__c =:I) AND (Product_Group__r.Name =: prod) AND (Make__c IN:c)];

 because I have this same basic structure set up for another method meant to search a different object in the same manner, which does work, and this line is the only thing which differs between them.

Alex.AcostaAlex.Acosta

You should be getting an exception if no records are found when making that query.

Alex.AcostaAlex.Acosta

Exception Type Reference Link: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_exception_methods.htm

 

QueryException - Any problem with SOQL queries, such as assigning a query that returns no records or more than one record to a singleton sObject variable.

Carter85Carter85

Ok, I set it up after fixing my try-catch for an exception, like so:

Public String getClass(){
        try{
        cl = [SELECT Make__c FROM Car_Class__c WHERE Insurer__c =:I AND Product_Group__r.Name =: prod];
        List<String> avC = New List<String>();
        for(Integer i=0; i < cl.size(); i++){
        avC.add(cl[i].Make__c);
        }
        String strList = String.valueOf(avC);
        List<String> c = strList.splitByCharacterTypeCamelCase();
        if (strList != null){
        cl2 = [SELECT Class__c FROM Car_Class__c WHERE (Insurer__c =:I) AND (Product_Group__r.Name =: prod) AND (Make__c IN:c)];
        contract.Class__c = string.Valueof(cl2[0].Class__c);
        }
        }
        catch(System.DmlException e){
        ApexPages.Message message = new ApexPages.Message(ApexPages.Severity.ERROR, 'Error on line' + e.getLineNumber());
                    ApexPages.addMessage(message);
        }
        
        return contract.Class__c;    
                            }

 and it looks as if I was right because the line it indicates is the

contract.Class__c = string.Valueof(cl2[0].Class__c);

 meaning it's the cl2 list query which is for some reason not pulling in a result, so perhaps the str list isn't separating the table values properly for some reason.  Anyone have any thoughts?

Alex.AcostaAlex.Acosta

My question to you, and maybe it's just because I don't see it, but avC is a list of String in which you are gathering all the Makes, the you cast that List as a string...

List<String> avC = New List<String>();
for(Integer i=0; i < cl.size(); i++){
avC.add(cl[i].Make__c);
}
String strList = String.valueOf(avC); <--- I don't get this.... why not do this within your loop above?

 

 

Either way, I did an example run of this to see what you would get and I highly suggest debuging all your varibles, and especially  'c'... use eclipse, developer console, work bench, etc, and write out your query using your results to test out why you aren't getting back any results

 

Here's my test:

List<String> testList = new List<String>{'oneTwo', 'threeFour', 'fiveSix', 'sevenEight'};
String strList = String.valueOf(testList);
system.debug(strList);
List<String> c = strList.splitByCharacterTypeCamelCase();
system.debug( c );

 

Results:

25.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: List<String> testList = new List<String>{'oneTwo', 'threeFour', 'fiveSix', 'sevenEight'};
Execute Anonymous: String strList = String.valueOf(testList);
Execute Anonymous: system.debug(strList);
Execute Anonymous: List<String> c = strList.splitByCharacterTypeCamelCase();
Execute Anonymous: system.debug( c );
09:46:35.036 (36803000)|EXECUTION_STARTED
09:46:35.036 (36812000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
09:46:35.037 (37321000)|SYSTEM_CONSTRUCTOR_ENTRY|[1]|<init>()
09:46:35.037 (37349000)|SYSTEM_CONSTRUCTOR_EXIT|[1]|<init>()
09:46:35.037 (37644000)|USER_DEBUG|[3]|DEBUG|(oneTwo, threeFour, fiveSix, sevenEight)
09:46:35.037 (37741000)|USER_DEBUG|[5]|DEBUG|((, one, Two, ,, , three, Four, ,, , five, ...) <-- that's not a very pretty string. For example... one of your values is an open and another value is a close parenthathese

Carter85Carter85

Ok, the previous method was pretty convoluted, so I've been trying a new avenue.  I've got this:

Public String getClass(){
try{
strList = String.valueOf(contract.Vehicle_Make__c);
c = strList.splitByCharacterTypeCamelCase();
cl = [SELECT Class__c FROM Car_Class__c WHERE Insurer__c =:I AND Product_Group__r.Name =:prod AND (Make__c IN:c)];
contract.Class__c = cl[0].Class__c;
}
catch(System.Exception e){
ApexPages.Message message = new ApexPages.Message(ApexPages.Severity.ERROR, 'Error on line' + e.getLineNumber() + c);
ApexPages.addMessage(message);
}
return contract.Class__c;
}

 Yet the error still lies when I try to pull the class with:

cl = [SELECT Class__c FROM Car_Class__c WHERE Insurer__c =:I AND Product_Group__r.Name =:prod AND (Make__c IN:c)];

 because even though the list I'm comparing to the picklist has only one value (ex: Hyundai), the cl comes up null for some reason.  Is there a better way to compare the contract.Vehicle_Make__c field selection to the multi-select list in Make__c?  Because it doesn't seem like it's comparing the values correctly.  Or, conversely, is there a field type that I could change the Make__c to in order to make the comparison easier?  Because I've tried emptying the list of all other values but one and it'll find it then, or just creating a text field with one value in it, but those are rather inefficient ways of going about it.

Carter85Carter85

Never mind.  I re-did my tables in a new object and simplified things so I don't need this fixed anymore.

Alex.AcostaAlex.Acosta

Multi-pick list values are saved in a String format seperated semi-colons similar to how a list seperates it's values with commas...

 

IE MultiList Example

(multiple values)

Color__c = 'red;blue;green;yellow;'

(single value)

Color__c = 'red;'

 

 

Try querying your record back out and looking at how your values are saved.