+ Start a Discussion
Lil_Ranger1111Lil_Ranger1111 

Required Field Missing Test Class?

I created a trigger to prevent the deletion of a parent record if a child record exists. I'm having problems with the test class. It says required field missing "Dept_Code__c". I have it in the test class, so I'm not sure what I'm missing.
Parent Object - Order_Request__c (Quantity, Cost Center (lookup), Order Type (lookup) are required fields.

Child Object - Order_Assignment__c (Order Request, Order Center (lookup) are required fields)

Any help is greatly appreciated.

trigger PreventOrderDeletion on Order_Request__c (before delete) {


    for(Order_Request__c lq : [Select Id, Name, (Select Id from Order_Assignments__r) from Order_Request__c where Id IN :trigger.oldMap.KeySet()]){

            if(lq.Order_Assignments__r != null && lq.Order_Assignments__r.size() > 0){

              lq.addError('The order request cannot be deleted because there is an associated order assignment record');

            }
     } 



@IsTest(SeeAllData=true)
public class PreventOrderDeletionTest{

static testMethod void PreventDeleteTest()
{
    //Create Parent Object
    Order_Request__c lic = new Order_Request__c(Name='Salesforce Testing');
    lic.Quantity__c = Decimal.valueOf('1.0');
    lic.Order_Type__c = 'a6xxxxxxxxxxxx';
    lic.Dep_Code__c = 'a6Oxxxxxxxxxxx';
    insert lic;

    //Start Test & create child object
    test.startTest();

    Order_Assignment__c asgn = new Order_Assignment__c();


    //Relate Child to the Parent from the inserted record above
    asgn.Order_Request__c = lic.Id;
    asgn.Dep_Code__c = lic.Default_Code__c;

    //Insert Child Record
    insert asgn;

    try {
    delete lic;
        } catch (Exception e) {
           system.assertEquals('You cannot delete this record!', e.getMessage());
          }
   //Stop Test
   test.stopTest();
}  
}


Ana Catarina BritesAna Catarina Brites
Hello Lil_Ranger, 

Where does the values for the lic.Default_Code__c comes from? 
Can you check to see if that value is not empty? 

Regards, 
Ana Catarina Brites
Karamala DamodarKaramala Damodar
see first u need to insert what ever topmost master i.e u said Cost Center (lookup), Order Type (lookup) u must include those fields also.
avoid hardcoding k
i.e entering id values

Lil_Ranger1111Lil_Ranger1111
Hi,

I had an error in my posting.  The parent record should have (Quantity__c, Dept_code__c (lookup), and order_type__c(lookup) which are all required on the parent object creation.

How do I avoid using IDs, but populate those required lookup values?

Thank you.
Lil_Ranger1111Lil_Ranger1111
Would it just be something like this?

lic.Dept_Code__c = lic.Dept_Code__c;


Karamala DamodarKaramala Damodar
exactly but in that also what ever field is required that also u must include k
Lil_Ranger1111Lil_Ranger1111
@IsTest
public class PreventOrderDeletionTest{

    static testMethod void PreventDeleteTest()
    {
        //Create Parent Object
        
        Order_Type__c typ = new Order_Type__c(Name='Testing');
        typ.Unit_Price__c = Decimal.valueOf('1.00');
        typ.Period__c = 'Monthly';
        
        insert typ;
        
        Dept_Code__c cost = new Dept_Code__c(Name='Dept Code Test');
        cost.Default_Code__c = '123456';
        insert cost;
            
        Order_Request__c lic = new Order_Request__c();
        lic.Name = 'Salesforce Test';
        lic.Quantity__c = Decimal.valueOf('1.0');
        lic.Order_Type__c = typ.Id;
        lic.Dept_Code__c = cost.Id;
        insert lic;
        
        //Start Test & create child object
        test.startTest();
        
        Order_Assignment__c asgn = new Order_Assignment__c();
        
        
        //Relate Child to the Parent from the inserted record above
        asgn.Order_Request__c = lic.Id;
        asgn.Dept_Code__c = cost.Default_Code__c;
        
        //Insert Child Record
        insert asgn;
              
        try {
        delete lic;
            } catch (Exception e) {
               system.assertEquals('You cannot delete this record!', e.getMessage());
              }
       //Stop Test
       test.stopTest();
    }  
}

I did some updates and recieved 100% code coverage.  

When I tested the code in the developer console, I received this error:  (I'm assuming this is expected)

System.AssertException: Assertion Failed: Expected: You cannot delete this record!, Actual: Delete failed. First exception on row 0 with id a6Q110000008OLyEAM; first error: DELETE_FAILED, Your attempt to delete Testing could not be completed because it is associated with the following order assignments.: LA-000026
Karamala DamodarKaramala Damodar
can send ur code to my mail id i.e damodar.karamala@gmail.com