function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RainerRichterRainerRichter 

LWC Imperative APEX with Parameters not taking Ids

I'm creating a LWC QuickAction. From a lookup field for accounts I need to fetch the external Id of the Account. I get the account ID from the lookup, but when I pass them to APEX, I get Error 500, Server Error. If I test the same function with the @api recordId it works fine. Here is what I have (relevant parts):
handleCustomerValueSelected(event) is the function in trouble!
 
import { LightningElement, api, wire, track } from 'lwc';
import getAccExternalId from '@salesforce/apex/AI_LessonCreateLwcController.getAccountExternalId';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';


export default class LessonCreate extends LightningElement {
    @api recordId;
    @api invoke(){};

    @track error;
    @track selectedCustomerRecordId = '';
    @track selectedTeacherRecordId = '';
    selectedCustomerExternalRecordId = '';
    selectedTeacherExternalRecordId = '';
    selectedSubjectRecordId = '';
    accountName;

    handleCustomerValueSelected(event) {
        this.selectedCustomerRecordId = event.detail;
        var custId = event.detail;
        console.log('***CustId: ' + this.selectedCustomerRecordId);
        getAccExternalId({ accId : this.selectedCustomerRecordId })
            .then(response => {
                this.selectedCustomerExternalRecordId = response;
                console.log('***ExId: ' + this.selectedCustomerExternalRecordId);
            })
            .catch(error => {
                this.error = error;
                console.log('***ExId Error: ' + this.error.status);
            });
        
    }


}


And the Apex:

@AuraEnabled
    public static String getAccountExternalId(String accId){
        system.debug('***Apex: ' + accId);
        Account acc = [SELECT Id, ET_UserExternalId__c FROM Account WHERE Id = : accId];
        
        return acc.ET_UserExternalId__c; 
    }


And this is the result from console log:

***CustId: 0011j000018avy1AAA

***ExId Error: 500

As stated before, when I for testing purpos use the "this.recordId" instead "this.this.selectedCustomerRecordId" I get the result. Also if i use a string ID like '0011j000018avy1AAA' works fine.

If somebody has an Idea ...

Best Answer chosen by RainerRichter
SwethaSwetha (Salesforce Developers) 
HI RainerRitcher,
I see you also reached out on  https://salesforce.stackexchange.com/questions/352949/lwc-imperative-apex-with-parameters-not-taking-ids

According to this, The event.detail was an array with one Id. so with "event.detail[0]" you were able to solve the problem

Copying answer posted:
"This error is most likely due to a mismatch in the type of parameter your Apex method expects, and the type of the value you are sending to it.
By looking at your code, it looks like you are directly using event.detail. Typically event.detail is an object which further holds properties like id etc, which in turn depends on the component that is firing the event. So passing an object to a String (Apex param) results in this error.
Thats why you arent getting any logs. The method itself isn't invoked due to param type mismatch.
Again, this is the most likely cause. As others have requested in the comments, if you can console.log this.error instead of this.error.status, you will get to know the actual cause, and we can help you."

Please mark this answer as best so that others facing the same issue will have better visibility and find this information useful. Thank you