I am inserting data to a custom object through LWC. I want to call an apex method from the JS with the Id of the newly created record and current user's email address.
Apex Method:
@AuraEnabled(cacheable=true)
public static void checkRequestor(String requestorEmail, Id recordId) {
if(requestorEmail.contains('@pwc.com')) {
List<User> getUserId = [SELECT Id, Email, IsActive FROM User WHERE Email =: requestorEmail AND IsActive = true LIMIT 1];
If(getUserId.size() > 0)
{
try
{
List<Tender_Tracker_Case__Share> listCaseToShare = new List<Tender_Tracker_Case__Share>();
for(User userId : getUserId)
{
Tender_Tracker_Case__Share caseToShare = new Tender_Tracker_Case__Share();
caseToShare.AccessLevel = 'Read';
caseToShare.UserOrGroupId = userId.Id;
caseToShare.ParentID = recordId;
caseToShare.RowCause = 'Manual';
listCaseToShare.add(caseToShare);
}
if(listCaseToShare.size() > 0)
{
Insert listCaseToShare;
}
} catch(Exception e) {
System.debug(e.getMessage());
}
}
}
}
LWC JS file:
import { LightningElement, track, api, wire } from 'lwc';
import USER_EMAIL from '@salesforce/schema/User.Email';
import checkRequestor from '@salesforce/apex/TT_CaseEmailService.checkRequestor';
export default class CreateRecordLWC extends NavigationMixin(LightningElement) {
@track currentUserEmail;
@wire(getRecord, { recordId: Id, fields: [USER_EMAIL]})
currentUserInfo({error, data}) {
if (data) {
this.currentUserEmail = data.fields.Email.value;
} else if (error) {
this.error = error ;
}
}
handleCreate() {
var fields = { 'Subject__c': this.template.querySelector("[data-field='Subject']").value,
'TT_Case_Summary__c': this.template.querySelector("[data-field='CaseSummary']").value,
};
const recordInput = { 'apiName': 'Tender_Tracker_Case__c', fields };
console.log(recordInput);
createRecord(recordInput)
.then((record) => {
this.showToast('Success!!', 'Case created successfully!! ' + record.id, 'success', 'dismissable');
this.showLoading = false;
this[NavigationMixin.Navigate]({
type: "standard__recordPage",
attributes: {
actionName: 'view',
objectApiName: 'Tender_Tracker_Case__c',
recordId: record.id
}
});
// call the apex method here and pass the parameters (email address and record id)
@wire(checkRequestor, {requestorEmail : '$currentUserEmail', recordId : record.id});
})
.catch(error => {
this.showLoading = false;
console.log(error);
this.showToast('Error!!', error.body.message, 'error', 'dismissable');
});
this.handleCancel();
}
}
In the JS code above, I am calling the apex method with the following line:
@wire(checkRequestor, {requestorEmail : '$currentUserEmail', recordId : record.id});
I am new to LWC and don't know if this is the right way to call an apex method and pass the parameters.