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
srikanth darbha 11srikanth darbha 11 

LWC Apex Imperative type Callout

Hi Everyone,

I have function inside JS file, where i am trying to invoke a apex imperative call as below-
getdlcontactscount({dls:str3})
.then(res => {
if (res.data) {
for(var i=0; i<data.length; i++) {
console.log('id=------' + data[i].TempValue__c);
this.refreshTable = data[i].TempValue__c;
}
this.error = undefined;
}

The request is hitting the apex method as below-

List<String> st= dls.split(';');
List<Contact> countval= new List<Contact>();
System.debug(LoggingLevel.INFO,'st value-----------'+st);
List<Contact> contactfinalids=new List<Contact>();
List<contact> lstcon=[Select Id,Email,Distributions_List_Assigned_To__c FROM Contact where email!=null];
for(String s:st)
{
for(Contact ct:lstcon){
if(ct.Distributions_List_Assigned_To__c!=null && (ct.Distributions_List_Assigned_To__c).contains(s.trim()))
contactfinalids.add(ct);
}
}
System.debug('contactfinalids-----------'+contactfinalids);
//countval.add(contactfinalids.size());
Contact c = new Contact();
c.TempValue__c=contactfinalids.size();
countval.add(c);
System.debug('contactfinalids----countval-------'+countval);
return countval;

I am getting the output in apex class, but when i am checking at the JS function the console statement itself is not getting printed
Can someone please help me on how to return values from apex imeprative calls , i.e how can i send a Integer value or a string value? 
 
Raquib SFRaquib SF
Hello Srikanth,

you can try the below :

JS
// apexWireMethodWithParams.js
import { LightningElement, track, wire } from 'lwc';
import findContacts from '@salesforce/apex/ContactController.findContacts';

/** The delay used when debouncing event handlers before invoking Apex. */
const DELAY = 300;

export default class ApexWireMethodWithParams extends LightningElement {
    @track searchKey = '';

    @wire(findContacts, { searchKey: '$searchKey' })
    contacts;

    handleKeyChange(event) {
        // Debouncing this method: Do not update the reactive property as long as this function is
        // being called within a delay of DELAY. This is to avoid a very large number of Apex method calls.
        window.clearTimeout(this.delayTimeout);
        const searchKey = event.target.value;
        this.delayTimeout = setTimeout(() => {
            this.searchKey = searchKey;
        }, DELAY);
    }
}

you apex class :
// ContactController.cls
public with sharing class ContactController {
    @AuraEnabled(cacheable=true)
    public static List<Contact> findContacts(String searchKey) {
        String key = '%' + searchKey + '%';
        return [SELECT Id, Name, Title, Phone, Email, Picture__c FROM Contact WHERE Name LIKE :key AND Picture__c != null LIMIT 10];
    }
}

Referenced from : https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex

Please mark as best answer if it solves your issue.

Thanks!
srikanth darbha 11srikanth darbha 11
Hi Hashmi,

I dont want to use wiring as i dont want the results to be cacheable. I want to user apex imperative call, Can anyone  please help me how to pass a return value using apex imeprative call, not using wiring.