+ Start a Discussion
Raj88Raj88 

Insert function using map in trigger

Trigger:

trigger trg_insertdeal on DealProduct__c (after insert)
{
    List <DealProduct__c> dplist = new List <DealProduct__c >();
     map<id,Deals__c> mapDeal = new map<id,Deals__c>([Select id,name from Deals__c]);
     map<id,DealProduct__c> dealprod = new map<id,DealProduct__c>();
     map<Id,Products__c> product = new map<Id,Products__c>([select Id,NHSListPrice__c from Products__c where name =:  dplist.list__r.name]);
     if(trigger.isInsert)
      {                                
        for(DealProduct__c obj :Trigger.new)
        {
        if(product.size() > 0)
        {
       DealProduct__c dealprd = new DealProduct__c(
       NHSListPrice__c = product.get(obj.id).NHSListPrice__c);
       dplist.add(dealprd);
        }
        }
       }
}

Actually I am trying to insert 'NHSListPrice__c' field from 'Products__c' object to 'DealProduct__c' object. I cant insert. whether anything is wrong with the coding?
sachin_dreamoncloudsachin_dreamoncloud
if(trigger.isInsert)
      {                               
        for(DealProduct__c obj :Trigger.new)
        {
        if(product.size() > 0)
        {
       obj.NHSListPrice__c = product.get(obj.id).NHSListPrice__c;
       dplist.add(obj);
        }
        }
       }

If you create an instance of the object, u need to specify the record id for the update action.