function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chris SecristChris Secrist 

I have a credit request object and it can have multiple credit lines. I am trying to write a trigger that will update a field in the credit request object if certain values are met in the credit lines

Please help, trying to finish a project and the latest request has stalled the project.  Trying to update the approval field in the credit request based on credit lines.  If a credit line has a certain qty or part, then the approval field should update to a check box.  Any support will be great.  New to salesforce and I believe a trigger is needed, any guidance or actual trigger will be wonderful.  A single request can have multiple credit lines.Image showing credit request and credit lines.
HARSHIL U PARIKHHARSHIL U PARIKH
I am not too clear when you say "If a credit line has a certain qty or part, then the approval field should update to a check box."

do you mean when the quantity is more than 2 + 1 + 1 = 4 then the Approval_Required__c checkbox should be true?

If yes, then try using the below trigger and it would work for all conditions such as Insert, Update, Delete, and UnDelete.

If not, then please tell the requirement in depth and I will make chnage in trigger accordingly.
 
Trigger MakingApprovalRequired Credit_Lines__c(After Insert, After Update, After Delete, After UnDelete){
    
    List<Id> parentIds = New List<Id>
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        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>();
    Integer I = 0;
    
    For(Credit_Request__c crdReq : [Select Id, Approval_Required__c,
                                            (Select Id, Quantity__c FROM Credit_Lines__r) FROM Credit_Request__c 
                                                            WHERE Id =: parentIds])
    {
        I = 0;
        For(Credit_Lines__c EveryChild : crdReq.Credit_Lines__r){
            I += EveryChild.QUantity__c
        }
        If(I > 3)
        {
            crdReq.Approval_Required__c = TRUE;
            parentFinalList.add(crdReq );
        }
        
    }
    try{
        If(!parentFinalList.IsEmpty()){
            update parentFinalList;
        }
    }
    Catch(Exception e){
        System.debug('Exception Thrown For MakingApprovalRequired Is:: ' + e.getMessage());
    }
}

If this answer solves the query then please mark it as best answer!
Chris SecristChris Secrist
That is great, it is so close to what I was looking for.  This is the formula I tried to use in the approval process, but since it did not look at individual credit lines, it would not succeed.  If this trigger is based on either of these conditions, then it should be perfect.
 
IF((Credit_Line_Product__r.Product__r.ProductCode = '100-171-800' && Credit_Line_Product__r.Quantity__c >= 0.5)|| (Credit_Line_Product__r.Product__r.ProductCode = '101-008-000' && Credit_Line_Product__r.Quantity__c > 0.0), True, False)

Credit line product code is based on the product name
HARSHIL U PARIKHHARSHIL U PARIKH
Hi chirs,

I am not sure the about the relationships of your objects here.
Let me know how the below trigger goes? I have taken in account for the condition you have stated inside the IF statements.

Trigger Code:
 
Trigger MakingApprovalRequired Credit_Lines__c(After Insert, After Update, After Delete, After UnDelete){
    
    List<Id> parentIds = New List<Id>
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        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,
                                            (Select Id, Quantity__c, Product__c FROM Credit_Lines__r) 
                                                                        FROM Credit_Request__c 
                                                                                WHERE Id =: parentIds])
    {
        d = 0.00;
        List<String> ProductCodes = New List<String>();

        For(Credit_Lines__c EveryChild : crdReq.Credit_Lines__r)
        {
            d += EveryChild.QUantity__c;
            ProductCodes.add(String.valueOf(EveryChild.Product__c));
        }
         
         
            If(d >= 0.5 && ProductCodes.Contains('100-171-800'))
            {
                crdReq.Approval_Required__c = TRUE;
                parentFinalList.add(crdReq );
            }
            else If(d > 0.00 && ProductCodes.Contains('101-008-000'))
            {
                crdreq.Approval_Required__c = TRUE;
                parentFinalList.add(crdReq );
            }
        
    }
    try{
        If(!parentFinalList.IsEmpty()){
            update parentFinalList;
        }
    }
    Catch(Exception e){
        System.debug('Exception Thrown For MakingApprovalRequired Is:: ' + e.getMessage());
    }
}

Hope it helps!
Chris SecristChris Secrist
Thanks been trying to get it to work with my limited understanding, but the develop console has the following errors.  Copied and paste the code but see the problem listed below.


Problem log
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Chris,

Please send a screen shot of your all associated objects (e.g., Credit_Lines__c, Credit_Request__c, Product etc..) with fields.
Chris SecristChris Secrist
Credit Request object [cid:image001.png@01D30C3C.944FB8A0] Credit Line Object [cid:image002.png@01D30C3C.944FB8A0]
Chris SecristChris Secrist
Credit LIne Field
Chris SecristChris Secrist
creditrequest
Chris SecristChris Secrist
I was able to get rid of the problems listed in the developer console, but unfortunately the trigger is not updating the checkbox.

trigger MakingApprovalRequiredTrigger on Credit_Lines__c (before insert) {
   
    List<Credit_lines__c> parentIds = List<credit_lines__c>>();
         
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Credit_Lines__c crdLine : Trigger.New){
            If(crdLine.Credit_Request__c != Null){
                parentIds(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,
                                            (Select Id, Quantity__c, Product__c FROM Credit_Lines__r)
                                                                        FROM Credit_Request__c
                                                                                WHERE Id =: parentIds])
    {
        d = 0.00;
        List<String> ProductCodes = New List<String>();

        For(Credit_Lines__c EveryChild : crdReq.Credit_Lines__r)
        {
            d += EveryChild.QUantity__c;
            ProductCodes == EveryChild.Product__c;
        }
        
        
            If(d >= 0.5 && EveryChild.Product__c == '100-171-800')
            {
                crdReq.Approval_Required__c = TRUE;
                parentFinalList.add(crdReq );
            }
            else If(d > 0.00 && ProductCodes.Contains('101-008-000'))
            {
                crdreq.Approval_Required__c = TRUE;
                parentFinalList.add(crdReq );
            }
       
    }
    try{
        If(!parentFinalList.IsEmpty()){
            update parentFinalList;
        }
    }
    Catch(Exception e){
        System.debug('Exception Thrown For MakingApprovalRequired Is:: ' + e.getMessage());
    }
}
}
Chris SecristChris Secrist
Got it to work, thanks so much
Chris SecristChris Secrist
Is there a way to put that trigger in a class?