+ Start a Discussion
jucuzoglujucuzoglu 

How do I perform an update to a field in a related object on creation or update

I have a custom object related to another custom object. When a particular field is updated on the parent object I would like to update a corresponding field on a child object.

 

I am new to Apex coding so might need a bit of hand holding.

 

Essentialy I need to:

 

1. Establish the trigger to happen when an object is updated

2. Get the value for a particular field

3. Evaluate that value  and fire off the next process if a criteria is met. (I think I can handle this one)

4. Reference the child object (? By sorting through all of that child object type where the parent ID field is equal to the parent record ?)

5. Update a field

 

OR (if I am looking to create a new related list item in the child object)

 

5.  Create a new related child object  pre-setting the field values.

 

I'm sure if I had something more then a very redimentary understanding of APEX I could probably figure this out, I thank you in advance for helping by providing me (or pointing me to) relevant examples that I can adapt to meet my needs.

Best Answer chosen by Admin (Salesforce Developers) 
ericszulcericszulc

Here's a basic template for that. Of course, it will need to be adapted to your own use.

 

trigger customObjectTrigger on customObject1 (after insert, after update)

{

  //A set of ids from the trigger object

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

 

  //List of custom object 2 queried

  List<CustomObject2> customObject2_list = new List<CustomObject2>();

 

//List holding what to insert or update

  List<CustomObject2> customObject2_toUpdate_list = new List<CustomObject2>();

  List<CustomObject2> customObject2_toInsert_list = new List<CustomObject2>();

 

//Run through the trigger records to found appropriate ids

  for(customObject1 c_i : Trigger.new)

  {

   if(c_i.field=='some value')

   {

    customObject1Id_set.add(c_i.Id);

   }

  }

 

//Use the previous ids to fetch matching records from custom object 2

  customObject2_list = [

   SELECT

    fields

   FROM

    CustomObject2

   WHERE 

    Id IN :customObject1Id_set

  ];

 

//Run through custom object 1 records,

  //for each run through custom object 2 for a match

  //We are doing this in case there is not a match and we need to create a new custom object 2

  for(customObject1 c1_i : Trigger.new)

  {

   Boolean found = false;

   for(customObject2 c2_i : customObject2_list)

   {

    if(c2_i.ParentObjectId == c1_i.Id)

    {

     //do something

   customObject2_toUpdate_list.add(c2_i);

found = true;

}

}

 

//We didn't find a corresponding custom object 2, let's create a new one

if(!found)

{

customObject2 c = new customObject2();

//do something

customObject2_toInsert_list.add(c);

}

}

 

//Insert or update

List<Database.Saveresult> customObject2_toUpdate_list_sr = Database.Update(customObject2_toUpdate_list);

List<Database.Saveresult> customObject2_toInsert_list_sr = Database.Insert(customObject2_toInsert_list);

}