+ Start a Discussion
Tommy GeorgiouTommy Georgiou 

error on test class

Hello all,

I have these two trigger 
trigger InsertPriceBookTrigger on Order (before insert) { 
 List<Pricebook2> stdPBL =  [select id from Pricebook2 where IsStandard = TRUE];
 if(!stdPBL.isEmpty()){
  for(Order o: Trigger.new)
   o.PriceBook2Id = stdPBL[0].id;
  }
}

and 
 
​trigger TaskTrigger on Task (after insert) {
    List<Order> ordersToUpdate = new List<Order>();

    for(Task t : Trigger.new) {
        if(t.WhatId.getSObjectType() == Order.sObjectType 
            && t.Subject.startsWith('Email:')) {
                ordersToUpdate.add(new Order(Id = t.WhatId, Status = 'Sent'));
        }
    }

    update ordersToUpdate;
}

for these triggers created the following test class
 
@isTest
public class Test3{
    public static testmethod void InsertPriceBookTrigger_Test1()
    {
       Account a = new Account(
            Name = 'Test'

        );
        insert a;

        Order o = new Order(name='Test1',EffectiveDate=system.today(),status='draft');
        insert o;

    List<Pricebook2> stdPBL =  [select id from Pricebook2 where IsStandard = TRUE];

    if (!stdPBL.isEmpty())
    {
        system.assertequals(stdPBL[0].id,o.PriceBook2Id);
    }
    } 

    public static testmethod void TaskTrigger_Test1()
    {

            Order o = new Order(name='Test1',EffectiveDate=system.today(),status='draft');
            insert o;

        Task t = new Task(whatid=o.id,Priority = 'normal',status='open',subject='Email:xxxx');
        insert t;

        system.assertequals('Sent',o.status);
    }


}

It fails to cover the triggers. The error on the test history is :


System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Account Name:You must enter a value: []

Any ideas why?
SYED MOOSA NAZIR T NSYED MOOSA NAZIR T N
Hi Tommy,

In your test class, before inserting any test records, please cross check the Mandatory fields in that object. Then provide the values for those mandatory fields in the test record. This will help you to avoid this issue.

Regards
TN Syed Moosa Nazir TN
Tommy GeorgiouTommy Georgiou
Hi Syed,

But I have the account name inserted as the error defines. 
Rahul Sangwan7341Rahul Sangwan7341
Hi Tommy,

As i see you are using standard order object and in Order Account is mandatory field and the error is coming while you are insering the order. Please check this and let me know if still there is any issue

Choose this as Best Answer if it helps you to solve the issue.
SF AdminSF Admin
Try this:
Account a = [select id from account limit 1];
Order o = new Order(name='Test1', EffectiveDate=system.today(), AccountId=a.id, status='draft');
 insert o;
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
 
Tommy GeorgiouTommy Georgiou
Hi,

The problem is that I need to insert at least 1 product. I forgot to insert an account on the 2nd test class that is why it was failing. Now it's failing because I need at least 1 product in it
Tommy GeorgiouTommy Georgiou
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TaskTrigger: execution of AfterInsert

caused by: System.DmlException: Update failed. First exception on row 0 with id 801110000011RMSAA2; first error: FAILED_ACTIVATION, An order must have at least one product.: []

Trigger.TaskTrigger: line 11, column 1: []
Tommy GeorgiouTommy Georgiou
When I insert a Line Item I get this error. Sorry for my poor knowledghe by the way
 
public static testmethod void TaskTrigger_Test1()
    {
          Account a = new Account(
            Name = 'Test'
            
        );
        insert a;
          
            Order o = new Order(name='Test1',AccountId=a.id,EffectiveDate=system.today(),status='draft');
            insert o;
            
             OrderLineItem b = new Order(
            OrderId = o.id);
        insert b;

        Task t = new Task(whatid=o.id,Priority = 'normal',status='open',subject='Email:xxxx');
        insert t;
        
        system.assertequals('Sent',o.status);


Error: Compile Error: Invalid type: OrderLineItem at line 33 column 14