Skip to main content
I am new to coding. Could you please suggest how to write a test class for the given trigger. Appreciated your time.

trigger Triggerupdatedirectbill on FX5__Ticket_Item__c (before insert, before update ) {

    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate) ) {

        List<FX5__Ticket_Item__c> items = Trigger.new;

        Set<String> accountsIds = new Set<String>();

        Set<String> wellsIds = new Set<String>();

        for(FX5__Ticket_Item__c item:items) {

            if(item.Customerforlogiconly__c!= null) {

               accountsIds.add(item.Customerforlogiconly__c);

               System.debug(accountsIds);

            }

            if(item.Disposal_Location_on_Ticket_item__c!= null) {

                wellsIds.add(item.Disposal_Location_on_Ticket_item__c);

                 System.debug(wellsIds);

            }

        }

        Map<String, Obj_Direct_Bill_Customer__c> directInvoicesMap = new Map<String, Obj_Direct_Bill_Customer__c>();

        for(Obj_Direct_Bill_Customer__c di :[Select Direct_Bill_Customer__c, Disposal_Location__c from Obj_Direct_Bill_Customer__c where Direct_Bill_Customer__c in :accountsIds and Disposal_Location__c in :wellsIds] ) {

            if(!directInvoicesMap.containsKey(di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c) ) {

                directInvoicesMap.put(di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c,di);

            }

            System.debug (di.direct_Bill_Customer__c);

            System.debug (di.Disposal_Location__c);

            System.debug (di.Direct_Bill_Customer__c+'_'+di.Disposal_Location__c);          

        }

        for(FX5__Ticket_Item__c item:items )       

         {

         System.debug (item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c);

           if(directInvoicesMap.containsKey(item.Customerforlogiconly__c+'_'+item.Disposal_Location_on_Ticket_item__c)) {

                item.Direct_Bill_Customer__c = True;

                  }

            else {

                item.Direct_Bill_Customer__c = False;

            }

                

        }

        

    }

}
4 answers
  1. Jan 10, 2019, 4:16 AM
    Add all required fields .. Please remove Customerforlogiconly__c from the test class becasue you no need to setup formula fields

    and use this code

     

    @isTest

    private class FX5TicketItemTestClass {

    @isTest static void testCaseOne() {

    Account acct = new Account(Name='Test Account');

    insert acct;

    Disposal_Location__c loc = new Disposal_Location__c ();

    loc.Name ='USA';

    insert loc ;

    Obj_Direct_Bill_Customer__c cust = new Obj_Direct_Bill_Customer__c () ;

    cust.Direct_Bill_Customer__c = acct.Id ;

    cust.Disposal_Location__c =loc.Id ;

    insert cust ;

    FX5__Price_Book_Item__c pb = new FX5__Price_Book_Item__c ();

    // add all fields

    bp.Name='Test';

    insert pb;

    FX5__Ticket__c tM = new FX5__Ticket__c ()

    tM.Name='Test';

    // add all required fields ;

    insert tM ;

    FX5__Ticket_Item__c t = new FX5__Ticket_Item__c ();

    //t.Customerforlogiconly__c = acct.Id ;

    // Add All required fields

    t.Disposal_Location_on_Ticket_item__c=loc.Id ;

    insert t ;

    }

    }

     
0/9000