• Gayatri Karthik
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 7
    Replies
Trigger code

trigger Ag_PurchaseContract_UpdateTradeConfirmation_Trigger on Ag_Purchase_Contract__c (after insert,after update) {

    set<string> tradeConfirmationNumSet = new set<String>();
    string purchaseContractName = null;
    for (Ag_Purchase_Contract__c purchase : Trigger.new)    
    {
      list<Ag_Purchase_Contract__c> purlist = [select id, Trade_Confirmation_No__c,name from Ag_Purchase_Contract__c where Trade_Confirmation_No__c = :purchase.Trade_Confirmation_No__c and name != :purchase.name];
   
      if (purlist != null && purlist.size() > 0 && purchase.Trade_Confirmation_No__c != null)
       {
         purchase.adderror('The TradeNumber is used in a different Purchase contract. The purchase contract number is ' + purlist[0].name  );
       }
       else
       {
         purchaseContractName = purchase.name;
         if(purchase.Trade_Confirmation_No__c != null )
         {
            if (purchase.Trade_Confirmation_No__c == Trigger.NewMap.get(purchase.Id).Trade_Confirmation_No__c)
            {
               tradeConfirmationNumSet.add(Trigger.NewMap.get(purchase.Id).Trade_Confirmation_No__c);
              
            }
            
            List<Trade_Confirmation__c> tcList = [Select ID,purchase_contract__c,name from Trade_Confirmation__c where id IN :tradeConfirmationNumSet];
          
            for (Trade_Confirmation__c tc: TClist)
            {
                tc.purchase_contract__c = purchaseContractName;
            }
             update tcList;
        }
     }
  }
}

the above trigger is working and 
test class is 

@isTest
private class Test_Ag_PurchaseContract {
    @istest static void testSuccess(){
    set<string> tradeConfirmationNumSet = new set<String>();
        
    string purchaseContractName = 'AGP/19/03/16599';
     String tradeConfirmationnum  = 'AGC0000012';
    Ag_Purchase_Contract__c purchase = new  Ag_Purchase_Contract__c();
     

      list<Ag_Purchase_Contract__c> purlist = [select id, Trade_Confirmation_No__c,name from Ag_Purchase_Contract__c where Trade_Confirmation_No__c = :tradeConfirmationnum and name=:purchaseContractName limit 1];
   
      if (purlist != null && purlist.size() > 0)
       {
         purchase.adderror('The TradeNumber is used in a different Purchase contract. The purchase contract number is ' + purlist[0].name  );
       }
       else
       {
        if(purchase.Trade_Confirmation_No__c != null )
         {
           
               tradeConfirmationNumSet.add(tradeConfirmationnum);
         }
            
         List<Trade_Confirmation__c> tcList = [Select ID,purchase_contract__c,name from Trade_Confirmation__c where id IN :tradeConfirmationNumSet limit 1];
         system.debug('purchaseContractName '+  purchase.name);
         system.debug('purchaseContractName After assign '+  purchaseContractName);
         system.debug('TClist :'+tcList.size());
           
           if(TClist!=null && TClist.size()>0)
           {
                for (Trade_Confirmation__c tc: TClist)
         {
           tc.purchase_contract__c = purchaseContractName;
           system.debug('tc.purchase_contract__c '+ tc.purchase_contract__c);
         }
           }
            
        
         try
         {
             update tcList;
         }
         catch(Exception e)
         {
             system.assertEquals(e.getMessage(), e.getMessage()) ;
         } 
       }
  }
}
With this test class Code coverage is zero.how to write pls help me , 

 
Trigger code

trigger Ag_PurchaseContract_UpdateTradeConfirmation_Trigger on Ag_Purchase_Contract__c (after insert,after update) {

    set<string> tradeConfirmationNumSet = new set<String>();
    string purchaseContractName = null;
    for (Ag_Purchase_Contract__c purchase : Trigger.new)    
    {
      list<Ag_Purchase_Contract__c> purlist = [select id, Trade_Confirmation_No__c,name from Ag_Purchase_Contract__c where Trade_Confirmation_No__c = :purchase.Trade_Confirmation_No__c and name != :purchase.name];
   
      if (purlist != null && purlist.size() > 0 && purchase.Trade_Confirmation_No__c != null)
       {
         purchase.adderror('The TradeNumber is used in a different Purchase contract. The purchase contract number is ' + purlist[0].name  );
       }
       else
       {
         purchaseContractName = purchase.name;
         if(purchase.Trade_Confirmation_No__c != null )
         {
            if (purchase.Trade_Confirmation_No__c == Trigger.NewMap.get(purchase.Id).Trade_Confirmation_No__c)
            {
               tradeConfirmationNumSet.add(Trigger.NewMap.get(purchase.Id).Trade_Confirmation_No__c);
              
            }
            
            List<Trade_Confirmation__c> tcList = [Select ID,purchase_contract__c,name from Trade_Confirmation__c where id IN :tradeConfirmationNumSet];
          
            for (Trade_Confirmation__c tc: TClist)
            {
                tc.purchase_contract__c = purchaseContractName;
            }
             update tcList;
        }
     }
  }
}

the above trigger is working and 
test class is 

@isTest
private class Test_Ag_PurchaseContract {
    @istest static void testSuccess(){
    set<string> tradeConfirmationNumSet = new set<String>();
        
    string purchaseContractName = 'AGP/19/03/16599';
     String tradeConfirmationnum  = 'AGC0000012';
    Ag_Purchase_Contract__c purchase = new  Ag_Purchase_Contract__c();
     

      list<Ag_Purchase_Contract__c> purlist = [select id, Trade_Confirmation_No__c,name from Ag_Purchase_Contract__c where Trade_Confirmation_No__c = :tradeConfirmationnum and name=:purchaseContractName limit 1];
   
      if (purlist != null && purlist.size() > 0)
       {
         purchase.adderror('The TradeNumber is used in a different Purchase contract. The purchase contract number is ' + purlist[0].name  );
       }
       else
       {
        if(purchase.Trade_Confirmation_No__c != null )
         {
           
               tradeConfirmationNumSet.add(tradeConfirmationnum);
         }
            
         List<Trade_Confirmation__c> tcList = [Select ID,purchase_contract__c,name from Trade_Confirmation__c where id IN :tradeConfirmationNumSet limit 1];
         system.debug('purchaseContractName '+  purchase.name);
         system.debug('purchaseContractName After assign '+  purchaseContractName);
         system.debug('TClist :'+tcList.size());
           
           if(TClist!=null && TClist.size()>0)
           {
                for (Trade_Confirmation__c tc: TClist)
         {
           tc.purchase_contract__c = purchaseContractName;
           system.debug('tc.purchase_contract__c '+ tc.purchase_contract__c);
         }
           }
            
        
         try
         {
             update tcList;
         }
         catch(Exception e)
         {
             system.assertEquals(e.getMessage(), e.getMessage()) ;
         } 
       }
  }
}
With this test class Code coverage is zero.how to write pls help me ,