1

Is it possible to pass Apex action as a parameter to other LWC component? This code gives me error

[LWC component's @wire target property or method threw an error during value provisioning. Original error: [g$LWS[7] is not a function]]

Is there any way to achieve this?

mainComponent.js

import { LightningElement, wire, api } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import apexMethod from '@salesforce/apex/ApexClass.apexMethod';
import { doSomeLogic } from 'c/commonFeatures';

export default class DisplayMatchingBCRs extends LightningElement {

   @api recordId;

   @wire(getRecord, { recordId: '$recordId', fields: [FIELDS] })
       wiredRecord({ error, data }) {
           if (data) {
               doSomeLogic({callback: apexMethod})
           }
    }
}

commonFeatures.js

const doSomeLogic = ({callback}) => {
    callback()
        .then(result => {
            console.log(result)
        });
}

export { doSomeLogic };

1 Answer 1

2

Not in the manner you're trying to do. What ends up happening is that the runtime complains about not having a VM (note: partly because this is set to the wrong object). The best you can do is handle the result, something like:

export const doSomeLogic = (...result) => {
  console.log(result);
}

Which you call as:

apexMethod().then(doSomeLogic);

However, you could also use the mixin design:

const Utils = (superclass) => class extends superclass {
  doSomeLogic({callback}) {
    callback().then(result=>{console.log(result);});
  }
};
export default Utils;

Which you can then use in any other class:

import Utils from 'c/utils';
import apexMethod from '@salesforce/apex/MyClass.MyMethod';

export default class MyComponent extends Utils(LightningElement) {
  someMethod() {
    this.doSomeLogic({callback:apexMethod});
  }
}
3
  • sfdcfox, this is completely new to me. I am not able to find Mixin design implementation anywhere on salesforce document. Can you help me with that ? Commented Nov 16, 2022 at 14:39
  • 1
    @AnshulAgrawal You won't find mixins directly in the documentation (but note that lightning-navigation includes the NavigationMixin). You'll want to read a basic tutorial to learn more, such as this one.
    – sfdcfox
    Commented Nov 16, 2022 at 15:15
  • The mixin approach is what I needed, thanks! Commented Nov 17, 2022 at 7:52

You must log in to answer this question.

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