You need to sign in to do that
Don't have an account?
Chris 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.
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.
If this answer solves the query then please mark it as best answer!
Credit line product code is based on the product name
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:
Hope it helps!
Please send a screen shot of your all associated objects (e.g., Credit_Lines__c, Credit_Request__c, Product etc..) with fields.
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());
}
}
}