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
Zoom_VZoom_V 

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.

 

Best Answer chosen by Admin (Salesforce Developers) 
ForcepowerForcepower

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

ForcepowerForcepower

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

Zoom_VZoom_V

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 : 

 

 

 

SObject row was retrieved via SOQL without querying the requested field: Contract_Terms__c.Subsidiaries_Included_On_Terms__c 

 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 : 


public class Contract_Terms_EDIT {
private final Contract_Terms__c abc;
public List<SelectOption> selectedSubs { get; set; }
public String message { get; set; }
public String selectedItems { get; set; }
    public Contract_Terms_EDIT(ApexPages.StandardController controller) {
      
      selectedSubs = new list<SelectOption>();
        this.abc = (Contract_Terms__c )controller.getRecord();
        selectedItems=abc.Subsidiaries_Included_On_Terms__c;
        portfoliofunction=[SELECT id,name,Subsidiaries_On_Contract__c FROM Contract_Overview__c where Contract_Overview__c.id=:ApexPages.currentPage().getParameters().get('CF00NW0000000diSH_lkid') limit 1]; 
    }

  public  String keyTechselection{get;set;} // keyTechselection refers to values selected on child field
public    String selectedMulPickKeyTech{get;set;} // this is a temporary string variable which is just used in the "getkeyTechValues()" method to separate and arrange the values and put in a drop down.

    public List<Contract_Overview__c> portfoliofunction = new List<Contract_Overview__c>();

    public String rightOptionsHidden { get; set; }
    public String leftOptionsHidden { get; set; }
        
    public List<SelectOption> getkeyTechValues()
    {
        List<SelectOption> options=new List<SelectOption>();
        options.add(new SelectOption('--None--','--None--'));
        String[] picklistlines =new String[]{};
        for(Contract_Overview__c f : portfoliofunction) //"portfoliofunction" is list of master records i.e. Contract_Overview__c
        {
            if(f.Subsidiaries_On_Contract__c=='[]') //Subsidiaries_On_Contract__c field is from master object Contract_Overview__c
                selectedMulPickKeyTech=null;
            else
            {
                selectedMulPickKeyTech=f.Subsidiaries_On_Contract__c; //Subsidiaries_On_Contract__c field is from master object Contract_Overview__c
                selectedMulPickKeyTech=selectedMulPickKeyTech.replace('[','');
                selectedMulPickKeyTech=selectedMulPickKeyTech.replace(']','');
            }
        }
        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++)
                {
                    options.add(new SelectOption(inputvalues[j],inputvalues[j] ));
                }
            }

        return options;
        }
    } 
    public PageReference save()

        {
        
        //System.debug('********************************' + names);
        
        message = null ;       
        Boolean first = true;
        for ( SelectOption so : selectedSubs ) {
            if (first) {
                message = message ;
            }
            
                
            message = message + ', ' + so.getValue() ;
            
            first = false;
            
        }
        
        message=message.removeStart('null, ');
        message='['+message+']';
        
        System.debug('********************************' + message);
        abc.Subsidiaries_Included_On_Terms__c=message;
        
        insert abc;
        //System.debug('********************************' + contract);
                  
        PageReference pr = new PageReference('/a0B/o');
      pr.setRedirect(true);
      return pr;
          
        }
}

 

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 !

 

 

ForcepowerForcepower

Zoom,

 

No problem. Try putting the following on your VF page. It should force your standard controller to fetch the field

 

 

<apex:outputText value="{!Contract_Terms__c.Subsidiaries_Included_On_Terms__c}" rendered="false" />
Zoom_VZoom_V

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.

 

contractname = abc.Contract_Title__c ; //it would be a page reference like the other field you helped with on the VF page

for(Contract_Overview__c x : [Select Id, Subsidiaries_On_Contract__c from Contract_Overview__c where id =:contractname)

 {
            if(f.Subsidiaries_On_Contract__c=='[]') //Subsidiaries_On_Contract__c field is from master object Contract_Overview__c
                selectedMulPickKeyTech=null;
             else
            {
                selectedMulPickKeyTech=f.Subsidiaries_On_Contract__c; //Subsidiaries_On_Contract__c field is from master object                                    Contract_Overview__c
                selectedMulPickKeyTech=selectedMulPickKeyTech.replace('[','');
                selectedMulPickKeyTech=selectedMulPickKeyTech.replace(']','');
            }

 

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.

ForcepowerForcepower

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.

 

for(Contract_Overview__c f : [Select Id, Subsidiaries_On_Contract__c from Contract_Overview__c where id =:contractname)

 {
            if(f.Subsidiaries_On_Contract__c=='[]') //Subsidiaries_On_Contract__c field is from master object Contract_Overview__c
                selectedMulPickKeyTech=null;
             else
            {
                selectedMulPickKeyTech=f.Subsidiaries_On_Contract__c; //Subsidiaries_On_Contract__c field is from master object                                    Contract_Overview__c
                selectedMulPickKeyTech=selectedMulPickKeyTech.replace('[','');
                selectedMulPickKeyTech=selectedMulPickKeyTech.replace(']','');
            }
Zoom_VZoom_V

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 : 

 

account a =[select name , (select name from Subsidiaries_and_Brands__r) from account where id =:accountid];
                for(SubsidiariesAndBrands__c s : a.Subsidiaries_and_Brands__r)
                    {
                        //check that selected items dont already contain the keys
                        if(!selectedItems.contains(s.name))
                    options.add(new SelectOption(s.name,s.name));
                    }

 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.

ForcepowerForcepower

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

This was selected as the best answer
Zoom_VZoom_V

That worked too ! I knew I couldn't be too far away.

 

Thanks so much Ram ! You've always been really great !

ForcepowerForcepower
Glad to hear it, Zoom!
Best,
Ram