You need to sign in to do that
Don't have an account?
Compare two strings to build a select option list
I am attempting to build a select option list out of two different strings which refer to two separate multi-value fields. Here is the (non-functioning) code which I have right now.
if(selectedMulPickKeyTech==null) { options.add(new SelectOption('','')); return options; } else { picklistlines = selectedMulPickKeyTech.split('\n'); for (Integer i=0;i<picklistlines.size();i++) { String[] inputvalues = new String[]{}; inputvalues=picklistlines[i].split(','); for(Integer j=0;j<inputvalues.size();j++) { if(!selectedItems.contains(inputvalue)) options.add(new SelectOption(inputvalues[j],inputvalues[j] )); } } return options; } }
selectedMultiPickKeyTech is a string which I got back from a query referring to another record's multi-value field.
selectedItems is a string which refers to a multi-value field on the current record.
As you can see in the bold\underlined lines, I was trying to use Contains as a way to get it but that wouldn't work with a string value.
I would like to have the select options only be the values which are not already in selectedItems.
Does anybody know the proper way to achieve this ?
Thank you very much for your help.
Zoom,
You can split selectedMulPickKeyTech using the delimiter that separates items in a multi pick list (I believe it's a semicolon). If you print it using debug you'll know
Try
for (String item : selectedMulPickKeyTech.split(';') ) {
if (!selectedItems.contains(item)) {
//add it to your iist
}
Hope that works for you.
Ram
All Answers
Zoom,
You may want to post examples of selectedItems and selectedMulPickKeyTech. I'd suggest putting a debug to print each one out. "contains" should work on a String but perhaps there is something going on with the two variables that I don't understand without seeing the debug output.
Ram
Ram - thanks for your response.
Actually, right now I'm not even able to get a return on selectedItems for some reason. I'm trying to use a copy of the class that I used for New records of this type for an Edit and for some reason I'm now getting an error when trying to refer to a field on the record :
I know that is supposed to come from trying to retrieve a field but not referencing properly, but I thought I was referencing it properly in this code :
I thought I had referenced it properly in the lines which I underlined. Do you know what I'm doing wrong with it ?
Also, can you tell me a quick way to pull a field value from a parent record ? I've been looking all over and can't find one. I would think it would be very simple.
Thanks very much Ram. You've always been a great help !
Zoom,
No problem. Try putting the following on your VF page. It should force your standard controller to fetch the field
Thanks Ram !
I have one more question if you feel like helping : This was all the code that went into a New record for this object. I'm now trying to use this in the Edit mode of the same object. But apparently, that portfoliofunction query method will not work properly in the Edit in order to retrieve the Subsidiaries_On_Contract__c field value from the parent, so I can't build the list which I would like to compare to selectedItems. I didn't realize that was happening when I first posted this question. How could I accomplish this w/out using that whole portfoliofunction method ? I do not want to use that page parameter method which grabs that id.
Contract_Title__c is the field in this child which is a lookup to the parent (Contract_Overview__c). Could I do something like this to get my selectedMulPickKeyTech string ? :
I don't know if this is proper syntax or the method. Please let me know if it isn't.
and somehow I would have to turn selectedMulPickKeyTech into a list and then use my Contain method I showed originally to only pull the values which have not yet been put into the Subsidiaries_Included_On_Terms__c field in the current record.
Again, I don't know if this is correct. I'm sure there are errors. If you could help me I would be extremely grateful.
Thank you so much for everything you've done.
Take care.
Zoom,
No worries. Just switch the x to f since that's what you're using within your loop. Hopefully that gets you further and we can go from there.
That worked ! So I'm finally where I wanted to be at the beginning of all this and that is to compare the 2 strings :
selectedItems
selectedMulPickKeyTech
In my code above you can see that I'm splitting up selectedMulPickKeyTech after creating it from the query. But I don't know how to take out values in selectedItems.
I have done this successfully in the past by doing this :
But I don't know how to apply that type of method here since I'm pulling in one single string.
Got any suggestions ?
Thanks again Ram.
Zoom,
You can split selectedMulPickKeyTech using the delimiter that separates items in a multi pick list (I believe it's a semicolon). If you print it using debug you'll know
Try
for (String item : selectedMulPickKeyTech.split(';') ) {
if (!selectedItems.contains(item)) {
//add it to your iist
}
Hope that works for you.
Ram
That worked too ! I knew I couldn't be too far away.
Thanks so much Ram ! You've always been really great !
Best,
Ram