• Rakesh Sahoo 22
  • NEWBIE
  • 5 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
Hi,

Anyone can help me to write test class for the below trigger.


trigger Matching_AnnualTarget_SFInvoice on Invoice_Dispatch_details__c(after insert,before update) {

    Set<string> sfinvoices=new Set<string>(); 
    Set<string> sfinvoicesMonthWisetarget=new Set<string>();
    set<Id> anuallId=new set<Id>();
    
    for(Invoice_Dispatch_details__c thesfinvoices: Trigger.new){
        sfinvoices.add(thesfinvoices.Concatenate__c);
        sfinvoicesMonthWisetarget.add(thesfinvoices.Concatenate_For_Month_Wise_Target_formul__c);
    }
    
    Map <String, Annual_Target__c> matchingAnnualTargetMap = new Map <String, Annual_Target__c>();
    for (Annual_Target__c annualtarget : [Select Id, Name,Concatenate__c From Annual_Target__c Where Concatenate__c IN :sfinvoices])
    {
        anuallId.add(annualtarget.id);
        matchingAnnualTargetMap.put(annualtarget.Concatenate__c, annualtarget);
    }
    Map <String, Month_Target__c> matchingMonthlyTargetMap = new Map<String, Month_Target__c>();
    for (Month_Target__c monthlytarget : [Select Id, Name,Concatenate_formula__c From Month_Target__c Where Concatenate_formula__c IN :sfinvoicesMonthWisetarget AND Annual_Target__c IN:anuallId])
    {
        matchingMonthlyTargetMap.put(monthlytarget.Concatenate_formula__c, monthlytarget);
    }
    
    List <Invoice_Dispatch_details__c> SFInvoiceToUpdate = new List <Invoice_Dispatch_details__c>();
    for(Invoice_Dispatch_details__c thesfinvoices : trigger.New){
        if (matchingAnnualTargetMap.get(thesfinvoices.Concatenate__c) != null)
        {
            thesfinvoices.Annual_Target__c = matchingAnnualTargetMap.get(thesfinvoices.Concatenate__c).Id;
            
            //thesfinvoices.Month_Wise_Target_Management__c = matchingAnnualTargetMap.get(thesfinvoices.Concatenate__c).Id;
            //SFInvoiceToUpdate.add(thesfinvoices);
        }
    }
    //update SFInvoiceToUpdate;
     List <Invoice_Dispatch_details__c> SFInvoiceToUpdate2 = new List <Invoice_Dispatch_details__c>();
     for(Invoice_Dispatch_details__c thesfinvoices : trigger.New){
        if (matchingMonthlyTargetMap.get(thesfinvoices.Concatenate_For_Month_Wise_Target_formul__c) != null)
        {
            thesfinvoices.Month_Wise_Target_Management__c= matchingMonthlyTargetMap.get(thesfinvoices.Concatenate_For_Month_Wise_Target_formul__c).Id;
            //thesfinvoices.Month_Wise_Target_Management__c = matchingAnnualTargetMap.get(thesfinvoices.Concatenate__c).Id;
            //SFInvoiceToUpdate.add(thesfinvoices);
        }
    }
    
}



Thanks in advance,
Please help me to get 100% code coverage of this apex class.


public class ctrl_LeadCreation
{
    public Lead__c ld{get;set;}
    public User u{get;set;}
    public Boolean zklPr{get;set;}
    public boolean fkl{get;set;}
    public Integer x{get;set;}
    public Integer y{get;set;}
    
    public ctrl_LeadCreation(Apexpages.standardController std)
    {
        ld=(Lead__c)std.getRecord();
        u=[Select CompanyName from User where id=:Userinfo.getUserId()];
        if(u.CompanyName=='ZKL' || u.CompanyName=='PR')
        {
            zklPr=true;
        }
        if(u.CompanyName=='FKL')
        {
            fkl=true;
        }        
    }
    
    public PageReference save()
    {
        Recordtype r1=[Select id from Recordtype where sobjecttype='Lead__c' and developername=:'Lead_Eligibility_Test'];
        Recordtype r2=[Select id from Recordtype where sobjecttype='Lead__c' and developername=:'Details_For_FKL'];
        
        y=0;
        
        if(u.CompanyName=='ZKL' || u.CompanyName=='PR')
        {
            
            
            x=1;
            List<Lead__c> ldzklpr=[Select number_count__c from Lead__c where FormulateUsersCompany__c=:u.CompanyName and recordtype.developername='Lead_Eligibility_Test' order by createddate desc limit 1];
            ld.recordtypeId=r1.id;
            if(!ldzklpr.isEmpty())
            {
                y=x+Integer.valueOF(ldzklpr[0].number_count__c);
                ld.Lead_Number_Set__c=u.CompanyName+'-LD-'+y;
                ld.number_count__c=Decimal.valueOF(y);
                ld.Approval_Status__c='New';
            }
            else
            {
                ld.Lead_Number_Set__c=u.CompanyName+'-LD-'+x;
                ld.number_count__c=Decimal.valueOF(x);
                ld.Approval_Status__c='New';
            }
         }
         
         if(u.CompanyName=='FKL')
         {
             x=1;
             List<Lead__c> ldfkl=[Select number_count__c from Lead__c where FormulateUsersCompany__c=:u.CompanyName and recordtype.developername='Details_For_FKL' order by createddate desc limit 1];
             ld.recordtypeId=r2.id;
             if(!ldfkl.isEmpty())
            {
                y=x+Integer.valueOf(ldfkl[0].number_count__c);
                ld.Lead_Number_Set__c=u.CompanyName+'-LD-'+y;
                ld.number_count__c=Decimal.valueOF(y);
                ld.Approval_Status__c='New';
            }
            else
            {
                ld.Lead_Number_Set__c=u.CompanyName+'-LD-'+x;
                ld.number_count__c=Decimal.valueOF(x);
                ld.Approval_Status__c='New';
            }
         }
         
         insert ld;
         
         Pagereference pg=new PageReference('/'+ld.id);
         return pg.setRedirect(true);
    }
    
    public PageReference cancel()
    {
        Schema.DescribeSObjectResult result = Lead__c.SObjectType.getDescribe();
        PageReference pg=new PageReference('/' + result.getKeyPrefix() + '/o');
        pg.setRedirect(true);
        return pg;
   }
}


Please HELP...
Thanks
So the structure I have is this:

Record of Custom Object Type A has a related list of multiple records of Custom Object Type B.


And this is what I'm looking to do:

Object Type B has a Currency field, and Object Type A has a field that is inteded to be the Sum of that Currency field for all records in the related list. Whenever any record of Object Type B is updated, I need the Sum field on Object Type A to calculate the new sum. (Essentially it is a rollup sum, but I cannot use a rollup field because these do not have a Master-Detail relationship.)


Example:

Initial State:
  • Object Type A Record SumField = 100
    • Related ObjectB Record 1 CurrencyField = 50
    • Related ObjectB Record 2 CurrencyField = 30
    • Related ObjectB Record 3 CurrencyField = 20
User Action:
  • Related ObjectB Record 2 CurrencyField is changed from 30 to 80
Desired End State:
  • Object Type A Record SumField = 150
    • Related ObjectB Record 1 CurrencyField = 50
    • Related ObjectB Record 2 CurrencyField = 80
    • Related ObjectB Record 3 CurrencyField = 20