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
mariappangomathirajmariappangomathiraj 

Getting Map value in js function

I have 100 accounts.I am forming one map with following values.

 

Map<String,String> accounts = new Map<String,String>();

for(Account a : AccountList){

string accountIds = a.Id+'str';

accounts.put(accountIds,a.Name)

}

 

I am using this map in inside of JS function.

 

JS code:

 $(document).ready(function() {

<apex:repeat value="{!AccountList}" var="acct">

 

var CRE = accountId+'str';

var Mapvalue = '{!accounts['+CRE+']}';
console.log('::::::::CRE:::::::'+CRE);

<apex:repeat/>

});

 

I am getting the below error:

 

Map key +CRE+ not found in map

Error is in expression '{!Mapvalue['+CRE+']}' in component <apex:repeat> in page accountsearch
 
Is any one help to get this value of Key(AccountId + string value)?

 

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Sorry, typo

 

<apex:outputText value = "{!accouts[accRec.id + 'str']}"/>

All Answers

Puja_mfsiPuja_mfsi

Hi,

I iterate the map over apex:Repeat to fetch their value .

 

<apex:page controller="GetValueInJSController">
<apex:form>
        <apex:repeat value="{!accounts}" var="accRec"> 

              <script>
                        var accName = '{!accounts[accRec]}';
               </script>
</apex:repeat>

</apex:form>

</apex:page>

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

 

 

mariappangomathirajmariappangomathiraj

Thanks Puja..

 

In normally we are getting following manner:

Storing the key as - Account id and value as name(String).


Example output:
{001W000000CrLj0IAF=Mariappan Gs,001W000000CrMj0IAF = Raja}

 

As per your code

<apex:page controller="GetValueInJSController">
<apex:form>
<apex:repeat value="{!accounts}" var="accRec">
<apex:outputText value = "{!accouts[accRec]}"/>
</apex:repeat>
</apex:form>
</apex:page>

 

accouts is the Map of accounts Id with name.

 

For My required output is below:

{001W000000CrLj0IAF-CIF=Mariappan Gs,001W000000CrMj0IAF-Other = Raja}

 

I am adding string value('CIF' or 'other' )to key.Then how to get the above output?

 

my trying code as in ur code:

I am using the dynamic variable and it put into the map.I am getting the below error

Map key +accName+ not found in map
Error is in expression '{!accouts["+accName+"]}' in component <apex:repeat> in page

 

<apex:page controller="GetValueInJSController">
<apex:form>
<apex:repeat value="{!accounts}" var="accRec">
<script>
var accName = '{!accRec.Id}'+'CIF';
var output = '{!accouts['+accName+']}'
alert(output)
</script>
<apex:outputText value = "{!accouts[accRec]}"/>
</apex:repeat>
</apex:form>
</apex:page>

 

how to get the Map values from adding string value to Id?

Puja_mfsiPuja_mfsi

Hi,

You can add the string value in Map when you create a map in class .

 

public Class GetValueInJSController {
             public Map<String,String> accounts {get;set;}
             public List<Account> AccountList{get;set;}

             public GetValueInJSController() {
                         accounts = new Map<String,String>();
                         AccountList = new List<Account>();
                         AccountList = [select Id,Name from Account];
                         for(Account a : AccountList){
                                 string accountIds = a.Id+'str';
                                 accounts.put(accountIds,a.Name);
                          }
              }
}

 

And in Page

 

<apex:page controller="GetValueInJSController">
<apex:form>
<apex:repeat value="{!accounts}" var="accRec">
<apex:outputText value = "{!accouts[accRec]}"/>
</apex:repeat>
</apex:form>
</apex:page>

mariappangomathirajmariappangomathiraj

Puja,

         I have added the same manner in my Apex class..I have added Id with string and put into the map.Then I didn't get in the page due to the below error.

 

Map key +accName+ not found in map
Error is in expression '{!accouts["+accName+"]}' in component <apex:repeat> in page

 


When I accessing id directly its works.I added with this string it throws error

jungleeejungleee

Hi

 

You could do something like this:

 

Class:

public class myMapclass{
    public map<string, account> x{get;set;}
    public set<string> str {get;set;}
    public myMapClass(){
        x  = new map<string, account>();
        for(account acc: [select name, id from account limit 25]){
            x.put(acc.id+'str', acc);
        }
        str = x.keySet();
    }
}    

 Page:

<apex:page controller="myMapclass">
<!--iterate thr the set--> <apex:repeat value="{!str}" var="key"> <script> var accountname = '{!x[key].name}'; console.log('the name is ' + accountname); </script> </apex:repeat> </apex:page>

 -Sam

 

 

mariappangomathirajmariappangomathiraj

Thanks junglee.

 

For example I told Map value is Name..May be it comes more than one object values.

But I Need Id with string to put in the map and we get the exact output..Is it possible?

jungleeejungleee

I am not sure what you mean when you say "May be it comes more than one object values". But if you're wanting to get the id with string then all you have to do is this

 

<apex:page controller="myMapclass">
<!--iterate thr the set-->
    <apex:repeat value="{!str}" var="key">
        <script>
        var accountname = '{!x[key].name}';
        console.log('the map key is ' +'{!key}'+ ' the name is ' + accountname);
        console.log('the format in which you're expecting: '+ '{!key}' + '='+'{!x[key].name}');
        </script>
    </apex:repeat>
</apex:page>

 

 

mariappangomathirajmariappangomathiraj

Thanks junglee.

 

I am expecting the following output:

 

See the code:

 

public Class GetValueInJSController {
public Map<String,String> accounts {get;set;}
public List<Account> AccountList{get;set;}

public GetValueInJSController() {
accounts = new Map<String,String>();
AccountList = new List<Account>();
AccountList = [select Id,Name from Account];
for(Account a : AccountList){
string accountIds = a.Id+'str';
string accountname = a.Name +'Raj';
accounts.put(accountIds,accountname);
}
}
}

In that Map everyIds have different Values of Map.
Let I want to know,How to access the map with dynamic key value?

And in Page

<apex:page controller="GetValueInJSController">
<apex:form>
<apex:repeat value="{!accounts}" var="accRec">
var accountId = '{!accRec}'+'str';

In that place,how to give this accountId to map and get this value?In below accessing map method throws error.

<apex:outputText value = "{!accouts['+accountId+']}"/>
</apex:repeat>
</apex:form>
</apex:page>

 

Starz26Starz26

<apex:outputText value = "{!accouts[accRecID + 'str']}"/>

 

That works in my use cases....

mariappangomathirajmariappangomathiraj

Hai Starz26,

        Can you please share your code for my reference?Because I gave the input as your way,it throws following error?

 

I am getting this values in inside of Js function.
Save error: Incorrect parameter type for operator '+'. Expected Number, Date, DateTime, received Object 

Starz26Starz26

Sorry, typo

 

<apex:outputText value = "{!accouts[accRec.id + 'str']}"/>

This was selected as the best answer
mariappangomathirajmariappangomathiraj

Thanks StarZ26.