+ Start a Discussion
Itzik LavonItzik Lavon 

begginers question....

i just wrote this code:

trigger updateQuant on Order__c (after insert) {


    Map <id,Item__c> ma=new Map<id,Item__c>();
    for(Order__c o:Trigger.New){
        ma.put(o.Item__c,null);
        
    for(Item__c i: [select id,Price__c,Quantity__c from Item__c where 
                                                id in:ma.keySet()])
        ma.put(i.id,i);
               
               
    for(Order__c q:Trigger.New){
        Item__c t= ma.get(q.Item__c);                                                 
        t.Quantity__c=t.Quantity__c-1;   
    }    
}
}

i tried to chage the Quantity number in the parent  item but it didn't change, where is my mistake?
thanks for the help
Best Answer chosen by Itzik Lavon
MithunPMithunP
Hi Itzik Lavon,

Try this updated trigger.
 
trigger updateQuant on Order__c (after insert) {
    
Set<Id> itemIds = new Set<Id>();
List<Item__c> itemList = new List<Item__c>();
for(Order__c o:Trigger.New){
    itemIds.add(o.Item__c);
}
Map <id,Item__c> ma=new Map<id,Item__c>([Select id,Price__c,Quantity__c from Item__c where id in: itemIds]);
for(Order__c o:Trigger.New){
    Item__c item= ma.get(o.Item__c); 
    item.Quantity__c = item.Quantity__c-1;
    itemList.add(item); 
}
if(itemList.size() > 0){
    update itemList;
}
}

Best Regards,
Mithun.

All Answers

Anoop yadavAnoop yadav
Hi,
Use "before insert" instead of "after insert"
Itzik LavonItzik Lavon
still doesn't work :(
MithunPMithunP
Hi Itzik Lavon,

Try this updated trigger.
 
trigger updateQuant on Order__c (after insert) {
    
Set<Id> itemIds = new Set<Id>();
List<Item__c> itemList = new List<Item__c>();
for(Order__c o:Trigger.New){
    itemIds.add(o.Item__c);
}
Map <id,Item__c> ma=new Map<id,Item__c>([Select id,Price__c,Quantity__c from Item__c where id in: itemIds]);
for(Order__c o:Trigger.New){
    Item__c item= ma.get(o.Item__c); 
    item.Quantity__c = item.Quantity__c-1;
    itemList.add(item); 
}
if(itemList.size() > 0){
    update itemList;
}
}

Best Regards,
Mithun.
This was selected as the best answer
Itzik LavonItzik Lavon

Mithun
thanks alot!!!
it worked, now i see what i was missing :)
MithunPMithunP
Select Best answer for your question, if your problem solves