0

I've written an Apex Trigger that deletes a related object when an Opportunity is deleted but I cannot get the test class correct as to be able to pass validation and move to production.

Apex Trigger:

trigger deletePLScenario on Opportunity (before delete) {
    //To store parent ids

    list<id> OpportunityIds=new list<id>();

    for(Opportunity oppVar:trigger.old)
    {
        OpportunityIds.add(oppVar.id);
    } 
    //Collecting all child records related to Parent records
    list<PLNDR__PrecisionLender_Scenario__c> listOfPLS=[select id from 
                                                        PLNDR__PrecisionLender_Scenario__c where PLNDR__Opportunity__c in 
                                                        :OpportunityIds];

    system.debug('listOfPLS'+listOfPLS);
    //deleting child records

    delete listOfPLS;
}

Apex Test Class:

@isTest
private class testPlsDelete 
{    
    static testMethod void testDel() 
    {
        Opportunity testOpp = new Opportunity ();

        insert testOpp;

        list<Opportunity> Opps = new list<Opportunity>();
        for (Integer i = 0; i < 10; i++){
            Opportunity o = new Opportunity();
            o.Name             = 'Testing' + i;
            o.StageName        = 'Qualification';
            o.Amount           = i * 1000000;
            o.CloseDate        = System.today() + 30;
            o.Type             = 'New Business';

        }
        insert Opps;
        update Opps;

        list<PLNDR__PrecisionLender_Scenario__c> testPls  = new list<PLNDR__PrecisionLender_Scenario__c> ();     
        for (Integer i = 0; i < 10; i++){
            PLNDR__PrecisionLender_Scenario__c pl = new PLNDR__PrecisionLender_Scenario__c();
            insert testPls;
            update testPls;

            Test.startTest();
            Test.stopTest();
        }
    }
}
3
  • 2
    Please edit your post to be more specific about what is not working. We are not mind readers. If you have an error message (or multiple), please include the message(s) verbatim. – Adrian Larson Jan 16 '18 at 14:40
  • 1
    Your test method doesn't delete anything. How is it going to cover a before delete trigger? – David Reed Jan 16 '18 at 14:43
  • testmethods without asserts aren't really test methods – cropredy Jan 16 '18 at 18:10
2

Looks like your problem is in this block of code:

        list<PLNDR__PrecisionLender_Scenario__c> testPls  = new list<PLNDR__PrecisionLender_Scenario__c> ();   

        for (Integer i = 0; i < 10; i++){
            PLNDR__PrecisionLender_Scenario__c pl = new PLNDR__PrecisionLender_Scenario__c();
            insert testPls;
            update testPls;

            Test.startTest();
            Test.stopTest();
        }

Where you:

  1. Create an empty list
  2. Iterate over some random numbers
  3. Create a blank object
  4. Insert this object (Without setting any primary keys, or relating it to your other test data)
  5. Start & end your test

It's kind of clear you don't really know what you're doing. I'd suggest going over the trailheads Salesforce created for new developers & admins. In addition to that, going over your code with a pencil and making notes on each step, can really help you understand code better. Once you have some concrete steps, you can turn that into a test class with a little bit of work.

Lets change your test. Lets try these steps instead:

  1. Create an empty list
  2. Iterate over your project list
  3. Create a new record, relate it to your test data from your loop, using a primary key PLNDR__Opportunity__c (without the key to relate the records, nothing will be returned by your query)
  4. insert your list of test records
  5. assert that there is data under each opportunity.
  6. Start your test, delete your test opportunities, end your test.
  7. Run your assert from step #5 again, but this time, make sure that there are 0 child records left.

I could write this code for you, but I think you'll learn better writing it on your own. These are the steps I would take to write the class, written out in plaintext. If you have a question on one or more of these steps, let me know, and I can add examples.

If you run into trouble writing the code, ask a new question, with a clear issue, and relevant code.

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.