2

I'm having trouble bulkifying this query? Anyone has any idea how I could achieve that.

trigger OrbT_LinhadeFatura on Linha_de_Fatura__c (after insert) {

    for(Linha_de_Fatura__c lf : trigger.new) {
        if (lf.Valor_Venda__c > 0) {

            List<Linha_de_Fatura__c> = [Select Valor_Venda__c from Linha_de_Fatura__c 
                                        Where Produto__c =: lf.Produto__c AND Fatura_r.Conta__c =: lf.Fatura_r.Conta__c ORDER BY Data__c DESC LIMIT 6]
        }
    }

}

This is what I have right now

trigger OrbT_LinhadeFatura on Linha_de_Fatura__c (after insert) {
    public class pair{    
        public String produto {get;set;}
        public String conta {get;set;}       
            public pair(String a, String b){         
                produto = a;
                conta = b;
            }
    }

    List<pair> pairs = new List<pair>();

    for(Linha_de_Fatura__c lf : trigger.new) {
        if (lf.Valor_Venda__c > 0) {
            pairs.add(new pair(String.valueOf(lf.Produto__c), String.valueOf(lf.Fatura_r.Conta__c)));
        }
    }

    String qString = 'Select Valor_Venda__c from Linha_de_Fatura__c Where';
    Integer counter = 1;
    for(pair p : pairs){
        qString += '(Produto__c = \'' + p.produto + '\' AND Fatura_r.Conta__c = \'' + p.conta + '\') ';
        if(counter < pairs.size())
          qString+= ' OR ';
        counter++;
    }
    qString += ')';
    system.debug('qString: ' + qString);

    List<Linha_de_Fatura__c> lfList = database.query(qString);
}

I just want it more efficient and get the 6 latest records for each linha_de_fatura__c.

2 Answers 2

3

Build up collections of your filter values:

Set<String> produtos = new Set<String>();
Set<String> contas = new Set<String>();
Set<Id> faturas = new Set<Id>();
for (Linha_de_Fatura__c lf : trigger.new)
{
    produtos.add(lf.Produto__c);
    faturas.add(lf.Fatura__c);
}
for (Fatura__c fatura : [SELECT Conta__c FROM Fatura__c WHERE Id IN :faturas])
   contas.add(fatura.Conta__c);

List<Linha_de_Fatura__c> linhas = [
    SELECT Id FROM Linha_de_Fatura__c
    WHERE Produto__c IN :produtos
    AND Fatura__r.Conta__c IN :contas
];
3
  • 2
    Your Produto__c query will return combinations of Produto__c and Conta__c that are not present in Trigger.new.
    – Keith C
    Feb 19, 2016 at 15:42
  • Thanks for the prompt answer. But how can I get only the 6 more recent records for each of the Linha_de_Fatura__c? That's really my problem
    – MikeFerrer
    Feb 19, 2016 at 15:43
  • @AdrianLarson In your original code if the two fields had values of a and 1 and b and 2 your query would also return matches for a and 2 and b and 1.
    – Keith C
    Feb 19, 2016 at 15:54
2

A lack of reputation has it that I can't comment, and so I dare answer:

Can you please go back one step and articulate your requirements? What are you actually trying to achieve with those, all in all, six invoice line-items?

It's just, when I look at your initial query, you appear to fetch the amount value of some six invoice line-items that have the same product and the same account as the one being inserted. What could possibly be the purpose of that?

I mean, if you're going to bulkify trigger code, let it be the code you're truly after.

1
  • 1
    True it's not really an answer, but +1 for "if you're going to bulkify trigger code, let it be the code you're truly after."
    – Adrian Larson
    Feb 19, 2016 at 19:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .