2

This has already been asked and answered and is working except for the errorCase.SuppliedName returns Null after my error message. How do I write a Test Class for this. I'm a newbie and this has helped me tremendously.

trigger PreventCloseServiceRequestTrigger on Case (after update) {

// Find all service request that are being closed
Set<Id> newlyClosedCaseIds = new Set<Id>();
for (Id caseId : Trigger.newMap.keySet()) {
    if (Trigger.newMap.get(caseId).IsClosed && 
        !Trigger.oldMap.get(caseId).IsClosed) {
        newlyClosedCaseIds.add(caseId);
    }
}

// For every newly closed service request that has at least one 
// not closed task add an error to the Trigger.newMap entry.
// Adding the error to the Trigger.new entry will prevent the save
// and add the error message to the page messages that get displayed on
// the page.
for (AggregateResult aggResult : [
        Select Count(Id), WhatId
        From Task
        Where WhatId In :newlyClosedCaseIds
              And IsClosed = false
        Group by WhatId
        Having Count(Id) > 0
]) {
    Id caseId = (Id) aggResult.get('WhatId');
    Case errorCase = Trigger.newMap.get(caseId);

    // add my error message to the coordinator goes down here
    errorCase.addError('Please complete all OPEN TASK before trying to CLOSE this SERVICE REQUEST: ' +
                       errorCase.SuppliedName); 
    }
 }
1
  • Oh, okay, I understand. That actually helps. I am on my way out but when I get home I have the entire weekend to try this out. That will help me with understanding how to write Test Code b/c you're right if someone does it then it defeats the purpose. Thank you
    – Troys Girl
    Commented Oct 25, 2013 at 20:00

2 Answers 2

5

Troys Girl, welcome to SFSE.

The reason that no one is directly answering your question in regards of how to write the test code for this trigger, is simply because you have not attempted to try, or at least you aren't showing that you have. If I am wrong, please post your code of what you have tried so far and people will help you where you are stuck. This site is full of great knowledgable people that are more than willing to help, but aren't going to just do your work for you. The more evidence you show of what you have tried, and where you are stuck, the more likely you are to get people helping you.

I won't write your test class for you but I will try to help you understand writing unit tests. It's tricky at first, but once you master it, you'll see not only how easy it can be, but also how useful in your development.

Create your data

Let's say your class is a controller for a VF page that lists myObject in a pageBlockTable, and allows you to do 'something' with these records. In order to test the class, you need to have some myObject__c records to work with.

myObject__c o1 = new myObject__c(name = 'test1', .......);
insert o1;
myObject__c o2 = new myObject__c(name = 'test2', .......);
insert o2;

Instantiate the class

In order to use this class and test the methods, you need to instantiate the class, that is fairly simple

myClass controller = new myClass();

Test the logic within that class

Lets assume you allow a user to remove one of the myObject__c records on the VF page from the list with a myCustomDelete method. Lets also assume you hold the list of myObjects in a controller property myObjectList.

integer listSize = controller.myObjectList.size(); 
controller.myCustomDelete();
system.assertEquals(controller.myObjectList.size(), listSize - 1);

Here are some great references to look at that you can look even deeper into writing test code.

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

*This is one of my favorite articles on the subject, its excellent* http://wiki.developerforce.com/page/Apex_Code_Best_Practices

By not writing the code for you, people really are just trying to help you in the long run

Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime...

4
  • ok, I'm starting on this today since I need it for work and I need to understand it - so I'm taking it one step at a time. This isn't must but it's what I have so far.
    – Troys Girl
    Commented Oct 28, 2013 at 14:45
  • @is Test private class PreventCloseServiceRequestTrigger { static testMethod void testPreventCloseServiceRequestTrigger() { // this is where I will start my test Test.startTest();
    – Troys Girl
    Commented Oct 28, 2013 at 15:07
  • No worries, don't worry about it being right. Just post what you have and we can help. Thats what we are here for. Commented Oct 28, 2013 at 15:08
  • You should edit your original question to include your testing code, so its formatted and everyone can easily see it. Just use the Edit link under the question Commented Oct 28, 2013 at 15:08
1

When you add an error using the addError() method, everything that happened in your transaction prior to adding the error is rolled back, including the creation of errorCase.

3
  • Thank you but what would my Test Class look like?
    – Troys Girl
    Commented Oct 25, 2013 at 18:27
  • Using the addError method prohibits you from surfacing any data from memory in apex in that error message from that transaction.
    – greenstork
    Commented Oct 25, 2013 at 18:29
  • @TroysGirl Just verify your error message contains the string you have mentioned as error message . Commented Oct 25, 2013 at 18:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .