+ Start a Discussion
Amol GujareAmol Gujare 

APEX Code is not working!!!!

Hi All:

 

I am trying to assign multiple default values in String[] but its not working. Could you please help me out.

 

NOT WORKING BELOW CODE

==========================================================================

if (leadDivision.size()!=0) {
             for(Integer k =0; k < leadDivision.size() ;k++)
             {            
                 userDivisionName=userDivisionName+'\''+leadDivision[k].Division__r.Name+'\'';
                 if(k<leadDivision.size()-1)
                 userDivisionName=userDivisionName+', ';
             }
             division=new String[]{userDivisionName};
  }

 

 WORKING BELOW CODE

==========================================================================

division=new String[] {'Corporate Office','Laboratory Equipment','Scientific Instruments'};

 

i tried a lot for all string manupulations but its not showing me as selected for not working code.

 

Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber

OK, I think I see the problem here.

 

This is not going to instantiate an array:

 

 

division=new String[]{userDivisionName};

 

 

Not sure why you are going to the trouble of crafting a comma-delimited string when you could presumably just be doing this:

 

 

String[] division = new String[]{};
for(User_Division__c ud:leadDivision) {
    if(ud.Division__r != null) {
        division.add(ud.division__r.name);
    }
}

 

Does that work?

 

All Answers

mtbclimbermtbclimber

gonna need more detail here.

 

Questions:

 

 

  1. What query are you using to populate your leadDivision collection?
  2. What do you mean by "not working"? Empty value? Errors? 

 

Amol GujareAmol Gujare

Hi Thanks for reply:

 

I am trying to build that string to show selected values multidropdown list. example: i have 10 divisions and i wanted to show 3 as PRE-selected in the dropdown.

 

If hardcode divison values then its working but when i build string with values then its not showing me as preselected in multilist

 

QUERY

 divQryUser='Select u.User__c,u.Id, u.Division__r.Name,u.Division__r.Id  From User_Division__c u where user__c= \''+UserInfo.getUserId()+'\'';
            if(isDivisionActive){
            divQryUser+= ' and u.Division__r.is_Live__c=true'; 
            }
            String divAdlUserQry=' order by u.Division__r.Name';
            String divFinalUserQry=divQryUser+divAdlUserQry;
            leadDivision =Database.query(divFinalUserQry);

 

Output for Divison String 

('Corporate Office', 'Laboratory Equipment', 'Scientific Instruments')

 

I removed/added/escaped quotes for above string but its not showing me selected in the multilist.

 

but if i hardcode those values as below command then its shoing me as preselected.

division=new String[] {'Corporate Office','Laboratory Equipment','Scientific Instruments'};

mtbclimbermtbclimber

OK, I think I see the problem here.

 

This is not going to instantiate an array:

 

 

division=new String[]{userDivisionName};

 

 

Not sure why you are going to the trouble of crafting a comma-delimited string when you could presumably just be doing this:

 

 

String[] division = new String[]{};
for(User_Division__c ud:leadDivision) {
    if(ud.Division__r != null) {
        division.add(ud.division__r.name);
    }
}

 

Does that work?

 

This was selected as the best answer
mtbclimbermtbclimber

OK, I think I see the problem here.

 

This is not going to instantiate an array:

 

 

division=new String[]{userDivisionName};

 

 

Not sure why you are going to the trouble of crafting a comma-delimited string when you could presumably just be doing this:

 

 

String[] division = new String[]{};
for(User_Division__c ud:leadDivision) {
    if(ud.Division__r != null) {
        division.add(ud.division__r.name);
    }
}

 

Does that work?

 

Amol GujareAmol Gujare

Thank you so much