+ Start a Discussion
Sam SatterfieldSam Satterfield 

Trigger that updates the status of one custom object to match another custom object

Hello Y'all

I am new to APEX and Salesforce in general; but I've been tasked with creating a trigger, like what is described in the title. I've been working on this for a couple days now and can't seem to get this to work. Any and all advice would be appreciated.

I am trying to have the field: Data_Conversion_Task_List_If_Needed__c on Object: New_Office_Setup__c update itself to match the field: Status__c on Object: Analytics__c whenever Analytics__c is updated.

What I have so far, is copied from old post that I have subbed the objects and field from my salesforce into:
===========================================================================

trigger UpdateTriggerTest on Analytics__c (before insert, before update) 
{
  //get a list of all the object 2 ID's contained in the records
  //to be updated.
  List<New_Office_Setup__c> NOSIDs = new list<New_Office_Setup__c>();
  //List<List<Analytics__c>> AnaIDs = new List<list<Analytics__c>> ([select id, Open_Projects_Analytics__c from Analytics__c]);
  for (Analytics__c Ana: trigger.new)  
      
  {
   NOSIDs.add(Ana.Open_Projects_Analytics__c );
  }
    
  //now get a list of all the records for object 2 that contain the
  //above IDs
  List <New_Office_Setup__c> NOS = new List <New_Office_Setup__c> ([select id, Data_Conversion_Task_List_If_Needed__c from New_Office_Setup__c where id in: NOSIDs]);

  //now loop again for all the records being updated and then for each
  //one loop through all the object 2 records retrieved above.
  for (Analytics__c Ana: trigger.new){
  
    //we do this for loop differently so that it has an inherent check
    //to ensure that our query above returned some records
    for (integer i = 0; i < NOS.size(); i++){

      //now we make sure the record IDs match
      if (Ana.Open_Projects_Analytics__c == NOS[i].id){

        NOS[i].Data_Conversion_Task_List_If_Needed__c = Ana.Status__c;
      }
    }
  }
  //update all the object 2 records
  update NOS;
}
======================================================================

I would also appreciate any advice on how to construct the test class for this. The test class seems to be the hardest code for me to write so far. At least for the handful of codes I've written before this.

Best,
Sam. S.
Best Answer chosen by Sam Satterfield
Balaji BondarBalaji Bondar
Hi Sam,
I have rewritten the trigger as below.Please udpate the trigger for API name /any logic change:
trigger UpdateTriggerTest on Analytics__c (after update) {
Map<Id, Analytics__c>  NOSIDAnalyticsMap = new Map<Id, Analytics__c>  ();
List<New_Office_Setup__c> NOSList = new list<New_Office_Setup__c>();

	for (Analytics__c AnalyticsObj : trigger.new){
		NOSIDAnalyticsMap.put(AnalyticsObj.Open_Projects_Analytics__c ,AnalyticsObj);
	}
	
	for(New_Office_Setup__c NOSObj :[elect id, Data_Conversion_Task_List_If_Needed__c from New_Office_Setup__c where id in: NOSIDAnalyticsMap.keyset()]){
		if(NOSIDAnalyticsMap.containskey(NOSObj.Id)){
			NOSObj.Data_Conversion_Task_List_If_Needed__c = NOSIDAnalyticsMap.get(NOSObj.Id).Status__c;
			NOSList.add(NOSObj);
		}
	}
	update NOSList;
}
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.

All Answers

Balaji BondarBalaji Bondar
Hi Sam,
I have rewritten the trigger as below.Please udpate the trigger for API name /any logic change:
trigger UpdateTriggerTest on Analytics__c (after update) {
Map<Id, Analytics__c>  NOSIDAnalyticsMap = new Map<Id, Analytics__c>  ();
List<New_Office_Setup__c> NOSList = new list<New_Office_Setup__c>();

	for (Analytics__c AnalyticsObj : trigger.new){
		NOSIDAnalyticsMap.put(AnalyticsObj.Open_Projects_Analytics__c ,AnalyticsObj);
	}
	
	for(New_Office_Setup__c NOSObj :[elect id, Data_Conversion_Task_List_If_Needed__c from New_Office_Setup__c where id in: NOSIDAnalyticsMap.keyset()]){
		if(NOSIDAnalyticsMap.containskey(NOSObj.Id)){
			NOSObj.Data_Conversion_Task_List_If_Needed__c = NOSIDAnalyticsMap.get(NOSObj.Id).Status__c;
			NOSList.add(NOSObj);
		}
	}
	update NOSList;
}
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
This was selected as the best answer
Sam SatterfieldSam Satterfield
Thank you Balaji,

This works perfectly I didn't have to change anything. This shows me that I need to spend a lot more time review the literature on salesforce coding. Any suggestions on where I should start on the test class?