Skip to main content
Gayatri Karthikeyan 님이 #Apex에 질문했습니다
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 , 

 
답변 18개
  1. 2019년 7월 9일 오후 11:25
    Hi Gayatri

    Depending on the exact update and the relation to the first object, you could use Process Builder or Flows, or a combination of both.  And if you want to simply display data in a child, you could use a formula field.

    Regards

    Andrew
0/9000