+ Start a Discussion
Vanessa BarrosVanessa Barros 

help to a trigger

hi. i have a problem with a select: //buscar a Margem PRIO consoante o produto
                Parametros_fixos_Pricing__c pfp_margem =
                //List<Parametros_fixos_Pricing__c> pfp_margem =
                [select margem_prio__c
                from Parametros_fixos_Pricing__c
                where produto__c = :pr2.id AND
                Centro_de_carga2__c = :ccd.Centro_de_Carga__c AND
               t__c = '012Q00000008aTE' AND
                
                
                Valido_ate__c >= today AND
                Valido_de__c <= today  ];

 

When the trigger runs, i get a error on the select above: to many soql: 21

someone can help me?

 

----------------------------------------this is my code---------------------------------------------------------------

trigger InserePrecoCompraOuPrecoVenda on Pricing__c (after insert) {
    for (Pricing__c pr : Trigger.new){
        if(pr.TR_background__c == 'Platts'){
    //já tenho aqui disponivel o valor final do platts na variavel pr.preco_final__c
    
            //construção da lista de produtos
            List<Product2> produtos = new List<Product2>();
            produtos = [Select id,Produto_Referencia_Preco_de_venda__c,
            iva__c from Product2 where Produto_Referencia_Platts__c = :pr.produto__c AND
            Grupo_de_Materiais__c = ''];
                            
                           
            if (produtos.isEmpty() == false){
            
                for(Product2 pr2 : produtos){
                //PVP REf para o produto
                Pricing__c pricing_pvp_ref
         
               = [select preco_final__c from Pricing__c where
                produto__c = :pr2.Produto_Referencia_Preco_de_venda__c AND
                TR_background__c = 'Preço de Venda' AND
              
                  Valido_ate__c >= today AND
                 Valido_de__c <= today
                    
                    ];
                //construção CCD para o produto
                    List<Centros_de_Carga_Disponiveis__c> CcDisponiveis =[select Centro_de_Carga__c from Centros_de_Carga_Disponiveis__c
                    where Produto__c =:pr2.id];
                //Fazer o Loop à tabela de centros de carga
              // if (CcDisponiveis.isEmpty() == false ){
               
                for(Centros_de_Carga_Disponiveis__c ccd : CcDisponiveis){
                    
                //buscar os dados anuais para cada centro de carga
                 Parametros_fixos_Pricing__c pfp =
                [select CSR_m__c,Fee_Instalacao_m__c,ISP_m__c,Spread_m3__c
                from Parametros_fixos_Pricing__c
                where
                tr_background__c = 'Parametros Anuais' AND
                produto__c = :pr2.id AND
                Centro_de_carga2__c = :ccd.Centro_de_Carga__c AND
                Valido_ate__c >= today AND
                Valido_de__c <= today ];
                 
                //buscar a Margem PRIO consoante o produto
                Parametros_fixos_Pricing__c pfp_margem =
              
                [select margem_prio__c
                from Parametros_fixos_Pricing__c
                where produto__c = :pr2.id AND
                Centro_de_carga2__c = :ccd.Centro_de_Carga__c AND
               t__c = '012Q00000008aTE' AND
                
                
                Valido_ate__c >= today AND
                Valido_de__c <= today  ];
              
                
                //Inserção do registo no objecto Pricing Produto final  
                Pricing_Produto_Final__c  ppf = new  Pricing_Produto_Final__c();                      
                ppf.produto__c =pr2.id;
                ppf.preco_de_compra__c=pr.preco_final__c + pfp.CSR_m__c +
                                         pfp.Fee_Instalacao_m__c + pfp.ISP_m__c + pfp.Spread_m3__c;
                ppf.Centro_de_carga2__c = ccd.Centro_de_Carga__c;
                ppf.margem_prio__c = pfp_margem.margem_prio__c;
                ppf.pvp_ref__c = (pricing_pvp_ref.preco_final__c*1000) /(1+ (pr2.iva__c*0.01));
                ppf.valido_de__c = pr.valido_de__c;
                ppf.valido_ate__c = pr.valido_ate__c;
                insert ppf;    
        
                }                          
               
           // }

        }
        }
        }
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

You have several select statements inside of your "for loop", this will often cause problems.  You need to refactor your code to get the selects out of the loop.  Usually this is accomplished by leveraging Maps.

Here is a good article that describes how to do this:

http://blog.jeffdouglas.com/2009/04/20/writing-bulk-triggers-for-salesforce/

All Answers

JimRaeJimRae

You have several select statements inside of your "for loop", this will often cause problems.  You need to refactor your code to get the selects out of the loop.  Usually this is accomplished by leveraging Maps.

Here is a good article that describes how to do this:

http://blog.jeffdouglas.com/2009/04/20/writing-bulk-triggers-for-salesforce/

This was selected as the best answer
Vanessa BarrosVanessa Barros

Hi. thanks for your comment.

but the select statements must be inside the for loop to do the calcution of the pricing

Vanessa BarrosVanessa Barros

i read your article its very usefull tks :)