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
albonillalbonill 

Need Test for a Trigger

Hello,

 

Excuse my ignorance, but I am not familiar on how to go about testing the trigger below.  The trigger is basically desinged to avoid an Opportunity from being deleted if there is an Invoice, Sales Order, Agreement or Attachment.  Many thanks for the assistance.

 

Trigger:

 

trigger OppDeleteCheck on Opportunity (before delete) 
{
    Set<Id> stId = new Set<Id>();
    List<echosign_dev1__SIGN_Agreement__c> lstRe =[Select Id, echosign_dev1__Opportunity__c from echosign_dev1__SIGN_Agreement__c where echosign_dev1__Opportunity__c in : Trigger.oldMap.KeySet() ];
    for(echosign_dev1__SIGN_Agreement__c objp : lstRe)
    {
        stId.add(objp.echosign_dev1__Opportunity__c);
    }

    Set<Id> stIdS = new Set<Id>();
    List<Sales_Order__c> lstReS =[Select Id, Opportunity__c from Sales_Order__c where opportunity__c in : Trigger.oldMap.KeySet() ];
    for(Sales_Order__c objp : lstReS)
    {
        stIdS.add(objp.opportunity__c);
    }

    Set<Id> stIdSI = new Set<Id>();
    List<Sales_Invoice__c> lstReI =[Select Id, Opportunity__c from Sales_Invoice__c where opportunity__c in : Trigger.oldMap.KeySet() ];
    for(Sales_Invoice__c objp : lstReI)
    {
        stIdSI.add(objp.opportunity__c);
    }
    
    Set<Id> stIdAtt = new Set<Id>();
    List<Attachment> lstAtt =[Select Id, ParentId from Attachment where ParentId in : Trigger.oldMap.KeySet() ];
    for(Attachment objp : lstAtt)
    {
        stIdSI.add(objp.ParentId);
    }
  
    for(Opportunity objA : Trigger.old)
    {
        if(stId.contains(objA.Id))
        {
           objA.addError('WARNING!!! This Opportunity contains an Agreement and cannot be deleted.');
           return;
        }

    if(stIdS.contains(objA.Id))
        {
           objA.addError('WARNING!!! This Opportunity contains a Sales Order and cannot be deleted.');
           return;
        }

    if(stIdSI.contains(objA.Id))
        {
           objA.addError('WARNING!!! This Opportunity contains an Invoice and cannot be deleted');
           return;
        }

    if(stIdAtt.contains(objA.Id))
        {
           objA.addError('WARNING!!! This Opportunity contains an Attachment and cannot be deleted');
           return;
        }

    }
}

 Regards,

Alex

Navatar_DbSupNavatar_DbSup

Hi,

 

In order to test you trigger, you first need to create an opportunity with the Invoice, Sales Order, Agreement or Attachment. Now try to Delete the same if it shows the Warning message supplied by you during deletion then its working fine else these is some issues in coding.

 

To facilitate the development of robust, error-free code, Apex Code requires the creation and execution of unit tests. Unit tests are comprised of test methods and classes that verify whether a particular piece of code is working properly.

 

 Unit tests are class methods that verify whether a particular piece of code is working properly. Unit tests are written in Apex Code and annotated with the testMethod keyword.

 

For more detail follow below link:

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

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

albonillalbonill

Hello Navatar_DbSup,

 

I read the material you suggested and I am learning slowly but surely.  I am a novice to Apex, so I had to get help with the trigger as well.  I have started to create the test, but I am stuck on how to call the trigger I want to test and the assert functions.  I hope you get a good chuckle out of my code below:

 

@isTest
public with sharing class TestOppDeleteCheck {
        static testMethod void runPosTestcases () {
   
/* This setup method will create an opportunity with line items. */
    
        /* Create an account */
        Account a = new Account();
        a.Name = 'TEST';
        Database.insert(a);  

        /* Setup a basic opportunity */
        Opportunity o = new Opportunity();
        o.OwnerID = a.OwnerId;
        o.AccountId = a.id;
        o.StageName ='Negotiation';
        o.Name = 'TestOp';
        o.closeDate = Date.today();

        /* Create the opportunity */
        Database.insert(o);
        
        /* Create an Sales Order */
        Sales_Order__c so = new Sales_Order__c ();
        so.OwnerId = o.OwnerId;
        so.Name = 'USD - U.S. Dollar';
        Database.insert(so);

        try {
   delete o;
   // should throw exception, so fail test if get this far
   System.assert(true, 'WARNING!!! This Opportunity contains a Sales Order and cannot be deleted.');
            }
            
catch (Exception e){
   // expected exception
            }

    }
}

 Any assistance would be greatly appeciated.

 

Regards,

Alex