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
Vasu@blrVasu@blr 

how to get 75% code coverage for my trigger?

Hi, below is my trigger how can I achieve 75% code coverage for my trigger,

Please, help me to get 75% code coverage.

@isTest
private class Test_My_MonthsRollUp_US{
    static testmethod void test_MonthsRollup_Us(){
        Profile p=[SELECT Id FROM profile WHERE name='System Administrator'];
        UserRole ur = new UserRole(Name='Role1'); 
        User u=new User(UserRoleid=ur.id,alias = 'user123', 
        email='user123@mail.com',emailencodingkey='UTF-8', lastname='u',
        languagelocalekey='en_US',localesidkey='en_US',
        profileid = p.Id,timezonesidkey='America/Los_Angeles',username='user@mail.com',CommunityNickname='usr');
        insert u;       
        RecordType rt = [select id, name 
                     from Recordtype 
                     where sobjectType ='Account' and name = 'Standard Account'];
        Account acc = new Account(
         RecordTypeid = rt.id ,
         Name = 'abc' ,
         Estimated_Annual_Spend__c = 1200 ,
         Market_Segment__c = 'Exporter' ,
         Lead_Source__c = 'ARCAT' ,   
         BillingStreet ='24,marg' ,
         BillingCity = 'kolkata' ,
         BillingState = 'WB' ,
         BillingPostalCode = '700061' ,
         BillingCountry ='India' ,
         Tier_Level__c = 'Tier 1',Total_My_SO_Sales_January__c=0, Total_My_SO_Sales_February__c=0,Total_My_SO_Sales_March__c=0,
    Total_My_SO_Sales_April__c=0,Total_My_SO_Sales_May__c=0,Total_My_SO_Sales_June__c=0,
    Total_My_SO_Sales_July__c=0,Total_My_SO_Sales_August__c=0,Total_My_SO_Sales_September__c=0,
    Total_My_SO_Sales_October__c=0,Total_My_SO_Sales_November__c=0,Total_My_SO_Sales_December__c=0,
    Total_Cash_Carry_Sales_January__c=0, Total_Cash_Carry_Sales_February__c=0,Total_Cash_Carry_Sales_March__c=0,
    Total_Cash_Carry_Sales_April__c=0,Total_Cash_Carry_Sales_May__c=0,Total_Cash_Carry_Sales_June__c=0,
    Total_Cash_Carry_Sales_July__c=0,Total_Cash_Carry_Sales_August__c=0,Total_Cash_Carry_Sales_September__c=0,
    Total_Cash_Carry_Sales_October__c=0,Total_Cash_Carry_Sales_November__c=0,Total_Cash_Carry_Sales_December__c=0);         
         insert acc;
        
        List<THD_Sales__c> thdlst = new List<THD_Sales__c>();
        THD_Sales__c thdrec1 = new THD_Sales__c(
        Month_Year__c = date.parse('1/1/2012'),
        POS_Order_Type__c = 'Special Order',
        Amount__c = 10000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec2 = new THD_Sales__c(
        Month_Year__c = date.parse('2/1/2012'),
        POS_Order_Type__c = 'Special Order',
        Amount__c = 5000.00,
        SFDC_Account_ID__c = acc.id
        );  
        THD_Sales__c thdrec3 = new THD_Sales__c(
        Month_Year__c = date.parse('3/1/2012'),
        POS_Order_Type__c = 'Special Order',
        Amount__c = 4000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec4 = new THD_Sales__c(
        Month_Year__c = date.parse('4/1/2012'),
        POS_Order_Type__c = 'Cash & Carry',
        Amount__c = 3000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec5 = new THD_Sales__c(
        Month_Year__c = date.parse('5/12/2012'),
        POS_Order_Type__c = 'Cash & Carry',
        Amount__c = 2000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        THD_Sales__c thdrec6 = new THD_Sales__c(
        Month_Year__c = date.parse('6/1/2012'),
        POS_Order_Type__c = 'Cash & Carry',
        Amount__c = 1000.00,
        SFDC_Account_ID__c = acc.id
        ); 
        thdlst.add(thdrec1);
        thdlst.add(thdrec2);
        thdlst.add(thdrec3);
        thdlst.add(thdrec4);
        thdlst.add(thdrec5);
        thdlst.add(thdrec6);
        insert thdlst;
        
        
        thdrec1.POS_Order_Type__c = 'Special Order';
        thdrec1.Amount__c = 5000.00;   
       
        thdrec2.POS_Order_Type__c = 'Cash & Carry';
        thdrec2.Amount__c = 8000.00; 
        
        update thdrec1;      
        update thdrec2;
    }
}

 Below is my trigger:

trigger My_MonthsRollUp_US on THD_Sales__c (after insert, after update, after delete, after undelete) {
    Map<Id,Account> updateAccounts = new Map<Id,Account>();
    Set<Id> updateAccountIds = new Set<Id>();    
  // If we are inserting, updating, or undeleting, use the new ID values
  if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete) {
    if(trigger.new != null)
        for(THD_Sales__c thdsales:Trigger.new)
            if(thdsales.SFDC_Account_ID__c != '0017000000ZWk80AAD')
              updateAccounts.put(thdsales.SFDC_Account_ID__c,null);
  }
  // If we are updating, some accounts might change, so include that as well as deletes
  if(Trigger.isUpdate || Trigger.isDelete) {
    if(trigger.old != null)
        for(THD_Sales__c thdsales:Trigger.old)
            if(thdsales.SFDC_Account_ID__c != '0017000000ZWk80AAD')
              updateAccounts.put(thdsales.SFDC_Account_ID__c,null);
  }
  // Do not create a record for null field
  updateAccounts.remove(null);  
  // Create in-memory copies for all accounts that will be affected
  for(Id accountId:updateAccounts.keyset()) 
    updateAccounts.put(accountId,new Account(id=accountId,
    Total_My_SO_Sales_January__c=0, Total_My_SO_Sales_February__c=0,Total_My_SO_Sales_March__c=0,
    Total_My_SO_Sales_April__c=0,Total_My_SO_Sales_May__c=0,Total_My_SO_Sales_June__c=0,
    Total_My_SO_Sales_July__c=0,Total_My_SO_Sales_August__c=0,Total_My_SO_Sales_September__c=0,
    Total_My_SO_Sales_October__c=0,Total_My_SO_Sales_November__c=0,Total_My_SO_Sales_December__c=0,
    Total_Cash_Carry_Sales_January__c=0, Total_Cash_Carry_Sales_February__c=0,Total_Cash_Carry_Sales_March__c=0,
    Total_Cash_Carry_Sales_April__c=0,Total_Cash_Carry_Sales_May__c=0,Total_Cash_Carry_Sales_June__c=0,
    Total_Cash_Carry_Sales_July__c=0,Total_Cash_Carry_Sales_August__c=0,Total_Cash_Carry_Sales_September__c=0,
    Total_Cash_Carry_Sales_October__c=0,Total_Cash_Carry_Sales_November__c=0,Total_Cash_Carry_Sales_December__c=0
    ));
    
    // Run an optimized query that looks for all accounts that meet the if/then criteria
    map< string, list< schema.sobjectfield > > fieldMap =
        new map< string, list< schema.sobjectfield > > {
            'Special Order' =>
                new list< schema.sobjectfield > {
                    null,
                    Account.Total_My_SO_Sales_January__c,
                    Account.Total_My_SO_Sales_February__c,
                    Account.Total_My_SO_Sales_March__c,
                    Account.Total_My_SO_Sales_April__c,
                    Account.Total_My_SO_Sales_May__c,
                    Account.Total_My_SO_Sales_June__c,
                    Account.Total_My_SO_Sales_July__c,
                    Account.Total_My_SO_Sales_August__c,
                    Account.Total_My_SO_Sales_September__c,
                    Account.Total_My_SO_Sales_October__c,
                    Account.Total_My_SO_Sales_November__c,
                    Account.Total_My_SO_Sales_December__c
                },
           'Cash & Carry' =>
                new list< schema.sobjectfield > {
                    null,
                    Account.Total_Cash_Carry_Sales_January__c,
                    Account.Total_Cash_Carry_Sales_February__c,
                    Account.Total_Cash_Carry_Sales_March__c,
                    Account.Total_Cash_Carry_Sales_April__c,
                    Account.Total_Cash_Carry_Sales_May__c,
                    Account.Total_Cash_Carry_Sales_June__c,
                    Account.Total_Cash_Carry_Sales_July__c,
                    Account.Total_Cash_Carry_Sales_August__c,
                    Account.Total_Cash_Carry_Sales_September__c,
                    Account.Total_Cash_Carry_Sales_October__c,
                    Account.Total_Cash_Carry_Sales_November__c,
                    Account.Total_Cash_Carry_Sales_December__c
                } 
        };     

   for(THD_Sales__c thdsales : [SELECT Id, Month_Year__c, POS_Order_Type__c, Amount__c, SFDC_Account_ID__c
   FROM  THD_Sales__c WHERE SFDC_Account_ID__c IN :updateAccounts.keySet( ) AND 
   POS_ORDER_TYPE__C IN ('Special Order','Cash & Carry') AND Month_Year__c != null]){ 
                            
        updateAccounts.get( thdsales.sfdc_account_id__c ).put(fieldmap.get( thdsales.pos_order_type__c )[thdsales.month_year__c.month()],
            ( Decimal )( updateAccounts.get( thdsales.sfdc_account_id__c ).get( fieldmap.get( thdsales.pos_order_type__c )[thdsales.month_year__c.month()]))+thdsales.Amount__c );
    }
  // Update all the accounts with new values.
  Database.update(updateAccounts.values());
}

 

SRKSRK

you can use @seealldata=ture at the top of  your test method

 

 

also can you plz tell me how much it covering rite now

Vasu@blrVasu@blr

It is covering 20%

jd123jd123

Hey which lines are not covered please highlight with red color then I can help you.

SRKSRK

Can you please share the screen shot of code coverage it would be more helpful

 

Thanks