1

In an apex "before update" trigger, I have a conditional statement based on a picklist value. However, this conditional statement never fires because the picklist value always returns null.

trigger NewProposalPQ on Proposal__c (before update) {
    List<ProdQueue__c> peecues = new List<ProdQueue__c>();
    for (Proposal__c p: trigger.new){
        system.debug(p.name);
        system.debug(p.Request_BOV_from_Production__c);
        system.debug(p.BOV_Created__c);
        if (p.Request_BOV_from_Production__c == 'YES' && p.BOV_Created__c == False){
            ProdQueue__c pq = new ProdQueue__c(name = p.name, Proposal__c = p.Id,Production_Type__c='BOV');
            if(p.Client__c != null){
                pq.Client__c = p.Client__c;  
            }
            if(p.Property__c != null){
                pq.property__c = p.property__c;
            }
            peecues.add(pq);
            insert peecues;
            p.production__c = pq.id;
            p.BOV_Created__c = True;
        }
    }
}

In my debug log, the value of p.Request_BOV_from_Production__c is always null.

  • Have you double checked the Field Level Security settings of your Request_BOV_from_Production__c field to make sure that you (or the user you're testing as) can read the field? If that's not the issue, are your org's profiles set to be able to read the field? – Derek F Apr 26 '16 at 20:14
  • can you explain more briefly because Request_BOV_from_Production__c always null means..@MRG – Pavan tej Apr 26 '16 at 20:14
  • 2
    How is the field being set? By a workflow? By a user? A process? – sfdcfox Apr 26 '16 at 20:15
  • 1
    Try creating a test class so you know you'll have valid data. Insert a record and set a value for the picklist. Then do an update which should fire your trigger. That should help you troubleshoot your issue. Be sure to use a RunAs() user and use Test.StartTest(), etc. If it happens there, that will help you find the source of the issue when the record is saved in the insert. – crmprogdev Apr 26 '16 at 22:20
  • 1
    As an aside to the question, you should move the insert peecues; line to outside of the for loop, as it will cause an excess amount of DML statements to be executed – Br0k3nL1m1ts Apr 26 '16 at 22:45
3

An answer to your issue about not being able to do this trigger's work without performing DML in a loop... you've just got to get a little fancier in your implementation.

trigger NewProposalPQ on Proposal__c (before update) {
    List<ProdQueue__c> prodQueuesToInsert = new List<ProdQueue__c>();

    for (Proposal__c p: trigger.new){
        system.debug(p.name);
        system.debug(p.Request_BOV_from_Production__c);
        system.debug(p.BOV_Created__c);
        if (p.Request_BOV_from_Production__c == 'YES' && p.BOV_Created__c == false) {

            ProdQueue__c pq = new ProdQueue__c(Name = p.Name
                                            , Proposal__c = p.Id
                                            , Production_Type__c = 'BOV');

            if (p.Client__c != null) {
                pq.Client__c = p.Client__c;  
            }
            if (p.Property__c != null) {
                pq.property__c = p.property__c;
            }

            prodQueuesToInsert.add(pq);

        }
    }

    // is there DML work to do?
    if (!prodQueuesToInsert.isEmpty()) {
        insert prodQueuesToInsert;

        // loop over the ProdQueue records and get their new ids
        // and associate them with the Proposal__c record
        for (ProdQueue__c pQueue : prodQueuesToInsert) {

            // get the original Proposal__c record from the newMap
            // using the relationship set earlier
            Proposal__c p = trigger.newMap.get(pQueue.Proposal__c);

            // set this pQueue Id on the related Proposal__c
            p.Production__c = pQueue.Id;
            p.BOV_Created__c = true;
            // because this is a before update, the Proposal__c here
            // will have a reference to this ProdQueue__c record when finished
        }
    }
}
  • The isEmpty check doesn't really add anything. – Adrian Larson Apr 27 '16 at 1:19
  • You are correct from a DML limits perspective. I don't presume to know what other logic might be performed if the list is not empty, so I added it to contain all of the 'post processing' logic. – Mark Pond Apr 27 '16 at 1:45
  • 1
    Ah gotcha. The post processing you have in there wouldn't be affected either, but it makes sense that there might be other logic. – Adrian Larson Apr 27 '16 at 1:54
2

Just for your own education (@MarkPond has the correct answer, be sure to give him credit) here's an alternate, fewer lines solution. Note the elimination of the if tests to set productQueue__c.client__c and property__c. If the Proposal__c has null values then no reason not to copy null into the ProdQueue__c fields.

trigger NewProposalPQ on Proposal__c (before update) {
  List<ProdQueue__c> pqInsList = new List<ProdQueue__c>();

  for (Proposal__c p: trigger.new){
    if (p.Request_BOV_from_Production__c == 'YES' && !p.BOV_Created__c) 
        pqInsList.add(new ProdQueue__c(
                                     Name = p.Name
                                   , Proposal__c = p.Id
                                   , Production_Type__c = 'BOV'
                                   , Client__c = p.client__c
                                   , Property__c = p.property__c
                              );

  }

  insert pqInsList; // no DML burned if list is empty

    // link the triggered Proposals to the created ProdQueues. 
    // Before update implicitly adds these values to the object being saved.
  for (ProdQueue__c pq : pqInsList) {
    trigger.newMap.get(pq.Proposal__c).Production__c = pq.id; 
    trigger.newMap.get(pq.Proposal__c).BOV_created__c = true;
  }

} 

Normally, one does related object DML in an after trigger but because of the two-way linking, that code would be more complex as you have to avoid recursion when updating the Proposal__c records so I can see why you went this route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.