Skip to main content

 @AuraEnabled(cacheable=true)

    public static String  dispLayAmounts(String oppId){

 

        Map<meatdata_mdt> oppLabelAndAPINameMap=new Map<String,meatdata_mdt>();

        String queryString='';

        Map<String,oppAmountWarper> opportunityAmountMap = new Map<String,oppAmountWarper>();

 

        for(meatdata_mdt opprecordfield : meatdata_mdt.getAll().values()){

 

            queryString+=opprecordfield.Opportunity_field1__c+', ';

            queryString+=opprecordfield.Opportunity_field2__c+', ';

            queryString+=opprecordfield.Opportunity_field3__c+', ';

            queryString+=opprecordfield.Opportunity_field4+', ';

 

            opportunityAmountMap.put(bucketName.Label,bucketName);

 

        }

 

        System.debug('*****oppId='+oppId);

        String oppQuery='';

 

        oppQuery='SELECT id,Name,'+queryString+'Amount';

 

        oppQuery+=' FROM Opportunity where id=:oppId';

 

        for(Opportunity oppRecord : Database.query(oppQuery)){

            for(String familyName :  oppLabelAndAPINameMap.keySet()) {

 

                meatdata_mdt oppMdtRecord = oppLabelAndAPINameMap.get(familyName);

 

                System.debug('*****oppProductBucketMdtRecord=' + oppProductBucketMdtRecord);

                Integer oppAmount1=oppRecord.get(oppProductBucketMdtRecord.Opportunity_Bookings__c)==null?0 : Integer.valueOf(oppRecord.get(oppProductBucketMdtRecord.Opportunity_Bookings__c));

                Integer oppAmount2=oppRecord.get(oppProductBucketMdtRecord.Opportunity_PO_Total__c)==null?0 : Integer.valueOf(oppRecord.get(oppProductBucketMdtRecord.Opportunity_PO_Total__c));

                Integer oppAmount3=oppRecord.get(oppProductBucketMdtRecord.Opportunity_Annualized__c)==null ?0 : Integer.valueOf(oppRecord.get(oppProductBucketMdtRecord.Opportunity_Annualized__c));

                Integer oppAmount4=oppRecord.get(oppProductBucketMdtRecord.Opportunity_PO_Annualized__c)==null ?0 : Integer.valueOf(oppRecord.get(oppProductBucketMdtRecord.Opportunity_PO_Annualized__c));

 

                opportunityAmountMap.put(familyName,

                        (

                                new OpportunityRemainingAmounts (

                                        familyName,

                                        oppBookingAmountRemaining-oppPOTotalAmountRemaining,

                                        oppBookingAmountAnnlzdRemaining - oppPOTotalAmountAnnlzdRemaining

                                )

                        )

                );/

 

            }

        }

         

        return  JSON.serialize(opportunityAmountMap) ;

    }

//wrapper class to return the oppAmountWarper product lines

public class oppAmountWarper{

    @AuraEnabled public String name;

 

    @AuraEnabled public Double oppAmount1Total;

    @AuraEnabled public Double oppAmount2Total;

 

    public oppAmountWarper(String nam,Double oppAmountTotal1,Double oppAmountTotal2){

     this.productName = nam;

 

        this.oppAmount1Total = oppAmountTotal1;

        this.oppAmount2Total = oppAmountTotal2;

 

    }

}

LWC code

