You need to sign in to do that
Don't have an account?
mollydog
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
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.
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
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:
That should give you an idea of what you need to do.
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
It should be something like:
That should take care of that error at least.
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.
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.
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.