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.

  • Please show the getData function. – Phil W Oct 16 '19 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 Oct 16 '19 at 11:40
  • @PhilW yep, but global is irrelevant in this issue. – vladykx Oct 16 '19 at 11:43
  • @PhilW sorry, added the correct method name – vladykx Oct 16 '19 at 11:43
  • Is there a reason you are actually using an import instead of simply passing the string "Account"? – Phil W Oct 16 '19 at 12:26
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.