I have a method in my Apex controller that takes two parameters, a String and a Map< String, String>. For the plain String it evaluates correctly for me to pass a variable ‘$myVariable’ and the Apex controller receives the value, but when it is embedded as a value in a map, it doesn’t evaluate it – it just passes the string ‘$myVariable’
LWC JS controller snippet:
// strToPass is received from the parent component, ex. "test value"
@api strToPass;
@wire(myApexMethod, {strParam: '$strToPass', mapStringParam:{‘key’: '$strToPass'}})
wiredInitData({ error, data }){
if(data){
this.data = data;
}
else if(error){
this.error = error;
}
}
Apex controller:
public static void myApexMethod(String strParam, Map<String, String> mapStringParam){
System.debug(‘strParam: ‘ + strParam);
System.debug(‘mapStringParam: ‘ + mapStringParam);
}
Results of debug statements:
strParam: test value
mapStringParam: '$strToPass'
Things I've tried that haven't worked:
// This produced the same results as above:
@wire(myApexMethod, {strParam: '$strToPass', mapStringParam:{key:'myKey', value:'$strToPass'}})
// The Apex method received null here for the map parameter:
mapValues = [{key: 'myKey', value: 'myValue' }];
@wire(myApexMethod, {strParam: '$strToPass', mapStringParam: '$mapValues'})
Seems like it's just got to be a syntax issue here, but I can't figure it out...
let obj = {'fruit': 'Apple'} let data = JSON.stringify(obj);
Change parameter type map to String in apex class, and parse the String in Apex method, could you try with that.