+ Start a Discussion
Force.platformForce.platform 

can we update field of unrelated object in trigger

I have two objects:
1)EProduct__c with field Quantity__C
2)EOrder__C with field OQuantity__c
my req. is when order get placed Quantity on product should get reduce as per quantity in order
 
prateek jainprateek jain
Hi 
Please provide more details 
Thanks
Force.platformForce.platform
Hi Prateek,
there are two objects:
1)EProduct__c with field Quantity__C
2)EOrder__C with field OQuantity__c and Product__C
there is no any relation between them.
when we create order record with Quantit__C. and also Product__C(text) field on EOrder__C is same as EProduct__c name
then
         Quntity on EProduct__c=Quntity on EProduct__c - Quantity on EOrder__c
Ashutosh Limaye 8Ashutosh Limaye 8
Fire a SOQL on the EProduct__c Object where Name = Product__c on the EOrder__c object. Of course the Product name should be unique on the EProduct__c object. 
Retrive the Quantity__c field in the SOQL above. Update the field by deducting the OQuntity__c from it (this will also help you valdiate whether there is enough quantity in the EProduct__c for the EOrder__c to be executed.
Update the EProduct__c record.
prateek jainprateek jain
write this code in your trigger on EOrder__c  After save trigger
for( EOrder__c tempEOrder:Trigger.new)
{  
integer quantityEOrder=tempEOrder.OQuantity__c ;
 list<EORder__c> temp=[select Product__C from EOrder__C  ];
EProduct__c tempproduct= [select  id,Quantity__C from EProduct__c where Name=:temp.Product__C];
  Integer finalQuantity=tempproduct.Quantity__C-quantityEOrder;
tempproduct.Quantity__C= finalQuantity;
update tempproduct;
}


 
prateek jainprateek jain
If you face any Issue let me know i will be happy to help
Thanks
 
Force.platformForce.platform
hi prateek
its not updating quantity on EProduct__C object 
prateek jainprateek jain
HI 
Use this Code
trigger ANS on EOrder__C (After insert) {

    
    for(EOrder__C temp:Trigger.new){
        
         Decimal tempQuantity=temp.Oquantity__c;
        string tempProduct=temp.Product__c;
        
        EProduct__c tempObject1record=[select Name,id,Quantity__c from EProduct__c where Name=:tempProduct]; 

            Decimal TempQuantyObject1=tempObject1record.Quantity__c;
            Decimal finalQuanty=TempQuantyObject1-tempQuantity;
        
        tempObject1record.Quantity__c=finalQuanty;
        
         update tempObject1record ;   
        
    }
    
    
}

Check the API  name of fields and Objects 
And After Saving  Record of  EOrder__C  refresh the  EProduct__c  object Record 
If any issue please let me know. I am happy to solve it .
Thanks.
Ashutosh Limaye 8Ashutosh Limaye 8
trigger updateProductQuantity on EOrder__c (After insert){
     
      Set<String> productNames=new Set<String>();
     for(EOrder__c order: Trigger.new){
        productNames.add(order.Product__c);
     }
      // SOQL outside for loop....
      List<EProduct__c> prodcutList=[Select id,Name,Quantity__c from EProduct__c 
                                                         where  Name IN : productNames];
      //Main loop starts...
      for(EOrder__c order: Trigger.new){
        for(EProduct__c product: productList){
             //Second part of if checks if there are enough products for the current order.
             if(order.Product__c == product.Name && (product.Quantity__c - order.Quantity__c)>0){
                    product.Quantity__c=product.Quantity__c - order.Quantity__c;
                 }
              }
        }
        try{
             Database.update(productList,FALSE);
        }
       catch(DMLException e){
            System.debug(e);
       }
}
This should take care of bulkification and Governors...

 
prateek jainprateek jain
Hi
you can achieve  this with process builder and flow