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
Sunil Kandel 10Sunil Kandel 10 

Test class for Trigger with Customer Object

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;
            }
                
        }
        
    }
}
Best Answer chosen by Sunil Kandel 10
Raj VakatiRaj Vakati
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 ; 
	
	
	
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
Try this code and add all required fields 
 
@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__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 ; 
	
	
	
    }
    
}

 
Sunil Kandel 10Sunil Kandel 10
Hi Raj,
Thank you for your prompt reply.
When I ran the code: it is showing me the following error: "Field is not writeable: FX5__Ticket_Item__c.Customerforlogiconly__c.
Customerforlogiconly is a formula field."

The required field in FX5__Ticket_Item__c are : Field: Fx5__ticket__c which is a lookup from object: FX5__Ticket__c and Field: FX5__Price_Book_Item__c which is a lookup from object: FX5__Price_Book_Item__c

(FX5__Ticket__c and FX5__Ticket_item__c has Master Detail relation and FX5__Ticket__c is a master)

Could you please suggest.
Thank you
 
Raj VakatiRaj Vakati
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 ; 
	
	
	
    }
    
}

 
This was selected as the best answer
Sunil Kandel 10Sunil Kandel 10
Thank you Raj