0

Currently I have a Credit_request object. This object has fields used to report at a high level the required items. This object has records from an object called Credit_Lines. I have a trigger that updates the Credit Request based on credit line input. This functions correctly, however, one gap I noticed is that if I manually change a field in the credit_request object and do not update credit_lines the trigger will not fire. Is there a way to force the trigger to fire if a field in the credit request section is changed?

trigger MakingApprovalRequiredTrigger on Credit_Lines__c (After Insert, After Update, After Delete){

List<Id> parentIds = new List<Id>();

If(Trigger.IsInsert || Trigger.IsUpdate){
    For(Credit_Lines__c crdLine : Trigger.New){
        If(crdLine.Credit_Request__c != Null){
            parentIds.add(crdLine.Credit_Request__c);
        }
    }
}


If(Trigger.IsDelete){
    For(Credit_Lines__c crdLine : Trigger.Old){
        If(crdLine.Credit_Request__c != Null){
            parentIds.add(crdLine.Credit_Request__c);
        }
    }
}

List<Credit_Request__c> parentFinalList = New List<Credit_Request__c>();
Double d = 0.00;

For(Credit_Request__c crdReq : [Select Id, Approval_Required__c,Reason_for_request__c,
                                        (Select Id, Quantity__c, Product__c FROM Credit_Lines__r WHERE Product__c = '01t70000004Oxl4AAC' OR Product__c = '01t3900000OMrYrAAL') 
                                                                    FROM Credit_Request__c 
                                                                            WHERE Id =: parentIds])
{
    d = 0.00;
    Set<String> ProductCodes = New Set<String>();

    For(Credit_Lines__c EveryChild : crdReq.Credit_Lines__r )
    {
        d += EveryChild.Quantity__c;
        ProductCodes.add(String.valueOf(EveryChild.Product__c));
    }
     If(crdReq.Reason_for_request__c.Contains('308 CR-Service/Troubleshooting') || crdReq.Reason_for_request__c.Contains('208 PB-Service/Troubleshooting'))

            If(d > 1.0 && ProductCodes.Contains('01t70000004Oxl4AAC'))
                {
                    crdReq.Approval_Required__c = true;
                    parentFinalList.add(crdReq );
                }
            else If(d > 1.0 && ProductCodes.Contains('01t3900000OMrYrAAL'))
                {
                    crdreq.Approval_Required__c = true;
                    parentFinalList.add(crdReq );
                }
            else
                {
                    crdreq.Approval_Required__c = false;
                    parentFinalList.add(crdReq );                
                }

    else 
        {
            If(d >= 0.5 && ProductCodes.Contains('01t70000004Oxl4AAC'))
                {
                    crdReq.Approval_Required__c = true;
                    parentFinalList.add(crdReq );
                }
            else If(d >= 0.0 && ProductCodes.Contains('01t3900000OMrYrAAL'))
                {
                    crdreq.Approval_Required__c = true;
                    parentFinalList.add(crdReq );
                }
            else
                {
                    crdreq.Approval_Required__c = false;
                    parentFinalList.add(crdReq );                
                }         
        }        


try
{
    If(!parentFinalList.IsEmpty()){
        update parentFinalList;
    }
}
Catch(Exception e){
    System.debug('Exception Thrown For MakingApprovalRequired Is:: ' + e.getMessage());
}

} }

4

You can use (what I call) a blank update to force database rules to run on an object:

trigger CreditRequestTrigger on Credit_Request__c(after update)
{
    update [select ID from Credit_Lines__c where Credit_Request__c in :Trigger.New];
}

As Derek F pointed out in comments, you should be careful when updating another object in a trigger in general, as you can cause recursion between the two objects if they can update each other. This is very easy to do with Master-Detail records, since roll-up fields cause an update to the parent, so if you update something in Credit_Lines__c which is part of a roll-up field on Credit_Request__c, you will end up calling your Credit_Request__c trigger again. You can prevent this using static variables, which will retain their value throughout the entire database transaction.

| improve this answer | |
  • 3
    One thing to be mindful of when using this (especially when you're in a trigger on a Parent object, and want to update child records) is recursion. If your child object also has code that updates the parent object (or a rollup summary field), you would get yourself into an infinite loop without some recursion control. – Derek F Aug 4 '17 at 12:49
4

No, triggers are object specific, you can see, the first line of code gives the trigger a name, specifies the object on which it operates, and defines the events that cause it to fire:

trigger TriggerName on ObjectName (trigger_events) {
   code_block
}

If you want, you can vote on an existing idea for supporting multiple objects.

Additionally, in the apex triggers best practices guide, you can read further details details on trigger best practices.

| improve this answer | |
  • Thanks for the input, we have limited resources. To do this coding. Is there a easy way to covert a trigger into a class. Updated to include code – Chris Secrist Aug 4 '17 at 14:29
  • I would suggest creating a new post for this rather than modifying the existing one. – glls Aug 4 '17 at 14:41
  • 1
    Created new post Convert a trigger to a class to clean up the code. – Chris Secrist Aug 4 '17 at 14:50
0

If you want a trigger to fire when either object A or Object B is updated, then consider using the new platform events feature:

  1. define the event - its like a custom object - you can define whatever fields you want on the event.

  2. Create workflow, or a trigger, to create the platform events (workflow off of Object A, and another on Object B)

  3. Create a trigger on the event - this trigger will fire whenever the event is created.

Since both Object A and Object B are creating events - the trigger will effectively fire for 2 objects.

Here is a trailhead with some trigger code examples: https://trailhead.salesforce.com/en/modules/platform_events_basics/units/platform_events_architecture

| improve this answer | |

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.