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
Maria22Maria22 

Trigger not updating parent's record upon updating recently modified filed value from related objects

Hi All,

I hope everyone is doing good. I turned up here for a valuable advise from all of you experts.

My usecase is as below:

We have 2 objects "Accounts" and "Orders". I need to update one field on account object called as "Upgrade Order Status"(API Name- Upgrade_Order_Status__c) which is a text field with the value of "Order Status" field of only that Order where new Order is being created as Order Type = 'Upgarde Order' or an already existing order with Order type='Upgarde Order' and status is getting changed.

Order type is a picklist field on Order object having multiple values in which "Upgrade Order" is one of among them. Account can have multiple Orders with different Order Type.But my use case is to populate account's "Upgrade Order Status" field with the Order status field value of recently created order having order type= "Upgarde Order' OR there is a change in Order Status field of an already existing Orders of type "Upgrade Order".

Illustartion:
Account Name: Account Demo
has 5 Orders with different Order Type
1. Order1 with Order Type= "ABC" with Order Status= "New"
2. Order2-with Order Type= "Upgrade Order" with Order Status="Open"
3. Order3 with Order Type= "XYZ" with Order Status="Open"
4. Order4 with Order Type="Upgrade Order" with Order Status= "New"
5. Order 5 with Order Type="ABCQRS"with Order Status= "Closed"
So we need to populated "Upgrade Order Status" with the status value of recently created/changed Orders's Status where Order Type= "Upgrade Order".So if suppose among 5 above mentioned orders if Order 4 is recently created  orders with Order Type="Upgrade Order" then "Upgrade Order Status" on account should be populated with value "New" because Order 4 is having Order Status= "New".
In case if we modify Order 2 and changed the status from "Open" to "Closed" then "Upgrade Order Status" on account should be populated with value "Closed" because Order 2 is recently modified order with order type=Upgarde Order.

My issues is
Trigger is working for when I am creating new Order and status is getting copied to account's field OR when I am changing the status of Order then also status is getting copied to account's field. But its is working only for the recently cretaed Orders.It is not working when I tried to change the status of an older Orders with Order type= Upgarde Order then status is not getting copied to Account's field. 

Below is my Trigger Code:
 
trigger OrderTrigger on Order (after insert, after update) {

 Map<ID, Account> parentAccts = new Map<ID, Account>(); //Making it a map instead of list for easier lookup

Set<Id> setIds=new Set<Id>();

  for (Order childObj : Trigger.new) {
    setIds.add(childObj.Bill_to_Customer__c);
  }

 system.debug('The value is: ' + parentAccts);
  parentAccts = new Map<Id, Account>([Select id,Type,Upgrade_Order_Status__c,(Select id,Type,OrderNumber,Bill_to_Customer__c,Status from Orders where Type ='Upgrade Order') from Account WHERE ID IN :setIds]);


  for (Order order: Trigger.new){
      
      if(Order.Type=='Upgrade Order'){
      
     Account myParentAcct = parentAccts.get(order.Bill_to_Customer__c);
       system.debug('The value is: ' + myParentAcct.Upgrade_Order_Status__c );    
     myParentAcct.Upgrade_Order_Status__c = order.Status;
     
      }
  }
system.debug('The value is: ' + parentAccts);
  update parentAccts.values();
}

Account is Parent Object
Order is related object.

Kindly help/suggests on above.

Any help will be greatly appreciated

Many thanks in advance

Thanks & Regards,
Harjeet​
v varaprasadv varaprasad
Hi Harjeet,

Please check once below code : 
 
trigger OrderTrigger on Order (after insert, after update) {

 Map<ID, Account> parentAccts = new Map<ID, Account>(); //Making it a map instead of list for easier lookup

Set<Id> setIds=new Set<Id>();

  for (Order childObj : Trigger.new) {
     if(childObj.Bill_to_Customer__c != null && childObj.Type=='Upgrade Order') 
        setIds.add(childObj.Bill_to_Customer__c);
  }

 
  parentAccts = [Select id,Type,Upgrade_Order_Status__c from Account WHERE ID IN :setIds]);
  system.debug('The value is: ' + parentAccts);

  for (Order order: Trigger.new){     
      
     Account myParentAcct = parentAccts.get(order.Bill_to_Customer__c);
       system.debug('The value is: ' + myParentAcct.Upgrade_Order_Status__c );    
     myParentAcct.Upgrade_Order_Status__c = order.Status;     
      
  }
system.debug('The value is: ' + parentAccts);
  update parentAccts.values();
}



Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
Maria22Maria22
Hi Varaprasd,

