function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Patrick Maxwell AppfolioPatrick Maxwell Appfolio 

APEX code works great...until I try running a test

Hello Salesforce Coders,

I have a problem, and it's not giving me warm fuzzies inside.  Quick disclaimer, I'm not experienced at coding, but I'm learning, and I'm very interested in it.  I have written some APEX that will update a field on one SObject based on a field being updated on another SObject.  I've bulkified it, and it works great in our sandbox.  Even when I dataload 200+ records, it's great, I'm happy.  However, now that I've gotten to the step to write test code, I am confounded by my predicament.  When I run the code via test the field that needs to be updated (after the update of the other field) will not update, so my final assertion is wrong and my test code fails.  I can get more detailed, but I figured I would keep things vague for now, and if anyone is interested, they can respond to this post and throw down some knowledge.

Thanks for your time!
Patrick
Best Answer chosen by Patrick Maxwell Appfolio
Ramu_SFDCRamu_SFDC
Hi Patrick,

Testing is tricky, if you are doing the test on sandbox you need to make sure that you insert every record that is in the relationship until we get to the object we are working on. So for example if we have created a trigger on case and we need to write a unit test in that case we begin with creating an account record and then a contact record and then a case record. Once we get the case record we do wat ever manipulations we have to and then insert/update it.

Later for checking the assertion, you need to create another case object and do a SOQL to get all the case information and then do an assert.

Long story short, create all the records from the top most parent object to the current object and do testing.

All Answers

Sonam_SFDCSonam_SFDC
Hey Patrick, the two fields you are trying to update - are both of them updated through the same trigger?

Would help you could share the code you've written so the members can understand the issue better and suggest accordingly.


Ramu_SFDCRamu_SFDC
Hi Patrick,

Testing is tricky, if you are doing the test on sandbox you need to make sure that you insert every record that is in the relationship until we get to the object we are working on. So for example if we have created a trigger on case and we need to write a unit test in that case we begin with creating an account record and then a contact record and then a case record. Once we get the case record we do wat ever manipulations we have to and then insert/update it.

Later for checking the assertion, you need to create another case object and do a SOQL to get all the case information and then do an assert.

Long story short, create all the records from the top most parent object to the current object and do testing.
This was selected as the best answer
Patrick Maxwell AppfolioPatrick Maxwell Appfolio
Thank you for your help.  I don't know how to exactly explain what the problem was, but what was happening was when I tested the assert against the objects value directly, it was not working.  However, when I tested against a SOQL query of the same object, with the same ID, it was updated.