0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

デフォルトの価格表のテストクラス

Posted at

元の質問 : Trigger coverage

Pricebook2オブジェクトの IsStandard フィールドは直接updateできないようです。
困った。
そのためテストクラスには標準の価格表のIdを得るメソッドが用意されているみたい。

Test.getStandardPricebookId();

しかし、テストクラスでは標準の価格表のIdが欲しい。
しかたないので、トリガーの方を変更します。

PDF作成の時の回避方法と似た感じですね。

     if (Test.isRunningTest() ){
             stPrId = Test.getStandardPricebookId(); 
        } else {
             Pricebook2 pb = [select Id from Pricebook2 where IsStandard = TRUE][0]; 
            stPrId = pb.Id;
         }
   /**
      * apex:pageを利用してPDFをつくる
     */
    @AuraEnabled
    public static String createPdf(){
        
        PageReference pageRef = new PageReference('/apex/pdfFkd_test');
        pageRef.getParameters().put('mode', 'save');
        pageRef.getParameters().put('Title', 'Simple');

        //pageRef.getParameters().put('id', paramIds);
        //pageRef.getParameters().put('rmk', remarks);
        //String body = Test.isRunningTest() ? 'ダミー' : String.valueof(pageRef.getContent());        
        String body = Test.isRunningTest() ? 'ダミー' : EncodingUtil.Base64Encode(pageRef.getContent());
        
        return body;
    }

トリガーの全容

public class Product2TriggerHandler {
    
    public static void AtualizaValorProduto(List<Product2> produtosLista){
        list<Id> Idlist = new list<Id>();
        list<Product2> ProdsToUpdate = new list<Product2>();
        for(Product2 pr:produtosLista){
            Idlist.add(pr.Id);
        }
        Id stPrId;
        if (Test.isRunningTest() ){
            stPrId = Test.getStandardPricebookId();
        } else {
            Pricebook2 pb = [select Id from Pricebook2 where IsStandard = TRUE][0];
            stPrId = pb.Id;
        }
        
        for(Product2 p:produtosLista){
            for(Pricebookentry pbe:[select Product2Id,UnitPrice from Pricebookentry where Pricebook2Id = :stPrId and Product2Id IN :Idlist]){
                If((pbe.Product2Id == p.Id)&&(p.Preco_catalogo_padrao__c!=pbe.UnitPrice)){
                    p.Preco_catalogo_padrao__c = pbe.UnitPrice;
                    system.debug('this is it '+pbe.UnitPrice);
                    system.debug('this is that '+p.Preco_catalogo_padrao__c);
                    ProdsToUpdate.add(p);
                }
            }
        }
        If(ProdsToUpdate.size()>0&&trigger.isAfter){
            update ProdsToUpdate;
        }
    } 
    
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?