You need to sign in to do that
Don't have an account?
Sunil 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;
}
}
}
}
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;
}
}
}
}
and use this code
All Answers
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
and use this code