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());
}
} }