0

I'm trying to learn about apex test classes, I have attached the below code. I can't execute this in production developer console, but works fine on my sandbox. I was asked to write a @isTest, is this really necessary? can't i just push the apex code to production through a change set and just execute it? Doing so will i mess up any thing in production? This is a one time thing, I have to update a certain checkbox in all the entries. Please advice, please do not down vote, if you find this question useless, let me know in advance and I will take it down. thank you.

public class SecondAttemptTrigger {
    public void TriggerMethod(){
        List<Exam__c> objects = [
            SELECT Name, Exam_state__c, doNothingExam__c
            FROM Exam__c
            WHERE Exam_state__c = 'Active' OR Exam_state__c ='Failed'];

        for(Exam__c e : objects){
            if(e.Exam_state__c == 'Active' || e.Exam_state__c == 'Failed') {  
                e.doNothingExam__c = True;
            } 
        }
        update objects;
    }
}
2

I was asked to write a @isTest, is this really necessary?

Yes.

can't i just push the apex code to production through a change set and just execute it?

Not without code coverage.

Doing so will i mess up any thing in production?

It can potentially cause problems. This is the purpose of a unit test: to make sure that your changes will not harm production.

I'm not sure I can emphasize this enough, but:

⚠️ DO NOT DEPLOY UNTESTED CODE TO PRODUCTION ⚠️

This is incredibly important. You can bring your entire business to your knees with a single line of code. Do you want to be that person? No, I didn't think so.

This is a one time thing, I have to update a certain checkbox in all the entries.

This may be your saving grace. You don't need this code in production, so you could just run it directly in production. Take out the the class and method, and you end up with:

List<Exam__c> objects = [
    SELECT Id
    FROM Exam__c
    WHERE Exam_state__c in ( 'Active','Failed' )];

for(Exam__c e : objects){
    e.doNothingExam__c = True;
}
update objects;

To execute this, click on "Your Name" > Developer Console > Debug > Open Execute Anonymous Window, put in the code, and click the Execute button.

Note that if you have more than 10,000 records, this code will fail, so you may need to do additional work.

  • I happen to have 1738 entries, but im getting this error <Line: 7, Column: 7 Variable does not exist: doNothingExam__c> List<Exam__c> objects = [ SELECT Name FROM Exam__c WHERE Exam_state__c in ( 'Active','Failed' )]; for(Exam__c e : objects){ e.doNothingExam__c = True; } update objects; – Niveth Kumar Nov 27 '18 at 8:24
  • i clearly have the variable doNothingExam__c. Why is this a problem? – Niveth Kumar Nov 27 '18 at 8:33
  • I Solved this, the checkbox was hidden, and not visible. that was the reason – Niveth Kumar Nov 27 '18 at 9:56
1

Yes, test classes are not only needed before sending your classes to production but they are mandatory and a serious part of APEX development.

Here you can find information on how to start building your Test Classes.

  • Thank you for your comment. I'm new to APEX, and I already tried going through this link. But can't seem to understand, how I should create one. In my case, do I have to create an Exam object and adding certain values etc, something like that? – Niveth Kumar Nov 27 '18 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.