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
Force.platformForce.platform 

update parent record by trigger

I have two object EProduct__C and EOrder__C. lookup filed on EOrder__C is EP_Del__C. and there are anather two field
Quantity__C on EProduct__C and
OQuantity__C on  EOrder__C 
I want to update Quantity on EProduct when on updation in Quantity of EOrder. update parent quantity when child quantity is updated
Best Answer chosen by Force.platform
Ajay K DubediAjay K Dubedi
Hi Arati,
Please try this code,
trigger UpdateQuantity on EOrder__c (after update) {
	Set<Id> pro_id=new Set<Id>();
    for(EOrder__c eod: Trigger.new){
        if(eod.EP_Del__c!=null){
            pro_id.add(eod.EP_Del__c);
        }
    }
    if(pro_id.size()>0){
    List<EProduct__c> pro_list=[Select id, Quantity__C from EProduct__c where ID IN:pro_id];
    if(pro_list.size()>0){
    for(EOrder__c eod: Trigger.new){
        for(EProduct__c epro: pro_list){
            if(eod.EP_Del__c==epro.id){
				epro.Quantity__c=eod.OQuantity__c;
            }
        }
    }
	update pro_list;
   }
   }
}

Thanks,
Ajay
 

All Answers

Shashank Prakash MishraShashank Prakash Mishra
In this case can we try in Process Builder?
Ajay K DubediAjay K Dubedi
Hi Arati,
Please try this code,
trigger UpdateQuantity on EOrder__c (after update) {
	Set<Id> pro_id=new Set<Id>();
    for(EOrder__c eod: Trigger.new){
        if(eod.EP_Del__c!=null){
            pro_id.add(eod.EP_Del__c);
        }
    }
    if(pro_id.size()>0){
    List<EProduct__c> pro_list=[Select id, Quantity__C from EProduct__c where ID IN:pro_id];
    if(pro_list.size()>0){
    for(EOrder__c eod: Trigger.new){
        for(EProduct__c epro: pro_list){
            if(eod.EP_Del__c==epro.id){
				epro.Quantity__c=eod.OQuantity__c;
            }
        }
    }
	update pro_list;
   }
   }
}

Thanks,
Ajay
 
This was selected as the best answer