You need to sign in to do that
Don't have an account?
Atiqah
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?
Thank You
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
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;
}
}
}