1

I have 2 triggers into sandbox that work s perfectly and I want to transfer it into production. The only problem is that I need to write a test class for each one in order to cover 75% of the triggers in order to full fill this. My triggers are

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 the other here

​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;
}

Any ideas would be helpful. Spare me please as I am really new into programming and have no other background into this.

Please also if you any good books or websites in order to start learning a few things over this would be helpful as well. Thank you in advance

update: I have tried this

    @isTest
public class Test3{
    public static testmethod void InsertPriceBookTrigger_Test1()
    {

        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);
    }
}

but it seems to be failing.

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

update 2:

   @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);
        }


    }

Hey Chiz, done that as well but still getting the same error. I assume that the problem is something with the code?!

update 3:

I have figured a few mistakes on the test class. first method for the orders is passed but the second for the task trigger I need to insert an order product but cannot seem to get it to work. 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.: []

Any suggestions?? My code is below :

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);

And the error Error: Compile Error: Invalid type: OrderLineItem at line 33 column 14

  • 1
    Can you please share with us what have you came up so far? Any test class code which doesn't cover at least 1% of your triggers? – Andrii Muzychuk Mar 2 '15 at 16:35
  • developer.salesforce.com/page/… - An Introduction to Apex Code Test Methods – Andrii Muzychuk Mar 2 '15 at 16:37
  • Hi Chiz please review my class (update to the question) where it keeps failing – Tommy Georgiou Mar 3 '15 at 10:11
  • I assume, Order object requires Account Name field to be filled in. I assume it's a must field for Order object. – Andrii Muzychuk Mar 3 '15 at 21:46
  • done that as well but still getting the same error. I assume that the problem is something with the code?! – Tommy Georgiou Mar 5 '15 at 7:34
1

Ummmm... Just make one test that inserts an Order, and another that inserts a Task?

Your triggers are both very simple, so just run them and make a basic assertion.

| improve this answer | |
  • Hi Renegade and thank you for your reply. The problem is that I don't even know the how-to for a single test class like in this case – Tommy Georgiou Mar 2 '15 at 14:58
1

Test Classes are a lot like Testing Scripts, you need to document what are the (minimum) steps and process scenario's required in order to complete the process that would fire your triggers and classes.

This is a completely made up test class but you can use this as a simple example to start from and get a feel for how it works. Remember this test data is not committed to the database so you have to create or query everything needed to actually test this:

@istest
public class Order_TestClass {
    static testMethod void validate_Order() {
        Account a = new Account(
            Name = 'Test Co',
            Field_Name__c = 'Value'
        );
        insert o;
        Order o = new Order(
            AccountId = a.id,
            Field_Name__c = 'Value'
        );
        insert o;

        OrderLineItem oli = new Order(
            OrderId = o.id,
            Field_Name__c = 'Value'
        );
        insert o;


        o.Status = 'Submit';
        update o;    
    }
}
| improve this answer | |

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.