• SushilBolwar
  • NEWBIE
  • 30 Points
  • Member since 2022

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies

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>

Hi!
I have such a problem. After the filter is triggered, the information from the account column disappears. Please tell me, how can this be fixed? Here ai my code:

Apex

public with sharing class ContactTable {
    @AuraEnabled(cacheable=true)
    public static List<Contact> getContacts() {
        return [
            SELECT Id, FirstName, LastName, Phone, Email, Account.Name, AccountId, CreatedDate
            FROM Contact
        ];
    }
    @AuraEnabled(cacheable=true)
    public static List<Contact> searchContacts(String searchKey) {
        String searchKeyword = '%' + searchKey + '%';
        List<Contact> contacts = [
            SELECT Id, FirstName, LastName, Phone, Email, Account.Name, AccountId, CreatedDate
            FROM Contact
            WHERE Name LIKE :searchKeyword
        ];
        return contacts;
    }
}

HTML
<template>
    <lightning-card title="Contact Table">
        <lightning-layout multiple-rows="true" vertical-align="end">
            <lightning-layout-item size="4" padding="around-small">
                                <!-- lightning input -->
                <lightning-input type="text" label="SEARCH BY NAME" value={key}
                onchange={updateKey}></lightning-input>
            </lightning-layout-item>
            <lightning-layout-item size="2" padding="around-small">
                                <!-- lightning button -->
                <lightning-button label="Filter" onclick={handleSearch} variant="brand"></lightning-button>  
            </lightning-layout-item>
            <lightning-layout-item size="12" padding="around-small">
                                 <!--lightning table -->
                <lightning-datatable key-field="id" data={contacts} columns={columns}
                    onrowaction={handleRowAction}>    
                </lightning-datatable>
            </lightning-layout-item>
        </lightning-layout>
    </lightning-card>
</template>

JS
import { LightningElement , wire} from 'lwc';
import getContacts from '@salesforce/apex/ProjectFirst.getContacts';
import searchContacts from '@salesforce/apex/ProjectFirst.searchContacts';
const columns = [
    {label: 'Contact FirstName',
    fieldName: 'FirstName',
    type: 'text'},
{
    label: 'Contact LastName',
    fieldName: 'LastName',
    type: 'text',
},
{
    label: 'Phone',
    fieldName: 'Phone',
    type: 'text',
},
{
    label: 'Email',
    fieldName: 'Email',
    type: 'Email',
},
{  
    label: "Account Name",  
    fieldName: "recordLink",  
    type: "url",
    typeAttributes: {
        label: { fieldName: "AccountName" },
        tooltip:"Name",
        target: "_blank",
        linkify: true},
},
{
    label: 'Created Date',
    fieldName: 'CreatedDate',
    type: 'date',
    typeAttributes: {
        day: 'numeric',
        month: 'numeric',
        year: 'numeric',
        hour: '2-digit',
        minute: '2-digit',
        hour12: true,
      },
},
];
export default class LightningDatatableLWCExample extends LightningElement {
    data = [];
    columns = columns;
    contacts;
    searchValue = '';
    @wire(getContacts)
    wiredContacts(value) {
        const {error, data} = value;
        if (data) {
            let contactData = JSON.parse(JSON.stringify(data));        
            contactData.forEach(Record => {
                console.log('Record= ', Record);
                if (Record.AccountId) {
                    Record.recordLink = "/" + Record.AccountId;  
                    Record.AccountName = Record.Account.Name;
                }
            });
            this.contacts = contactData;
        } else if (error) {
            this.error = error;
        }
    }
    updateKey(event){
        this.searchValue = event.target.value;
    }
    handleSearch(){
        searchContacts({searchKey: this.searchValue })
        .then(result => {
            this.contacts = result;
        })
        .catch(error => {
            this.contacts = null;                
        });
    }
}


 
I have a scenario where i need a button on task record which should directly create a opportunity based on whoid of a task where whoid is Contact .
How could we achieve this, basically  I tried this using flows but i couldn't able to achieve at the moment if any help would be apprechiable.