1

I had a "Variable Does Not Exist" error for currentportfolioId so I thought I'd declare it with public string currentportfolioId {get;set;}

but now I'm getting an "Expression cannot be assigned" error

Here is the code :

public with sharing class Multi_Select_From_Field {
    String keyTechselection{get;set;} // keyTechselection refers to values selected on child field
    public string currentportfolioId {get;set;}
    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.
    List<Contract_Overview__c> portfoliofunction=[SELECT id,name,Subsidiaries_On_Contract__c FROM Contract_Overview__c where id=:currentportfolioId limit 1]; 

    public List<SelectOption> getkeyTechValues() { 
        List<SelectOption> options=new List<SelectOption>(); 
        options.add(new SelectOption('--None--','--None--')); 
        String[] picklistlines =new String[]{}; 

        //"portfoliofunction" is list of master records i.e. Contract_Overview__c
        for(Contract_Overview__c function : portfoliofunction) { 
            if(function.Subsidiaries_On_Contract__c=='[]') //Subsidiaries_On_Contract__c field is from master object Contract_Overview__c
                selectedMulPickKeyTech=null; 
            else { 
                selectedMulPickKeyTech=function.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(){
        Contract_Terms__c.Subsidiaries_Included_On_Terms__c=keyTechselection; //This line will push the value from the string variable to actual custom field of child object.
        // Key_Technology__c this field is from child object(don't confuse with the master Subsidiaries_On_Contract__c field)
        //Then insert the values in the database
    }
}

Thank you very much for any help.

  • 2
    Hi @user2428. I edited your code for you again. Please take some more time next time you ask a question with code examples actually formatting the code. It is very difficult to read code that isn't properly indented and it greatly reduces your chance of good answers if you don't take the time to make it easier for the community to read. Please keep that in mind for your next question. – Jesse Altman Mar 11 '13 at 19:07
  • 1
    I'm sorry Jesse. I initially submitted it and then went back. I thought that I had re-formatted it correctly with the {} and it looked ok when I submitted it. – user2428 Mar 11 '13 at 19:12
  • No worries. I just want you to be able to get the best answers possible and that requires the best question possible. Enjoy the community! – Jesse Altman Mar 11 '13 at 19:13
  • Jesse - I'm trying to put my code between <pre> and <code> brackets, but it's still coming up unformatted. Then I tried putting {} around it all and it did the same. What and I doing wrong here ? I don't want to be the jerk posting unformatted code. – user2428 Mar 12 '13 at 13:20
  • The formatting isn't automatic. Unfortunately, because this is a web form, tabs don't work. On StackExchange the practice is to use 4 spaces where a tab should be. If you can paste the code in the textarea, you should be able to highlight it all and then just click the {} button at the top of question/answer box. It will indent every line by 4 spaces and should maintain your formatting. – Jesse Altman Mar 12 '13 at 13:27
2

The position of the following line seems a bit odd:

List<Contract_Overview__c> portfoliofunction=[SELECT id,name,Subsidiaries_On_Contract__c FROM Contract_Overview__c where id=:currentportfolioId limit 1]; 

When the Multi_Select_From_Field class is initialised this line will run, but currentportfolioId wouldn't have been set yet. I'm guessing this is causing the "Expression cannot be assigned" error as the value will be null in the SOQL.

Below I've reworked the code to only populate the portfoliofunction list if currentportfolioId is not null or empty. I also check if any values are returned in the query.

You need to think about what sets currentportfolioId and when it occurs. If it hasn't been set yet and getkeyTechValues() is called then you won't get the additional SelectOptions.

Is this value set from a query string paramenter? It could be read and set in the constructor.

Is it set via an element on the Visualforce page?

public with sharing class Multi_Select_From_Field {

    // keyTechselection refers to values selected on child field
    String keyTechselection {get;set;} 

    public string currentportfolioId {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.
    String selectedMulPickKeyTech{get;set;}

    public List<SelectOption> getkeyTechValues() { 
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('--None--','--None--')); 

        if(currentportfolioId == null || currentportfolioId.isEmpty()) {
            return options;
        }

        String[] picklistlines = new String[]{}; 

        // "portfoliofunction" is list of master records i.e. Contract_Overview__c
        List<Contract_Overview__c> portfoliofunction = [SELECT id,name,Subsidiaries_On_Contract__c FROM Contract_Overview__c where id = :currentportfolioId limit 1]; 

        if(portfoliofunction == null || portfoliofunction.size() == 0) {
            return options;
        }

        for(Contract_Overview__c function : portfoliofunction) { 
            if(function.Subsidiaries_On_Contract__c=='[]') //Subsidiaries_On_Contract__c field is from master object Contract_Overview__c
                selectedMulPickKeyTech=null; 
            else { 
                selectedMulPickKeyTech=function.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(){
        Contract_Terms__c.Subsidiaries_Included_On_Terms__c=keyTechselection; //This line will push the value from the string variable to actual custom field of child object.
        // Key_Technology__c this field is from child object(don't confuse with the master Subsidiaries_On_Contract__c field)
        //Then insert the values in the database
    }
}
  • Now I'm getting an 'unexpected token : '=' error on this line : currentportfolioId = portfolioId; I tried putting a : in front of the portfolioId but that didn't work. Thanks for your help. – user2428 Mar 11 '13 at 19:57
  • @user2428 It might be that you can delay the population of portfoliofunction until you actually need it. This depends on when and how you populate the currentportfolioId and how often getkeyTechValues() is called. If it is only called once per Visualforce page then consider only running the SOQL to populate the list directly in the method. – Daniel Ballinger Mar 11 '13 at 20:21
  • Thank you Daniel. I don't think I'll be able to get it to work right. I'm just trying to hack it together at this point. I've seen it work at one time, I don't know what's wrong with it now. Thanks – user2428 Mar 11 '13 at 20:52

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.