+ Start a Discussion
Pawan Kumar 32Pawan Kumar 32 

Update Child ServiceMax object's field based on update to Parent ServiceMax object's field

Hi All,
I want a custom field "OD_Last_Qualified_Date_Update_At__c" on Child Object SVMXC__Installed_Product__c(ServiceMax object) to be updated to value "System.now" when Parent Object SVMXC__site__c's(ServiceMax object) custom field "SVMXC__street__c" is updated to "any value".
In the below code my trigger is not getting fire.
trigger OD_Integration_Trigger on SVMXC__site__c (after update){
  
      set<id> locationIds = new set<id>();      
      if (trigger.isupdate) {
           locationIds.addAll(trigger.newMap.keySet());
           //productsToProcess.add(p);               
           system.debug('@@@@@@@@@@@@@@'+ locationIds);
      
   // update productsToProcess;
    list<SVMXC__Installed_Product__c> productsToprocess= new list<SVMXC__Installed_Product__c>();
        for (SVMXC__Installed_Product__c p : [select id, Name, OD_Last_Qualified_Update_At__c,OD_Sync_Status__c from SVMXC__Installed_Product__c 
where id in :locationIds and Product_Line__c='OnDemand']){
         p.OD_Last_Qualified_Update_At__c = system.now();
         p.OD_Sync_Status__c = 'Pending';
         productsToprocess.add(p);
         }
         system.debug('##########'+ productsToprocess);         
         update productsToprocess; 
         }
}


Any assistance with code or alternate solutions are much appreciated!
Best Answer chosen by Pawan Kumar 32
Neetu_BansalNeetu_Bansal
Hi Pawan,

In your code, there are various flaws, the reason your trigger is not firing because in your query's where clause you are comapring Id of two different objects. Also, if you correct that thing, your trigger will always fire, if you do any change in the Site. You can use the below code:
trigger OD_Integration_Trigger on SVMXC__site__c( after update )
{
	// update productsToProcess;
    List<SVMXC__Installed_Product__c> productsToprocess = new List<SVMXC__Installed_Product__c>();
	
    for( SVMXC__Installed_Product__c p : [ Select id, Name, OD_Last_Qualified_Update_At__c, OD_Sync_Status__c, SVMXC__site__r.SVMXC__street__c from SVMXC__Installed_Product__c where SVMXC__site__c IN: trigger.new and Product_Line__c = 'OnDemand' ])
	{
		if( trigger.oldMap.get( p.SVMXC__site__c ).SVMXC__street__c != p.SVMXC__site__r.SVMXC__street__c )
		{
			p.OD_Last_Qualified_Update_At__c = system.now();
			p.OD_Sync_Status__c = 'Pending';

			productsToprocess.add(p);
		}
    }
	
	system.debug('##########'+ productsToprocess);      
	if( productsToprocess.size() > 0 )
		update productsToprocess; 
}
Let me know if this helps you and mark it as best answer.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Pawan,

In your code, there are various flaws, the reason your trigger is not firing because in your query's where clause you are comapring Id of two different objects. Also, if you correct that thing, your trigger will always fire, if you do any change in the Site. You can use the below code:
trigger OD_Integration_Trigger on SVMXC__site__c( after update )
{
	// update productsToProcess;
    List<SVMXC__Installed_Product__c> productsToprocess = new List<SVMXC__Installed_Product__c>();
	
    for( SVMXC__Installed_Product__c p : [ Select id, Name, OD_Last_Qualified_Update_At__c, OD_Sync_Status__c, SVMXC__site__r.SVMXC__street__c from SVMXC__Installed_Product__c where SVMXC__site__c IN: trigger.new and Product_Line__c = 'OnDemand' ])
	{
		if( trigger.oldMap.get( p.SVMXC__site__c ).SVMXC__street__c != p.SVMXC__site__r.SVMXC__street__c )
		{
			p.OD_Last_Qualified_Update_At__c = system.now();
			p.OD_Sync_Status__c = 'Pending';

			productsToprocess.add(p);
		}
    }
	
	system.debug('##########'+ productsToprocess);      
	if( productsToprocess.size() > 0 )
		update productsToprocess; 
}
Let me know if this helps you and mark it as best answer.

Thanks,
Neetu
This was selected as the best answer
Pawan Kumar 32Pawan Kumar 32
Thank you so much neetu for the help.. I am able to do.
Now I have optimized the code as well. :)
trigger OD_Integration_Trigger on SVMXC__site__c (after update){
System.debug('@@@@@@@@ enter in trigger');
    List<SVMXC__Installed_Product__c> productsToprocess = [Select SVMXC__site__c, SVMXC__street__c,SVMXC__city__c,SVMXC__state__c,SVMXC__zip__c,SVMXC__country__c FROM SVMXC__Installed_Product__c 
WHERE SVMXC__site__c IN : Trigger.newMap.keySet()];
    System.debug('@@@@@@@@@@@@@@ list' + productsToprocess);
    for(SVMXC__Installed_Product__c child :productsToprocess){
    if(trigger.isUpdate && trigger.NewMap.get(child.SVMXC__site__c) != trigger.OldMap.get(child.SVMXC__site__c)){
        child.OD_Last_Qualified_Update_At__c = system.now();
        child.OD_Sync_Status__c = 'Pending';
        }
     }
     if(productsToprocess.size() > 0){
      try{
      update productsToprocess;
      }
      catch (System.Dmlexception e){
      System.debug(e);
      }
     }
}