You need to sign in to do that
Don't have an account?
calling Apex class in LWC Button
Hi All - I just want to call the apex method which has two paramaeters in when on click button. I have created the Button using LWC and trying to call the apex method , below is my code. can someone correct me if my approach is correct. New to LWC trying to implement this want best way and suggestions.
Apex Method :
@AuraEnabled(cacheable=true)
public static void recountAssets(Id b2bAccountId, Id b2bOrderId){
String sDISCOCouponCode;
List<Id> lstB2BOrderIds = new List<Id>();
List<String> lstPkgDefCodes = new List<String>();
List<OrderItem> lstB2BOrderItems = new List<OrderItem>(); //List of all B2B Order Items that requires reconciliation
//List<Order> lstB2BOrders = new List<Order>();//List of all B2B Orders that requires reconciliation
if(b2bAccountId != null){
Account objB2BAccount = [SELECT ID, DISCOCouponCode__c FROM Account WHERE ID =: b2bAccountId AND RecordType.DeveloperName = 'ACC_B2BCustomer' LIMIT 1];
if(objB2BAccount != null){
sDISCOCouponCode = objB2BAccount.DISCOCouponCode__c;
if(!String.isEmpty(sDISCOCouponCode)){
if(b2bOrderId != null){
// for(Order o:b2bOrders){
// lstB2BOrderIds.add(o.Id);
// system.debug('lstB2BOrderIds' +lstB2BOrderIds);
// }
lstB2BOrderItems = [SELECT Id, OrderId, Package_Definition_Code__c, DISCOCouponCode__c, NoOfActiveSubscriptionsExclBuffer__c, Quantity, Order.NumberOfDigitalSubscriptionSold__c
FROM OrderItem WHERE TECH_OrderCategory__c ='Corporate' AND (OrderId =: b2bOrderId OR Order.Original_Subscription__r.Id =: b2bOrderId) AND DISCOCouponCode__c =: sDISCOCouponCode AND Order.Status = 'ACTIVE' AND Order.StartDate__c <= TODAY AND Order.End_Date__c >= TODAY AND Order.B2BProduct__c != 'Anonymous' ORDER BY Order.StartDate__c ASC];
system.debug('lstB2BOrderItems' +lstB2BOrderItems);
}
else{
lstB2BOrderItems = [SELECT Id, OrderId, Package_Definition_Code__c, DISCOCouponCode__c, NoOfActiveSubscriptionsExclBuffer__c, Quantity, Order.NumberOfDigitalSubscriptionSold__c
FROM OrderItem WHERE TECH_OrderCategory__c ='Corporate' AND DISCOCouponCode__c =: sDISCOCouponCode AND Order.Status = 'ACTIVE' AND Order.StartDate__c <= TODAY AND Order.End_Date__c >= TODAY AND Order.B2BProduct__c != 'Anonymous' ORDER BY Order.StartDate__c ASC];
}
// lstB2BOrderIds.clear(); //Repurposing the variable for further use
// system.debug('lstB2BOrderIds' +lstB2BOrderIds);
if(lstB2BOrderItems.size()>0){
for(OrderItem oi:lstB2BOrderItems){
lstPkgDefCodes.add(oi.Package_Definition_Code__c); //Prepare the list of package definition codes
lstB2BOrderIds.add(oi.OrderId); //Prepare the list of ids of all the B2B orders that will go through active sub count reconciliation
oi.NoOfActiveSubscriptionsExclBuffer__c = 0; //Reset the current asset count
system.debug(' oi.NoOfActiveSubscriptionsExclBuffer__c ' + oi.NoOfActiveSubscriptionsExclBuffer__c);
}
}
if(lstPkgDefCodes.size()>0){
Map<String, Decimal> mapActiveAssetCount = new Map<String, Decimal>(); //Count of all the active assets that requires recounting, grouped by Package Definition Code
List<Asset> lstActiveAssets = [SELECT Package_Definition_Code__c, Id FROM Asset
WHERE Package_Definition_Code__c IN: lstPkgDefCodes AND Order_Product__r.DISCOCouponCode__c =: sDISCOCouponCode AND Status = 'ACTIVE'];
for (Asset a : lstActiveAssets) {
if(mapActiveAssetCount.containsKey(a.Package_Definition_Code__c)){
//System.debug('a.Package_Definition_Code__c : ' + a.Package_Definition_Code__c );
Decimal nAssetCount = (Decimal) mapActiveAssetCount.get(a.Package_Definition_Code__c);
nAssetCount++;
system.debug('nAssetCount' +nAssetCount);
mapActiveAssetCount.put(a.Package_Definition_Code__c, nAssetCount);
}
else{
mapActiveAssetCount.put(a.Package_Definition_Code__c, 1);
}
}
Map<Id,Decimal> mapTotalActiveSubsInB2BOrder = new map<Id,Decimal>();
for(OrderItem oi:lstB2BOrderItems){
Decimal nB2BOrderTotalActiveSubs;
if(mapTotalActiveSubsInB2BOrder.containsKey(oi.OrderId)){
nB2BOrderTotalActiveSubs = mapTotalActiveSubsInB2BOrder.get(oi.OrderId);
system.debug('nB2BOrderTotalActiveSubs' +nB2BOrderTotalActiveSubs);
}
else{
nB2BOrderTotalActiveSubs = 0;
system.debug('nB2BOrderTotalActiveSubs' +nB2BOrderTotalActiveSubs);
}
if(mapActiveAssetCount.containsKey(oi.Package_Definition_Code__c)){
Decimal nCurrentAssetCountByPkgDefCode = (Decimal) mapActiveAssetCount.get(oi.Package_Definition_Code__c);
system.debug('nCurrentAssetCountByPkgDefCode' +nCurrentAssetCountByPkgDefCode);
if(nCurrentAssetCountByPkgDefCode > 0){
// System.debug('Package_Definition_Code__c : ' + oi.Package_Definition_Code__c );
// System.debug('nCurrentAssetCountByPkgDefCode : ' + nCurrentAssetCountByPkgDefCode );
// System.debug('Order.NumberOfDigitalSubscriptionSold__c : ' + oi.Order.NumberOfDigitalSubscriptionSold__c );
// System.debug('nB2BOrderTotalActiveSubs : ' + nB2BOrderTotalActiveSubs );
if((oi.Order.NumberOfDigitalSubscriptionSold__c >= nCurrentAssetCountByPkgDefCode) && (nB2BOrderTotalActiveSubs < nCurrentAssetCountByPkgDefCode)){
Decimal nCountUp = nCurrentAssetCountByPkgDefCode - nB2BOrderTotalActiveSubs;
oi.NoOfActiveSubscriptionsExclBuffer__c += nCountUp;
nB2BOrderTotalActiveSubs += nCountUp;
nCurrentAssetCountByPkgDefCode -= nCountUp;
system.debug('nCountUp' +nCountUp);
}
else if((oi.Order.NumberOfDigitalSubscriptionSold__c < nCurrentAssetCountByPkgDefCode) && (nB2BOrderTotalActiveSubs < oi.Order.NumberOfDigitalSubscriptionSold__c)){
Decimal nCountUp = oi.Order.NumberOfDigitalSubscriptionSold__c - nB2BOrderTotalActiveSubs;
oi.NoOfActiveSubscriptionsExclBuffer__c += nCountUp;
nB2BOrderTotalActiveSubs += nCountUp;
nCurrentAssetCountByPkgDefCode -= nCountUp;
system.debug('nCountUp' +nCountUp);
}
mapTotalActiveSubsInB2BOrder.put(oi.OrderId, nB2BOrderTotalActiveSubs);
mapActiveAssetCount.put(oi.Package_Definition_Code__c, nCurrentAssetCountByPkgDefCode);
}
}
}
update lstB2BOrderItems;
system.debug('lstB2BOrderItems' +lstB2BOrderItems);
//Reconciling NumberofDigitalSubscriptionsActive__c in B2B Orders
if(lstB2BOrderIds.size()>0){
List<Order> lstB2BOrders = [SELECT Id, NumberofDigitalSubscriptionsActive__c, TECH_TotalNumberofActiveSubscription__c FROM Order WHERE Id IN: lstB2BOrderIds];
system.debug('lstB2BOrders' +lstB2BOrders);
if(lstB2BOrders.size()>0){
for(Order o : lstB2BOrders){
o.NumberofDigitalSubscriptionsActive__c = o.TECH_TotalNumberofActiveSubscription__c;
}
update lstB2BOrders;
}
}
}
}
}
}
LWC JS:
import { LightningElement,track } from 'lwc';
import recountAssets from '@salesforce/apex/B2BActiveAssetsRecountHandler.recountAssets';
export default class ReCountMechforAssetManagement extends LightningElement {
clickedButtonLabel;
@track b2bAccountId;
@track b2bOrderId;
@track error;
handleClick(event) {
this.clickedButtonLabel = event.target.value;
recountAssets()
.then(result =>{
this.b2bAccountId = result;
this.b2bOrderId = result;
})
.catch(error => {
this.error = error;
});
//this.clickedButtonLabel = event.target.label;
}
}
HTML:
<template>
<div class="slds-m-top_small slds-m-bottom_medium">
<!-- Brand outline variant: Identifies the primary action in a group of buttons, but has a lighter look -->
<lightning-button variant="brand-outline" label="Recount" title="Primary action with lighter look" onclick={handleClick} class="slds-m-left_x-small"></lightning-button>
</div>
</template>
</template>
Hii John
Try Below Code I've Modified It
Please Mark It As Best Asnwer If It HelpsThank You!
All Answers
Try Below Code Please Mark It As Best Asnwer If It Helps
Thank You!
LWC1503: "getRecord" is not a known adapter. (13:12)
Hii John
Try Below Code I've Modified It
Please Mark It As Best Asnwer If It HelpsThank You!
Please Close your Query By Marking It As best Answer So it Also helps Others As Well I Future