1

I have an apex method which is returning map in the form of Map<string,Object> and map will contains only one record

Can anyone help me how can I set attribute on component from map value

On component, I have two string attributes string1 and string2

From Java script, I want to set

component.set('v.string1',map.field1)
component.set('v.string2',map.field2)

doInit : function(component, event, helper) {
    //sai start
    var action = component.get('c.showSearch');
    action.setCallback(this, function(response) {
    var state = response.getState();
    if(state === 'SUCCESS') {
        var result = response.getReturnValue();
            component.set('v.searchHub', response.getReturnValue().Search_Hub_Name__c);
            component.set('v.name2',response.getReturnValue().Visualforce_Component_Name__c);
    } else {
        console.log('Error: ' + response.getError());
    }
    });

Can anyone help on this please

5
  • Harish, can you share the map and it's values Commented Mar 28, 2023 at 6:02
  • Mukul, map values in debug is {Access_Permission=Search__mdt:{Permission_Set_Name__c=Access_Permission, Hub_Name__c=ArticlesPanel, Visualforce_Name__c=article-hub}}
    – user130647
    Commented Mar 28, 2023 at 6:05
  • I simply want to put map.field1 to attribute1 and map.field2 to attribute2
    – user130647
    Commented Mar 28, 2023 at 6:07
  • Log the map in the browser's console so I can understand how the map is formed. Commented Mar 28, 2023 at 6:13
  • I have added apex class code in question which is creating map
    – user130647
    Commented Mar 28, 2023 at 6:19

1 Answer 1

0

Why return a map if you only need to return one object?

Assuming there's a good reason for returning a map:

An Apex Map should become a standard Javascript object when it arrives at the front end.

To access it, if you know the name, you can get the object out of the map like this:

let accessPermissionObj = yourResultObject['Access_Permission'];

Now you have the object, you can access the fields as you were before:

component.set('v.string1',accessPermissionObj.Hub_Name__c)
component.set('v.string2',accessPermissionObj.Visualforce_Name__c)

If you don't know the key of the map, you can iterate like so:

Object.keys(yourResultObject).forEach(obj => {
  //do something with your object
  component.set('v.string1',obj.Hub_Name__c);
  //you'd normally put this object in an array or apply a map function
})

If you wanted to return just one object, your Apex method would look like:

public static Coveo_Similar_Search__mdt showSearch(){}

this would be the result of your Aura server call. And you'd just use the result directly:

var result = response.getReturnValue();

component.set('v.string1', result.Hub_Name__c)
component.set('v.string2', result.Visualforce_Name__c)
6
  • thanks for helping out here, if I return object instead of map then how can I assign object field to attributes
    – user130647
    Commented Mar 28, 2023 at 6:40
  • updated with an example of this. Commented Mar 28, 2023 at 6:56
  • Thanks a ton Caspar Harmer for helping here
    – user130647
    Commented Mar 28, 2023 at 6:59
  • how can I define result object here, I added aura server call code in question
    – user130647
    Commented Mar 28, 2023 at 7:44
  • updated question Commented Mar 28, 2023 at 9:07

You must log in to answer this question.