Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public with sharing class InspectionQuotasHandler{
- public static void UpdateMonthlyQuotaOfInspection(List<Inspection__c> InspectionQuotasList){
- Set<id> AccountId = new Set<Id>();
- for (Inspection__c i : (List<Inspection__c>)Trigger.new) {
- AccountId.add(i.Franchise__c);
- }
- Map<String,map<String,Id>> keyByLookupMap = new Map<String,map<String,Id>>();
- for(Monthly_Quota__c mq : [SELECT Id, Name,Account__c
- FROM Monthly_Quota__c
- WHERE Account__c = :AccountId]){
- if(!keyByLookupMap.containsKey(mq.Account__c))
- keyByLookupMap.put(mq.Account__c, new map<String,Id>());
- keyByLookupMap.get(mq.Account__c).put(mq.Name,mq.Id);
- }
- if(!keyByLookupMap.isEmpty()){
- for(Inspection__c i : (List<Inspection__c>)trigger.new){
- if(i.Inspection_Date__c != null && keyByLookupMap.containsKey(mq.Account__c) && keyByLookupMap.get(mq.Account__c).containsKey(convertMonthToWord(i.Inspection_Date__c.month()))){
- if(i.Status__c == 'Completed' && i.Month_Since_Launch__c > 0){
- i.Monthly_Quota__c = keyByLookupMap.get(mq.Account__c).get(convertMonthToWord(i.Inspection_Date__c.month()));
- }
- }
- }
- }
- }
- public static String convertMonthToWord(Integer monthIndex){
- if(monthIndex==1){
- return 'January';
- }else if(monthIndex==2){
- return 'February';
- }else if(monthIndex==3){
- return 'March';
- }else if(monthIndex==4){
- return 'April';
- }else if(monthIndex==5){
- return 'May';
- }else if(monthIndex==6){
- return 'June';
- }else if(monthIndex==7){
- return 'July';
- }else if(monthIndex==8){
- return 'August';
- }else if(monthIndex==9){
- return 'September';
- }else if(monthIndex==10){
- return 'October';
- }else if(monthIndex==11){
- return 'November';
- }else if(monthIndex==12){
- return 'December';
- }else{
- return null;
- }
- }
Add Comment
Please, Sign In to add comment