function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mack DsozaMack Dsoza 

Not getting key value in Map

If I am not getting any key as well as values from Map then please tell me that what are the things that needs to be checked.

Here is my map code,

Map<Id,asset__c> & here I am not getting any key value so what I have to do for it..

 

Regards...

bob_buzzardbob_buzzard

Giving us the map type is not enough information.  I'm assuming from this post that your map is empty when you come to process it.  You'll need to post the code that populates the map in order for us to suggest anything.

Ankit AroraAnkit Arora

I second Bob, your req is not clear. But if you talking about checking that a particular value exists in map of not then here is the code :

 

Map<Id,asset__c> M = new Map<Id,asset__c>() ;
if(M.containsKey('XXXXXXXXXXXXXXX'))
{
    //Do Something
}

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Mack DsozaMack Dsoza

public class AssetAllocationBean {

 

    public MAP<ID,Asset__c> assetMap {

                                        get {

                                            if(assetMap == null) {

                                                assetMap = new Map<ID,Asset__c>();                                             

                                            }

                                            return assetMap;

                                        }

                                        set;

}

}

 

This is my bean class which is taking values but I am confuse in bean concept whether bean accpets or sets values only from VFpage or it can accepts or sets values from database too..

If it accepts values from database too so that how to assign value into bean with database values.

 

bob_buzzardbob_buzzard

Your bean will accept values from both the page and the database, but only if you extract those values and set them into the map.  Are you expecting some sort of automatic population of the map?

Mack DsozaMack Dsoza

Yaa exactly Bob, actually on click of save button it should create map which will store the values of all my custom object so that I can use that values in another form.

If I want to populate values in this bean then what i have to do ?

bob_buzzardbob_buzzard

You'll have to come up with the key/value pairs and add them to the map.  

 

Something like:

 

AssetAllocationBean aabean=new AssetAllocationBean();

aabean.assetMap.put('Key1', 'Value1');

 

where you replace the key1/value1 literals with the actual values you want to store.

Mack DsozaMack Dsoza

But if i want to access values from database then what i have to do for that.

 

Since i am working on three classes as, ControllerClass, BeanClass & ServiceClass.

Controller just handles action

Bean Initializes a fields

Service holds my entire business logic

 

So also tell me where should i write my code to put values in map...

Regards...

bob_buzzardbob_buzzard

You can populate a map with a database query as follows:

 

AssetAllocationBean aabean=new AssetAllocationBean();

aabean.assetMap.putAll([select id from Asset__c]);

 Replacing the rather basic SOQL query with your own requirement.

Mack DsozaMack Dsoza

Since I have made entire things according to MVC pattern....

I have written these function to get assetMap

private Map<ID,Asset__c> getAssetMap(String entityId) {

        Map<ID,Asset__c> assetMap = new Map<ID,Asset__c>();
      
      try {        
          List<Asset__c> assetList;

// Gets values from Database object...
          assetList = databaseObj.getAssets(entityId);
        
          for(Asset__c asset : assetList) {
              assetMap.put(asset.Id,asset);
      }
      }catch(Exception ex) {
         throw new GeneralException('AssetAllocationService.getAssetMap : Internal Error ' + ex.getMessage());  
        }       
        return assetMap;    

    }

 

But nw the problem is how to assign this returning value to my bean or since i added the value in it so will it get automatically assign..or is there any mistake in my above mentioned code.

bob_buzzardbob_buzzard

You can assign the contents of one map to another using the putAll method.

 

E.g.

 

AssetAllocationBean aabean=new AssetAllocationBean();

aabean.assetMap.putAll(myObject.getAssetMap(entityId));

 

 

 

Mack DsozaMack Dsoza

Getting an error...

 

Error: NewAssetAllocationController Compile Error: Method is not visible: [AssetAllocationService].getAssetMap(String) at line 17 column 35

bob_buzzardbob_buzzard

This sounds like your method is private and needs to be made public so that other classes can access it.

Mack DsozaMack Dsoza

I created instance of my Bean class then printed value of that  instance...& I am getting all value as null.

So can u tell me wht mistake i am doing ?

 

Fetch asset allocation bean value is : AssetAllocationBean:[assetMap=null, assetRecordTypeBeanList=null, goalAssetAssociationMap=null, goalInsuranceAssociationMap=null, goalInvAssetAssociationMap=null, headerList=null, insuranceMap=null, investmentAssetMap=null]

bob_buzzardbob_buzzard

Have you assigned the value to the beans.

Mack DsozaMack Dsoza

I have sent code to you through private message. 

So check that code, As I think that might be problem in my assignment of the value bt I am nt getting it so please tell me the solution.

But according to me that I have written/assign in bean but u also chk that...

bob_buzzardbob_buzzard

You'll need to post your code on here I'm afraid.  It doesn't add to the knowledge if we go offline part way through.  

 

There's also a lot of code there - can you just post the important parts for now.

Mack DsozaMack Dsoza
public class AssetAllocationBean { 
    public MAP<ID,Asset__c> assetMap { 
                                        get {
                                            if(assetMap == null) {
                                                assetMap = new Map<ID,Asset__c>();                                              
                                            }
                                            return assetMap;
                                        }
                                        set;
                                    }                                                                                    
    public Map<String,List<Investment_Asset__c>> investmentAssetMap { get;set;}
    public Map<ID,Insurance__c> insuranceMap { get;set;}    

    // ID will be Goal ID
    public Map<ID,List<GoalAssetAssociation__c>> goalAssetAssociationMap {get;set;}
    public Map<ID,List<GoalInvestmentAssetAssociation__c>> goalInvAssetAssociationMap {get;set;}
    public Map<ID,List<GoalInsuranceAssociation__c>> goalInsuranceAssociationMap {get;set;}
    
    
    public List<AssetRecordTypeBean> assetRecordTypeBeanList { 
                                                            get {
                                                                    if(assetRecordTypeBeanList == null) {
                                                                        assetRecordTypeBeanList = new List<AssetRecordTypeBean>();
                                                                    }
                                                                    return assetRecordTypeBeanList;
                                                                }   
                                                            set;
                                                        }

        
        public List<GoalAssetMapping>  goalAssetMappingList {get {
                                                                    if(goalAssetMappingList == null) {
                                                                        goalAssetMappingList = new List<GoalAssetMapping>();
                                                                    }
                                                                    return goalAssetMappingList;
                                                                } 
                                                                set;}
        
        public GoalAssetMapping populateGoalAssetMappingList(ID goalId,String assetAllocated) {
            GoalAssetMapping bean = new GoalAssetMapping();
            bean.index = goalAssetMappingList.size();
            bean.goalId = goalId;
            bean.assetAllocated = assetAllocated;
            goalAssetMappingList.add(bean);
            return bean;
        }
    public void populateGoalAssetMappingList(GoalAssetMapping mappingBean) {            
            mappingBean.index = goalAssetMappingList.size();
            goalAssetMappingList.add(mappingBean);            
    }   
    }   
    
    public class GoalAssetMapping {
        public Integer index {get;set;}
        public ID goalId {get;set;} 
        public String goalAssetAssociationId;                    
        public Decimal goldAllocatedPercent { get;set;}
        public Decimal debtAllocatedPercent {get;set;}
        public Decimal equityAllocatedPercent {get;set;}                    
        public String assetAllocated {
                                        get {
                                            if(assetAllocated == null){
                                                assetAllocated = '';                                                
                                            }
                                            return assetAllocated;
                                        }
                                        set;
                                    }
    }
    
}

 

bob_buzzardbob_buzzard

Where are you creating your AssetAllocationBean records and setting up the assetmap?  I can't see anything in that code that is populating the map.

Mack DsozaMack Dsoza

I am also not getting why it is not accessing,

Since it is not getting values so can u please tell me from where it is trying to fetch values or general idea that whenever I create get & set method then from where it fetches values so that i can try to solve it..

Is their any idea to fill values in it so that porblem will get solved..

bob_buzzardbob_buzzard

Presumably you have some code in your system that is creating an AssetAllocationBean?

 

As there isn't any code in the bean itself to populate the assetMap, should you be setting up the assetMap when you create it?  If not, how are you planning to set the values?

 

 

Mack DsozaMack Dsoza

Can u just give me an idea or little bit code to initialize my bean. 

As i am doing that I have created getAssetMap method in controller which put the key & values in it...

Following is the code...

But tell me also that it is returning value as assetMap & redmarked text will not assign the value...

    /*
      Asset Map is holding ALL assets for any given parent and its child entities
    */
    private Map<ID,Asset__c> getAssetMap(String entityId) {
        Map<ID,Asset__c> assetMap = new Map<ID,Asset__c>();
      
      try {        
          List<Asset__c> assetList;
          assetList = databaseObj.getAssets(entityId);
        
          for(Asset__c asset : assetList) {
              assetMap.put(asset.Id,asset);
      }
      }catch(Exception ex) {
         throw new GeneralException('AssetAllocationService.getAssetMap : Internal Error ' + ex.getMessage());  
        }       
        return assetMap;    
    }

Ankit AroraAnkit Arora

@Bob Don't give up please :)

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

bob_buzzardbob_buzzard

The assetMap that you are putting the values into is not the same assetmap as that stored in your bean is it?

 

The redmarked text assigns to the local assetMap to the method.  Which class is this method from?

Mack DsozaMack Dsoza

Actually that method named getAssetMap is in my service class.

Actually We have created with MVC pattern,

 

1) VFPage - generally user interface
2) Controller - handles the action of user 
3) Bean - Initialization of field values will be handled by this class
4) Service - All calculation & business logic will be handled by this class
5) DBSOQL - Entire project queries to fetch values will be handles by this class
6) DBDML - All DML operation will be handled by this class
So that method is in my service class...
bob_buzzardbob_buzzard

And which of these should be writing the assetMap information into the bean class?

Mack DsozaMack Dsoza

In this, service class has method as getAssetMap method which is fetching values from DBSOQL class.

& it is returning values as assetMap.

 

Since it is returning values so it will directly will get assign to Bean..

Bcoz whenever we write any get & set method named account so when we write getAccount & return its value as account then its value gets assigned to Bean...

bob_buzzardbob_buzzard

But your service class has a getter that instantiates a new map and returns that - it doesn't store any information elsewhere in either that class or the bean class.