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
Poorna DeveloperPoorna Developer 

Apex Trigger - Matrix Search

Hi, 
I have a decision_matrix__c table with following fields.
Age_of_Vehicle__c,
Max_Term__c,
Vehicle_Type__c by using Age_of_Vehicle__c and Vehicle_Type__c the Max_Term__c will be calculated automatically.
ie. Vechicle_Type='Bike', Age_of_Vehicle__c>=4 then Max_Term__c should be less than 5years.

And, I have another table named 'Vehicle_Application'.
In the 'Vehicle_Application' have fields called - Type_Of_Vehicle and Age_of_vehicle. By given these two values, I need to validate the Term__c field data fetch from 'decision_matrix__c -> Max_Term__c'.
ie. Age_of_vehicle = 3 and Vechicle_Type='bike' and Term__c should be less than which is given in the 'decision_matrix__c -> Max_Term__c'.
(Matrix Search)
Is there any idea?
Thanks.

Need Trigger to validation.
Best Answer chosen by Poorna Developer
Sandesh D GanjareSandesh D Ganjare
Hi Poorna,

Considering these two objects don't have any relationship between them. Following the trigger code will help you to achieve your goal. 

trigger myTrigger on Vehicle_Application__C (before insert, before update) {
    List<decision_matrix__c> decision_matrix = [Select Age_of_Vehicle__c,Vehicle_Type__c,Max_Term__c From decision_matrix__c Limit 1000 ];
    for(Vehicle_Application__C app : Trigger.new){
            for(decision_matrix__c matrix : decision_matrix){
                    if(matrix.Age_of_Vehicle__c= app.Age_of_vehicle__c && matrix.Vehicle_Type__c = Type_Of_Vehicle__C){
                        if(matrix.Max_Term__c>app.Term__c){
                            app.addError('Term__c should be less than which is given in the decision_matrix__c -> Max_Term__c');
                        }
                    }
            }
    }
}

Mark it as best answer if it helps

All Answers

AbhishekAbhishek (Salesforce Developers) 
Please refer to the link provided:

https://www.abclearn.com/courses/salesforce/salesforce-basics/salesforce-trigger-context-variables-and-trigger-events/


For additional reference, you can check this,
https://www.forcetalks.com/salesforce-topic/what-is-matrix-of-variable-availability-to-trigger-events-in-salesforce/
https://www.forcetalks.com/salesforce-topic/how-we-can-achieve-dynamic-approval-process-in-salesforce/


It might help you.
Sandesh D GanjareSandesh D Ganjare
Hi Poorna,

Considering these two objects don't have any relationship between them. Following the trigger code will help you to achieve your goal. 

trigger myTrigger on Vehicle_Application__C (before insert, before update) {
    List<decision_matrix__c> decision_matrix = [Select Age_of_Vehicle__c,Vehicle_Type__c,Max_Term__c From decision_matrix__c Limit 1000 ];
    for(Vehicle_Application__C app : Trigger.new){
            for(decision_matrix__c matrix : decision_matrix){
                    if(matrix.Age_of_Vehicle__c= app.Age_of_vehicle__c && matrix.Vehicle_Type__c = Type_Of_Vehicle__C){
                        if(matrix.Max_Term__c>app.Term__c){
                            app.addError('Term__c should be less than which is given in the decision_matrix__c -> Max_Term__c');
                        }
                    }
            }
    }
}

Mark it as best answer if it helps
This was selected as the best answer