1

new at Apex having an issue with creating a trigger. I'm trying to update the value of a picklist depending on if a checkbox is checked in Carrier_Product__c. I know you cannot do this method, and it relies on another lookup where it won't return null. Here's what I have, thank you.

trigger ShellPolicyRebalance on Quote_Policy__c(before insert) {
  for (Quote_Policy__c p: Trigger.new) {
    if (p.RecordTypeId == Schema.SObjectType.Quote_Policy__c.getRecordTypeInfosByName().get('Shell Policy').getRecordTypeId()) {
      if (p.Carrier_Product__r.Rebalance__c) {
        p.Renewal_Flow__c = 'Rebalance';
      }
    }
  }
}
1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Feb 18, 2022 at 0:08

2 Answers 2

2

This is probably doable in a flow... but to do this in a trigger, there are some generic steps that you can follow.

  1. Get get all the carrier product ids. Put them in a list or a set by looping Trigger.new

  2. Query the Carrier_Product__c object to get the checkbox field that you want. eg [SELECT Id, Rebalance__c FROM Carrier_Product__c WHERE Id IN :carrierProductIdList ];

  3. Put the results of this query into a Map<Id,Carrier_Product__c>

  4. Loop Trigger.new and use the product id to get the correct carrier product out of the map.

  5. Examine the value and set/unset the checkbox.

  6. This should be a before trigger, so you don't need to update.

1
  • I ended up using a flow, but this was great at teaching how it could have been done with a trigger. Commented Feb 19, 2022 at 19:14
1

you cannot get relationships value in trigger.new . p.Carrier_Product__r.Rebalance__c will not return correct value.

use after insert instead of before insert and iterate over records using soql. finally update records using dml.

something like that (just a sample code. did not tested it. you might also want to include recursive check.)

trigger ShellPolicyRebalance on Quote_Policy__c(after insert) {
  List<Quote_Policy__c> recordsToUpdate = new List<Quote_Policy__c>();
  for (Quote_Policy__c p: [select id,recordtypeid,Carrier_Product__r.Rebalance__c,Renewal_Flow__c  from Quote_Policy__c   where id in :Trigger.new) {
    if (p.RecordTypeId == Schema.SObjectType.Quote_Policy__c.getRecordTypeInfosByName().get('Shell Policy').getRecordTypeId()) {
      if (p.Carrier_Product__r.Rebalance__c) {
        p.Renewal_Flow__c = 'Rebalance';
        recordsToUpdate.add(p);
      }
    }
  }

if(recordsToUpdate.size()>0)
  update recordsToUpdate ;
}

on a side note, your problem statement is more suited for flow. flow will be a better option than trigger.

3
  • 1
    If Carrier_Product__c is populated, then it's still possible to use a before insert trigger here (by using the steps outlined by Caspar). This would remove the need for an extra DML call at the end.
    – Derek F
    Commented Feb 18, 2022 at 0:50
  • 1
    Also, there is no need to check if a list is empty before performing DML. Salesforce does that sanity check for us, and is even kind enough to not count it against the DML operations governor limit if the list is empty.
    – Derek F
    Commented Feb 18, 2022 at 0:52
  • @Derek F. agreed. it can be done in before insert as outlined by casper. before insert approach using map did not clicked me while answering. appreciate your feedback. Commented Feb 18, 2022 at 16:22

You must log in to answer this question.

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