Thanks for your response. I tried with the code whichyou have provided still I am at same place. Actually wheever I am trying to create a new order with Order Type= Upgrade Order then status of order is getting copied to Account's field "Upgrade Order Status" which is correct. Next when I tried to create second order with different order type then status of second order is not getting copied to account's field which is also correct.
Now when I tried to edit the first order which is of Order Type= Upgrade Order and changed the status then status of second order which was of other order type is getting copie dto account's field.Infact upon updation/modification also only status of order with Order type=Upgrade Order should get copied.

I created another 3rd Order with Order Type=Upgarde Order and status of 3rd order is getting copied to account's field which is also correct.After this I went to first Order again which is of Order Type=Upgarde Order and tried toc henge the status but upon saving modified status of 1st order is not getting copie dto account's field.

So the problem is with when I am trying to update an order(order type=Upgrade Order) which is already existing then the status of recently modified order is not getting copied.

Positive cases:
1.Only working when I am trying to create a new order with order Type= Upgarde Order then status is getting copied to account's field-Correct Behaviour
2. When I tried to create an Order with different order type then status is not getting copied-Correct Behaviour

Below scnearios not working
1. When I create 2 orders with order tYpe=Upgarde Order and tried to update status of  first order thne also status of 2nd order is getting copied to account's field.

Conclusion: Trigger is only working on insertion of new orders with order typ= Upgrade Order
Trigger is not working on recently modified Order with Order Type= Upgarde Order infact trigger is only working on recently created

Below is my trigger and trigger handler:
trigger OrderTrigger on Order (after insert, after update) {
				
    OrderTriggerHandler orderhandler= new OrderTriggerHandler(trigger.new);
    
     if(trigger.isAfter && trigger.isInsert) {  
         orderhandler.HandleAfterInsert();
     }
    
    if(trigger.isAfter && trigger.isUpdate){
        orderhandler.HandleAfterUpdate();
    }  
}
 
public class OrderTriggerHandler {
   List<Order>newOrders;
 
    public OrderTriggerHandler(List<Order>newOrders){
        this.newOrders=newOrders;
        
    }
    
    public void HandleAfterInsert(){ 
       // updateOrderUpgradeStatus();
    }
    
      public void HandleAfterUpdate(){
            // updateOrderUpgradeStatus();
      }
    
    public void updateOrderUpgradeStatus(){
        
         Map<ID, Account> parentAccts = new Map<ID, Account>(); //Making it a map instead of list for easier lookup
          //List<Id> listIds = new List<Id>();
            Set<Id> setIds=new Set<Id>();
        
            for (Order childObj : newOrders) {
            setIds.add(childObj.Bill_to_Customer__c);
          }
        
            system.debug('The value is: ' + parentAccts);
            parentAccts = new Map<Id, Account>([Select id,Type,Upgrade_Order_Status__c,(Select id,Type,OrderNumber,Bill_to_Customer__c,Status from Orders where Type ='Upgrade Order') from Account WHERE ID IN :setIds]);
          //list<Account> parentAccts=new list<Account>();
          //parentAccts = ([Select id,Name,Type,Upgrade_Order_Status__c,(Select id,Type,OrderNumber,Status,Recipient__c from Orders where Type ='Upgrade Order') from Account WHERE ID IN :listIds ]);
          for (Order order: newOrders){
              
              if(Order.Type=='Upgrade Order'){
              
             Account myParentAcct = parentAccts.get(order.Bill_to_Customer__c);
               system.debug('The value is: ' + myParentAcct.Upgrade_Order_Status__c );    
             myParentAcct.Upgrade_Order_Status__c = order.Status;
             
              }
          }
        system.debug('The value is: ' + parentAccts);
          update parentAccts.values();
            }
        
    }

Any help will be greatly appreciated

Mnay thanks in advance

Thanks & Regards,
Harjeet​
Maria22Maria22
Hi Varaprasd,

I again cross checked and found that trigger is working fine only for insertion of a new order with Order type= Upgarde Order and status of the order is getting copied to account's field.

Also trigger is not updating the account;s field if trying to createa new ordre if order type is not equas to Upgrade order-Correct behaviour.

But when trying to modify the status of already existing order then instaed of copying the modified status trigger is updating the account's field with the status of last created order's status irresprctive of order type.
I guess there is some problem when updation is happening.Ideally trigger should copy the status of modified status of only order with Order Type= Upgrda eOrder but currently irrespective of any order type trigger is copying the status from Order to account's field.

I checked the debug logs.In debug logs I am getting the correct value but don't know why its not updating on UI.

Kindly help

Any help will be greatly appreciated

Many thanks ina dvance

Thanks & Regards,
Harjeet