0

in 5 related case records if status__c pilklist is yes and checkbox is true ,then in its parent account ,the custom field no_of_closed_status_in_case__c should get updated as 5 .for this above trigger i am trying it using lists and maps but it is throughing error as

Error: Compile Error: DML requires SObject or SObject list type: Map<Account,Decimal> at line 31 column 11  

--

trigger statupdtrecbulk on Case (before insert) {


list<id> accslst = new list<id>();
 for(case cs: trigger.new)
   {
       if(cs.status__c=='yes'&& cs.check_box__c==True)
       accslst.add(cs.id);

   }
      list<account> acc = new list<account>();
acc = [select id,no_of_closed_status_in_case__c,(select id,check_box__c,accountid from cases) from account where id in :accslst];

      map<account,decimal> alst = new map<account,decimal>();
   for(account ac1l : acc)
   {
     if(ac1l.no_of_closed_status_in_case__c == NULL)
         {
        ac1l.no_of_closed_status_in_case__c = 0;
        ac1l.no_of_closed_status_in_case__c=ac1l.no_of_closed_status_in_case__c+1;
          alst.put(ac1l,ac1l.no_of_closed_status_in_case__c);
         } 
         else
         {
          ac1l.no_of_closed_status_in_case__c=ac1l.no_of_closed_status_in_case__c+1;
          alst.put(ac1l,ac1l.no_of_closed_status_in_case__c);
      } 

   } 
      update alst;

 }

2 Answers 2

1

I would advise against using SObject records as map keys. Usually using their Id is much simpler and more stable. Regardless, the error describes exactly what the problem is, you can only update two data structures: SObject or List<SObject>. Here you are using a collection, so you need the latter:

List<Account> accounts = new List<Account>(alst.keySet());
update accounts;
1

Krishna,

You cannot update a map like that. Sets are not available for DML.

If you want to optimize, try changing your code to this:

list<id> accslst = new list<id>();
 for(case cs: trigger.new)
   {
       if(cs.status__c=='yes'&& cs.check_box__c==True)
       accslst.add(cs.id);

   }
      list<account> acc = new list<account>();
acc = [select id,no_of_closed_status_in_case__c,(select id,check_box__c,accountid from cases) from account where id in :accslst];

   for(account ac1l : acc)
   {
     if(ac1l.no_of_closed_status_in_case__c == NULL)
         {
        ac1l.no_of_closed_status_in_case__c = 1;
         } 
         else
         {
          ac1l.no_of_closed_status_in_case__c += 1;
          } 

   } 
      update acc;
7
  • still it is throwing the error Commented Nov 30, 2015 at 18:27
  • Error: Compile Error: DML requires SObject or SObject list type: Set<Account> at line 31 column 11 Commented Nov 30, 2015 at 18:28
  • Can I see how your code looks now? Commented Nov 30, 2015 at 18:40
  • I edited the answer to include some a better way Commented Nov 30, 2015 at 18:46
  • You can't update Set<SObject>. You also added a query in a for loop.
    – Adrian Larson
    Commented Nov 30, 2015 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .