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
Tommy GeorgiouTommy Georgiou 

Class for trigger to go into production

Hello,


I have 2 triggera 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 fullfill 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;
  }
}
 
​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 helpfull. Is there any generic way to do this? Any manuals or anything to study through in order to achieve this (and for future purposes as well)
Vishal NegandhiVishal Negandhi
Hi Tommy, 

The best way to start learning/knowing about test coverages is here:
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

Once you go through this, you can try and create test classes for your triggers. And if something acts as a blocker, let us know here.
Good luck!
David ZhuDavid Zhu
For your reference:

@isTest
public class My_Test{
    public static testmethod void InsertPriceBookTrigger_Test1()
    {
        Contract c = new Contract(name ='Contract1',status='activated',startdate=system.today(),ContractTerm=12);
        insert c;

        Order o = new Order(name='Test1',EffectiveDate=system.today(),Contract=c,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()
    {
            Contract c = new Contract(name ='Contract1',status='activated',startdate=system.today(),ContractTerm=12);
            insert c;

            Order o = new Order(name='Test1',EffectiveDate=system.today(),Contract=c,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);
    }


}
 
Tommy GeorgiouTommy Georgiou
Hi Vishal & David,

Thank you both for your replies.

David as I am really new to this would you please be kind and explain me some basic stuff of the class that you wrote. Thank you in advance
Tommy GeorgiouTommy Georgiou
Hi David,

I have inserted the suggested class but the result is 0/2 passed
User-added image

I don't know if it helps but after reviewing the class I do not have any contracts active since we are using orders without any contracts. I then changed the class into 
@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);
    }


}
and still failing


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