2

I'm trying to implement a wire service to get data with passing schema items, but I'm getting an error that objectName can't be mapped to a String

I'm including the relevant parts below

LWC part

import getData from '@salesforce/apex/ApexTest.getData';
import OBJECT_NAME from '@salesforce/schema/Account';

...


@wire(getData, { objectName: OBJECT_NAME })
wiredPicklistValues(result) {
  ...
}

Apex class

global class ApexTest {

  @AuraEnabled(cacheable=true)
  global static String getData(String objectName) {
     return 'test';
  }

}

When looking on LWC side, it seems to be mapping Object and Field schema to an interface called fieldId, but what is the appropriate object type on the Apex side?

PS: I can, of course, use JSON.stringify(...) and deserialize on Apex side, but would like to know if there is a more elegant solution to this.

5
  • Please show the getData function.
    – Phil W
    Commented Oct 16, 2019 at 11:40
  • (Why are you making the Apex global? Most of the time this is unnecessary and you can use public instead)
    – Phil W
    Commented Oct 16, 2019 at 11:40
  • @PhilW yep, but global is irrelevant in this issue.
    – vladykx
    Commented Oct 16, 2019 at 11:43
  • @PhilW sorry, added the correct method name
    – vladykx
    Commented Oct 16, 2019 at 11:43
  • Is there a reason you are actually using an import instead of simply passing the string "Account"?
    – Phil W
    Commented Oct 16, 2019 at 12:26

1 Answer 1

3

The problem is, you are passing the import directly which is an object but the apex method is expecting a string. If you log the imported object on connectedCallback, you will get the object.

You should use objectApiName from the imported object which will be string.

@wire(getData, { objectName: OBJECT_NAME.objectApiName })
wiredPicklistValues(result) {
    console.log(result);
}
connectedCallback() {
    console.log('OBJECT_NAME => ', OBJECT_NAME);
}

LOG:

enter image description here

Here OBJECT_NAME.objectApiName will pass the object name correctly to apex and you will get return.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .