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
new2forcenew2force 

updating object value in Maps

Hi , iam new to SFDC and came to know u gus here help a lot. i have been facing this problem since three days.

 

Iam here trying to update Map value

 

Map<Id,Object> objMap=new Map<Id,Object>([select id,A,B from Object]);

 

for(Id tkId: taskIds)

{

        objMap.get(Trigger.newMap.get(tkId).whatId).A='Hi'  ;

}

 

update objMap; 

 

this is giving me the error. same kind of thing working for lists. but not for maps.

 

can u help me out here by looping thru the map and update one of the obejct values.

 

Note:   Trigger.newMap.get(tkId).whatId  gives me the Id in Map 

 

thanku.... rock with force........

Best Answer chosen by Admin (Salesforce Developers) 
new2forcenew2force

Hi Steve,

                    finally i am able to search in google with ur piece of advice. i found related code at

 

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_shopping_cart_example_code.htm.

 

according to my requirement, i have written my code like this. It worked

 

Map<Id,Application> appMap=new MapMap<Id,Application>([select id,Status from Application]);
        List<Application> appList=[select id,Status from Application];

 

for(Application app:appList)
        {
            app.Status='Submit';
            appMap.put(app.Id,app);
        }
        
        update appMap.values();

 

 

Thanku so much

All Answers

SteveBowerSteveBower

Well, I don't know about your code since you've only posted snippets, however the "update" statement expects a List<SObject> as an argument, not a Map.

 

So, if you have a map and you want to extract the values of the map for update.   The "values()" method pulls all of the values from a Map and makes a List which you can then feed into the update statement.

 

update objMap.values();

 

Best, Steve.

new2forcenew2force

Hi Steve, thanks  for ur help.

 

I have

 

Map<Id,Application> appMap=new Map<Id,Application>([select id,Name,Status from Application]);

 

Here Application is an Object . Status is the field of Application. Now I have to update Status field for all set of Task Ids. I have taskIds with me.

 

Task ids are some of Trigger ids. So, iam using trigger.newMap down here

 

appMap.get(Trigger.newMap.get(ids).whatId) gives the Id of appMap.

 

for(Id ids: TaskIds)

{

     appMap.get(Trigger.newMap.get(ids).whatId).Status='Submit';    //  Status is picklist field.

 

}

 

Can u give me the piece of code bocz i  have no idea how to use map.values(). I couldnt find about it anywhere in google

new2forcenew2force

Hi Steve,

                    finally i am able to search in google with ur piece of advice. i found related code at

 

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_shopping_cart_example_code.htm.

 

according to my requirement, i have written my code like this. It worked

 

Map<Id,Application> appMap=new MapMap<Id,Application>([select id,Status from Application]);
        List<Application> appList=[select id,Status from Application];

 

for(Application app:appList)
        {
            app.Status='Submit';
            appMap.put(app.Id,app);
        }
        
        update appMap.values();

 

 

Thanku so much

This was selected as the best answer
Varalakshmi Niharika JaguVaralakshmi Niharika Jagu
I have written a simple example for as per my requirement and below code will let you know how to check if a map is empty or not and how to submit it for update.
 
trigger UpdateMultiselectPicklist on Opportunity (before update) {
    Map<Id, Account> accList = new Map<Id, Account>();
    for(Opportunity opp : Trigger.new) {
        if(opp.Multiselect_Picklist__c != null) {
            Account ac = [SELECT Id, Multiselect_Picklist__c, Name FROM Account WHERE Id =: opp.AccountId];
            ac.Multiselect_Picklist__c = opp.Multiselect_Picklist__c;
            accList.put(ac.Id, ac);
        }
    }
    if(accList.keyset().size() > 0) {
    update accList.values();
    }
}

Thank you!