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
mdinatalemdinatale 

Newbie Help with test condition

Please help with test condition it passes in Develop->Apex Test Execution  but when i go to deploy it fails, Not even sure if im doing it right. Been working on this for days. Trigger is on custom object Debt_Info

 

Fails with

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []", Failure Stack Trace: "Class.Test_Dept_info_trigger.myTest: line 11, column 8 External entry point"

 

 

trigger Monthly_Payment on Debt_Info__c (after update, after insert) {

for(Debt_Info__c mPay : Trigger.new){
LIST<Debt_Info__c> i = [SELECT Name, Principal_Balance__c, Percent__c, Creditor_Table__c FROM Debt_Info__c WHERE Opportunity__c =: mPay.Opportunity__c];

decimal pm = 0.00;
for(Debt_Info__c dc: i){
if(dc.Principal_Balance__c != null){
pm += dc.Principal_Balance__c * (dc.Percent__c/100);
}
}

State_Fee_limit__c sf = [SELECT Fee__c FROM State_Fee_limit__c WHERE State__c = 'MI'];


decimal sfv = sf.Fee__c;

sfv = pm * sfv;
pm = pm + sfv;

opportunity o = [SELECT Monthly_Payment__c FROM opportunity WHERE Id =: mPay.Opportunity__c LIMIT 1];
o.Monthly_Payment__c = pm;
o.CCC_Fee__c = sfv;
update o;
}


}

 

 

 

public class Test_Dept_info_trigger {

    static testMethod void myTest() {
 
       
       // Create a Lead
        Lead l = new Lead();
            l.FirstName = 'Test';
            l.LastName = 'Lead';
            l.Company = 'Test Company';
            l.Email = 'leademail@example.com';
        insert l;
 
        // Create an Account
        Account a = new Account();
            a.Name = 'Test Account';
        insert a;
 
        // Create a Contact
        Contact c = new Contact();
            c.FirstName = 'Test';
            c.LastName = 'Contact';
            c.AccountId = a.Id;
            c.Email = 'contactemail@example.com';
        insert c;
 
        // Create Opportunities
         //list<Opportunity> l_Opps = new list<Opportunity>();
        Opportunity o = new Opportunity();                      
            o.AccountId = a.id;
            o.Name = 'Test Opportunity';
            o.CloseDate = date.today();
            o.StageName = 'Qualified';
            o.Sales_Monthly_Fee__c = 49;
            o.Description = 'Test Opportunity Description';
        //l_Opps.add(o);
 
      
         insert o;
         
        //insert l_Opps;
       
       
       Creditor_Table__c cr = [SELECT Name FROM Creditor_Table__c WHERE Name = 'CITI' LIMIT 1];
       
       Debt_Info__c tdebt = new Debt_Info__c(Creditor_Table__c=cr.Id,Opportunity__c=o.id,Principal_Balance__c=1000);
       
       insert tdebt;
    
    
    
     }   
}

Andy BoettcherAndy Boettcher

Best Practice:  ALWAYS create ALL data in a Unit Test, never rely on data that you assume exists.  That's why your UT is failing.

 

Near the end of your UT, you are querying the Creditor_Table__c  object.  Create a record in there in your UT for it to succeed.

 

-Andy

mdinatalemdinatale

That worked, ive added in after delete now my test condition won't work. heres my new code, i know its redundant but didn't know another way, thanks in advance. I tried adding in another debt info and deleting it to fire the delete part but its says not enough coverage what else do i need to do. Thanks

 

