+ Start a Discussion
mollydogmollydog 

Apex Trigger To Update A Custom Field

Hello I am new to Apex / Trigger and would like to use it to update one field from another on a custom object. I know this can be done via workflow but would like to try this with  a trigger so I can start to understand more about Apex. I am in the mids of reading about apex etc but would be grateful if someone could give me a few examples.

 

What I am trying to achieve:

 

Object name = Test object

 

Field 1 = A

 

Field 2 = B

 

All I want to do is make "field A" update with "field B" value after update.

 

Thank you

Jake GmerekJake Gmerek
Trigger updateFields on Test_Object__c (before update){
  for (Test_Object__c obj: trigger.new){
    field_1__c = field_2__c;
  }
}

 That should do the trick, you want to do this on a before update trigger to avoid an infinite loop which the system will not allow.  All this does is when an update is called, but before the database is actually updated, we cycle through each object updated and make field 1 = field 2.  Then the object(s) are updated and the value(s) that we want are stored in the database.

 

Hope this helps you out.

mollydogmollydog

Hello Jake, Thank you very much for your fast reply. This has made it very clear and I have tested in my sandbox and all's good. I have also been trying to get the below to work which is like before but acorss to objects.

 

I have two custom objects (Object 1 & Object 2) when object 1 is edited in anyway, I want  field 1 to be updated with field 2 on object 2.

 

Any help on this woudl be very much appreciated and once again thank you for your help.

 

Cheers

 

 

 

 

Jake GmerekJake Gmerek

OK, this one is a little more complicated for two reasons: first we need to "bulkify" the code, which means that we have to assume that multiple records are being inserted/updated all at once and we have to code accordingly, and second we have to have some way of identifing which records in object2  need to be updated.  This is usually done by creating a relationship between the two records, so for our example we will assume that object 2 has a lookup field on object 1 called Object_1__c.  Here goes:

 

trigger updateSecondObject on Object_1__c(before insert, before update){
  //get a list of all the object 2 ID's contained in the records 
  //to be updated.
  List<ID> obj2IDs = new List<ID>;
  for (Object_1__c obj: trigger.new){
   obj2IDs.add(obj.Object_2__c);
  }
  //now get a list of all the records for object 2 that contain the 
  //above IDs
  List<object_2__c> obj2s = new List<object_2__c>(select id, field_to_update__c from Object_2__c where id in: obj2IDs]);
  //now loop again for all the records being updated and then for each
  //one loop through all the object 2 records retrieved above.
  for (Object_1__c obj: 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 < obj2s.size(); i++){
      //now we make sure the record IDs match
      if (obj.Object_2__c == obj2s[i].id){
        obj2s[i].field_to_update__c = some value or field from obj;
      }
    }
  }
  //update all the object 2 records
  update obj2s;
}

 That should give you an idea of what you need to do.

mollydogmollydog

Thanks again Jake, I have had a play with what you sent me and uderstand most of it / what it is doing. I am however getting an error: "Compile Error: Variable does not exist: Program_Block__c at line 6 column 15".

 

I am not sure why as yet as am still learning but will keep playing with it. I have put my code below so if you do have time to look at it I would be very greatful but not to worry if not.

 

Once again thank you for your help as I am really enjoying learning what can be done with SF and it is good to know people will help the "Newbie"

 

Cheers

 

trigger updateActualStartDate on Item__c(before insert, before update){
  //get a list of all the program block ID's contained in the records 
  //to be updated.
  List<Program_Block__c> ProIDs = new List<Program_Block__c>();
  for (Item__c Ite:trigger.new){
   ProIDs.add(Program_Block__c);
  }
  //now get a list of all the records for program block that contain the 
  //above IDs
  List<Program_Block__c > Pros = new List<Program_Block__c >([select id, Actual_Start_Date__c from Program_Block__c 
  where id in:ProIDs]);
  //now loop again for all the records being updated and then for each
  //one loop through all the program block records retrieved above.
  for (Item__c obj: 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 < Pros.size(); i++){
      //now we make sure the record IDs match
      if (Pros.Program_Block__c == pros[i].id){
        Pros[i].Actual_Start_Date__c = Actual_Start_Date_Hidden__c;
      }
    }
  }
  //update all the program block records
  update Pros;
}

 

Jake GmerekJake Gmerek

It should be something like:

 

ProIDs.add(Ite.Program_Block__c);

 That should take care of that error at least.

sachin24sachin24

Hi,

Can any one tell me that suppose we want to update the field of object A with that of object B then how we can achieve this.

Also, both the Object A and Object B are independent to each other they doesn't have any kind of relationship in between them.

sachin24sachin24

Hi,

Can any one tell me that suppose we want to update the field of object A with that of object B then how we can achieve this.

Also, both the Object A and Object B are independent to each other they doesn't have any kind of relationship in between in them.

Jake GmerekJake Gmerek
If there is no relationship between the objects then there is no way to determine what to update. There has to be some sort of relationship somewhere. Think of how you would identify the record if you were making the update manually and go from there.
Christina Moseley 11Christina Moseley 11
Jake, do you think you could help me write a trigger as well?
Jaswanth Reddy 6Jaswanth Reddy 6
Hi Jake,
The above example you given to update the object_1  based on the object_2 is After Update and After Inserted know.
Please correct me if I am wrong.
Thank you for your time.