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
AtiqahAtiqah 

Pass JavaScript variable to Apex Controller

Hi, anyone can help me how to call ApprovarId variable from JavaScript to Apex controller to replace value = :ActorId in query?
export default class ToApproved extends LightningElement {
    ApprovarId= Id;
    availableItems;
    error;
    columns = columns;

    @wire (toApproved)
    wiredAccount({error,data}){
        if(data){
            let tempRecs = [];
            data.forEach((record)=>{
                let tempRec = Object.assign({},record);

                //Name & target object id
                var objDes = JSON.stringify(tempRec.ProcessInstance.TargetObject);
                var finalObject = JSON.parse(objDes);
                tempRec.Object = '/'+tempRec.ProcessInstance.TargetObjectId;
                tempRec.recordName = finalObject.Name;

                //Name & created/submitted user id
                var createduser = JSON.stringify(tempRec.CreatedBy);
                var finalData = JSON.parse(createduser);
                tempRec.CreatedBy = '/'+tempRec.CreatedById;
                tempRec.createdByName = finalData.Name;

                //Name and approverid
                var approvar = JSON.stringify(tempRec.Actor);
                var finalApprovar = JSON.parse(approvar);
                tempRec.ApprovedBy = '/'+finalApprovar.name;
                tempRec.ApprovarName = tempRec.Actor.Name;

                tempRecs.push(tempRec);
            });
            this.availableItems = tempRecs;
            this.error = undefined;

        }else if(error){
            this.error = error;
            this.availableItems = undefined;
        }

    }

}
 
public with sharing class toApproved {
    @AuraEnabled(cacheable=true)
    public static list<ProcessInstanceWorkitem> getItemApprove(id ActorId){
        return[select ProcessInstance.TargetObject.name, CreatedDate, CreatedBy.name, ActorId, Actor.name from ProcessInstanceWorkitem
        where ActorId = :ActorId
        order by CreatedDate limit 100];
    }
}

Thank You
 
Shri RajShri Raj
To pass the JavaScript variable to the Apex controller, you can add a parameter to the @AuraEnabled method in the Apex controller and then pass the variable as an argument when calling the method from the JavaScript file.
In the Apex controller, add a parameter to the getItemApprove method like this:

@AuraEnabled(cacheable=true)
public static list<ProcessInstanceWorkitem> getItemApprove(id ActorId, Id ApprovarId){
    return[select ProcessInstance.TargetObject.name, CreatedDate, CreatedBy.name, ActorId, Actor.name from ProcessInstanceWorkitem
    where ActorId = :ActorId
    order by CreatedDate limit 100];
}
In the JavaScript file, you need to import the Apex class and call the method, passing the variable as an argument like this:

import toApproved from '@salesforce/apex/toApproved.getItemApprove';

export default class ToApproved extends LightningElement {
    ApprovarId= Id;
    availableItems;
    error;
    columns = columns;

    @wire (toApproved, {ActorId: this.ApprovarId})
    wiredAccount({error,data}){
        if(data){
            let tempRecs = [];
            data.forEach((record)=>{
                let tempRec = Object.assign({},record);

                //Name & target object id
                var objDes = JSON.stringify(tempRec.ProcessInstance.TargetObject);
                var finalObject = JSON.parse(objDes);
                tempRec.Object = '/'+tempRec.ProcessInstance.TargetObjectId;
                tempRec.recordName = finalObject.Name;

                //Name & created/submitted user id
                var createduser = JSON.stringify(tempRec.CreatedBy);
                var finalData = JSON.parse(createduser);
                tempRec.CreatedBy = '/'+tempRec.CreatedById;
                tempRec.createdByName = finalData.Name;

                //Name and approverid
                var approvar = JSON.stringify(tempRec.Actor);
                var finalApprovar = JSON.parse(approvar);
                tempRec.ApprovedBy = '/'+finalApprovar.name;
                tempRec.ApprovarName = tempRec.Actor.Name;

                tempRecs.push(tempRec);
            });
            this.availableItems = tempRecs;
            this.error = undefined;

        }else if(error){
            this.error = error;
            this.availableItems = undefined;
        }

    }

}