trigger Monthly_Payment on Debt_Info__c (after delete, after update, after insert) {
if(Trigger.isDelete){
for(Debt_Info__c mPay : Trigger.old){
LIST<Debt_Info__c> i = [SELECT Name, Principal_Balance__c, Percent__c, Creditor_Table__c FROM Debt_Info__c WHERE Opportunity__c =: mPay.Opportunity__c];

decimal pm = 0.00;
for(Debt_Info__c dc: i){
if(dc.Principal_Balance__c != null){
pm += dc.Principal_Balance__c * (dc.Percent__c/100);
}
}
opportunity o = [SELECT Monthly_Payment__c, AccountId FROM opportunity WHERE Id =: mPay.Opportunity__c LIMIT 1];
Account ac = [SELECT PersonMailingState FROM Account WHERE id =: o.AccountId];
LIST<State_Fee_limit__c> sf = [SELECT Fee__c FROM State_Fee_limit__c WHERE State__c =: ac.PersonMailingState];

if(sf.size() >=1){
decimal sfv = sf[0].Fee__c;

sfv = pm * sfv;
pm = pm + sfv;

o.Monthly_Payment__c = pm;
o.CCC_Fee__c = sfv;
update o;
}
}

}else{
for(Debt_Info__c mPay : Trigger.new){
LIST<Debt_Info__c> i = [SELECT Name, Principal_Balance__c, Percent__c, Creditor_Table__c FROM Debt_Info__c WHERE Opportunity__c =: mPay.Opportunity__c];

decimal pm = 0.00;
for(Debt_Info__c dc: i){
if(dc.Principal_Balance__c != null){
pm += dc.Principal_Balance__c * (dc.Percent__c/100);
}
}
opportunity o = [SELECT Monthly_Payment__c, AccountId FROM opportunity WHERE Id =: mPay.Opportunity__c LIMIT 1];
Account ac = [SELECT PersonMailingState FROM Account WHERE id =: o.AccountId];
LIST<State_Fee_limit__c> sf = [SELECT Fee__c FROM State_Fee_limit__c WHERE State__c =: ac.PersonMailingState];

if(sf.size() >=1){
decimal sfv = sf[0].Fee__c;

sfv = pm * sfv;
pm = pm + sfv;

o.Monthly_Payment__c = pm;
o.CCC_Fee__c = sfv;
update o;
}
}
}
}

 

 

public class Test_Dept_info_trigger {

static testMethod void myTest() {

// Create an Account
Account a = new Account();
a.Name = 'Test Account';
a.BillingState = 'CA';
insert a;

// Create a Contact
Contact c = new Contact();
c.FirstName = 'Test';
c.LastName = 'Contact';
c.AccountId = a.Id;
c.Email = 'contactemail@example.com';
insert c;

// Create Opportunities
//list<Opportunity> l_Opps = new list<Opportunity>();
Opportunity o = new Opportunity();
o.AccountId = a.id;
o.Name = 'Test Opportunity';
o.CloseDate = date.today();
o.StageName = 'Qualified';
o.Sales_Monthly_Fee__c = 49;
o.Description = 'Test Opportunity Description';
//l_Opps.add(o);


insert o;

//insert l_Opps;

//list<State_Fee_limit__c> l_st = new list<State_Fee_limit__c>();
//State_Fee_limit__c sfl = new State_Fee_limit__c();
//sfl.state__c = 'MI';
//sfl.Fee__c = 2;
//l_st.add(sfl);

//insert l_st;


Creditor_Table__c ctc = new Creditor_Table__c();
ctc.APR__c = '2.0';
ctc.Percent__c = '3.0';
ctc.Name = 'CITI';
insert ctc;

Creditor_Table__c cr = [SELECT Name FROM Creditor_Table__c WHERE Name = 'CITI' LIMIT 1];

//Debt_Info__c tdebt = new Debt_Info__c(Creditor_Table__c=cr.Id,Opportunity__c=o.id,Principal_Balance__c=1000);

Debt_Info__c tdebt = new Debt_Info__c();
tdebt.Creditor_Table__c=cr.Id;
tdebt.Opportunity__c=o.id;
tdebt.Principal_Balance__c=1000;

insert tdebt;

Debt_Info__c tdebt2 = new Debt_Info__c();
tdebt2.Creditor_Table__c=cr.Id;
tdebt2.Opportunity__c=o.id;
tdebt2.Principal_Balance__c=2000;

insert tdebt2;


delete tdebt;



}
}

Andy BoettcherAndy Boettcher

Is there an error message that comes up (that usually accompanies a not enough coverage on a trigger message)?

 

-Andy

mdinatalemdinatale

yeah it says 66% needs 75% not sure what other information i need to test, i just added the after delete and in the test im deleteing a debt_info record to trigger it.

Andy BoettcherAndy Boettcher

If you're running your tests via the Salesforce UI, click on the % of coverage number after your unit test - that will show you exactly what lines are covered vs. not covered.