export default class oppAmount extends LightingElement{

@track oppamountMap=new map();

getdispLayAmounts(oppId) {

    console.error('*********opp recordId22222=' + oppId);

    dispLayAmounts({ oppId: oppId })

        .then((data) => {

            var result= JSON.parse(data);

            for(var key in result) {

             //   console.log('*********Key00=' + key + '*********value00=' + result[key]);

                console.log('*********result.hasOwnProperty(key)=' + result.hasOwnProperty(key));

                if (result.hasOwnProperty(key)) {

                  //  console.log('*********result[key].oppReamingAmount00=' + JSON.parse(result[key]));

                    console.log('*********key=' + key); // printing key

                    console.log('*********value=' + JSON.stringify(result[key]));// printing value

 

                    this.oppamountMap.set(key, JSON.stringify(result[key]));

 

                    console.log('*********inside this.oppamountMap0000=' + JSON.stringify(this.oppamountMap)); // printing empty map

 

                }

            }

            console.log('*********his.oppamountMap3333=' + JSON.stringify(this.oppamountMap));/ printing empty map

 

        })

        .catch((error) => console.error(error));

}

oppamountMap map is not setting key values.  can anyone please suggest me what is wrong with the above code why oppamountMap is empty , I can see Key and values in console , but not in map.

1 answer
  1. Feb 13, 2024, 6:47 AM

    @Aruna Rapolu

    Check below Steps:

     

    • typo in Class Name: meatdata_mdt seems to be a typo. Ensure the correct API name is used, typically Metadata_Mdt or similar.
    • Map Declaration: The map declaration Map<meatdata_mdt> oppLabelAndAPINameMap=new Map<String,meatdata_mdt>(); is incorrect. It should be Map<String, meatdata_mdt>.
    • Dynamic SOQL and Wrapper Instantiation: Your code dynamically constructs a SOQL query but doesn't handle the response properly with the wrapper class. Ensure that you correctly parse the Opportunity fields' values and instantiate your wrapper class with these values.
    • Logic to Set Values in the Map: Your logic to put values into opportunityAmountMap seems to be commented out incorrectly or has syntax issues.

    check the syntax Apex:

    @AuraEnabled(cacheable=true)

    public static String dispLayAmounts(String oppId) {

    Map<String, meatdata_mdt> oppLabelAndAPINameMap = new Map<String, meatdata_mdt>();

    // Your logic to fill oppLabelAndAPINameMap

    String queryString = ''; // Assuming this is dynamically generated based on metadata

    // Corrected: Initialize the map correctly

    Map<String, oppAmountWarper> opportunityAmountMap = new Map<String, oppAmountWarper>();

    // Your logic to construct queryString based on meatdata_mdt records

    String oppQuery = 'SELECT Id, Name, ' + queryString + ' Amount FROM Opportunity WHERE Id = :oppId';

    for (Opportunity oppRecord : Database.query(oppQuery)) {

    // Your logic to use oppRecord and oppLabelAndAPINameMap to fill opportunityAmountMap

    }

    return JSON.serialize(opportunityAmountMap);

    }

    Incorrect Map Initialization: In LWC, use a JavaScript Map object, not map (case sensitive).

    • Setting Map Values in LWC: The way you're trying to set and log the map values seems incorrect because of asynchronous execution and possibly due to incorrect handling of JavaScript Map.

    import { LightningElement, track, wire } from 'lwc';

    import dispLayAmounts from '@salesforce/apex/YourApexClass.dispLayAmounts';

    export default class OppAmount extends LightningElement {

    @track oppAmountMap = new Map();

    connectedCallback() {

    this.getDispLayAmounts('someOppId');

    }

    getDispLayAmounts(oppId) {

    dispLayAmounts({ oppId: oppId })

    .then((data) => {

    const result = JSON.parse(data);

    Object.keys(result).forEach((key) => {

    this.oppAmountMap.set(key, result[key]);

    });

    // Now oppAmountMap should have values

    })

    .catch((error) => {

    console.error(error);

    });

    }

    }

    • Ensure your Apex method correctly parses Opportunity records and fills the wrapper class instances into the map.
    • Use JavaScript's Map object correctly in LWC. Remember, LWC's reactivity might not track changes inside complex objects like Maps directly. Consider using arrays or plain objects if you continue to face issues with reactivity.
    • Always ensure method and variable names are correctly spelled and match between your Apex and LWC code.
0/9000