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
BobBob 

Apex Trigger Error: Initial term of field expression must be a concrete SObject: Map<Id,Warranty__c>

I keep getting this error message in my developer console, but I'm not sure why.

trigger Trigger_UpdateWarranties on Unit__c (after delete, after insert, after update) {
    
    if (trigger.isdelete)
       delete [select id from Warranty__c where product__c in :trigger.oldmap.keyset()];
    if (trigger.isinsert || trigger.isupdate)
    {
         map<id,Warranty__c> mapw = new map<id,Warranty__c>([select id,product__c from Warranty__c where product__c in :trigger.newmap.keyset()]);
         list<Warranty__c> lsw = new list<Warranty__c>();

          for (unit__c u : trigger.new)
           {
              boolean exist = false;
                 for (Warranty__c w : mapw.values)
                {
                    if (u.id == w.product__c)
                    {
                        exist = true;
                        break;
                     }
                     //may enhance your logic here
                  }
              if (!exist)
              {
                   Warranty__c w1 = new Warranty__c();
                   w1.Commercial_Parts__c = Warranty__c.Commercial_Parts__c;
                   lsw.add(w1);
               }
    }
       if(lsw.size() > 0)
        insert lsw;
    

    }
}
PuneetsfdcPuneetsfdc
You are getting error on this line
 
map<id,Warranty__c> mapw = new map<id,Warranty__c>([select id,product__c from Warranty__c where product__c in :trigger.newmap.keyset()]);

you have written a trigger on Unit__c and creating an map after querying on Warranty BUT in where clause you have used

trigger.newMap.keyset() having values on Unit__c
( trigger.newMap equivalent to map<Id, Unit__c> )

to solve this you have make a set<Id>, something like
 
Set<Id> productSetId = new Set<Id>();
for(Unit__c unit : trigger.new) {
    productSetId .add(unit.product__c);
}
// use this set<Id> to query on Warranty__c

map<id,Warranty__c> mapw = new map<id,Warranty__c>([select id,product__c from Warranty__c where product__c in: productSetId ]);


and above will work.

 
BobBob
I adding the code you suggested and now I am getting a Variable does not exist: lsw at line 34 column 16. Any help would be greatly appreciated. trigger Trigger_UpdateWarranties on Unit__c (after delete, after insert, after update) { if (trigger.isdelete) delete [select id from warranty__c where product__c in :trigger.oldmap.keyset()]; if (trigger.isinsert || trigger.isupdate) { Set productSetId = new Set(); for(Unit__c unit : trigger.new) { productSetId .add(unit.product__c); } map mapw = new map([select id,product__c from Warranty__c where product__c in: productSetId ]); for (unit__c u : trigger.new) { boolean exist = false; for (warranty__c w : mapw.values) { if (u.id == w.product__c) { exist = true; break; } //may enhance your logic here } if (!exist) { Warranty__c w1 = new Warranty__c(); w1.Commercial_Parts__c = Warranty__c.Commercial_Parts__c; lsw.add(w1); } } if(lsw.size() > 0) insert lsw; }