Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. global without sharing class XXX_SSM13_ReCloseCheckBatch implements Database.Batchable<SObject>, Database.Stateful {
  2.     global final static Set<String> fields2Check = new Set<String>{'MRC__c','CurrencyISOCode','Commited_Bandwidth__c',
  3.             'Recommended_Base_Unit_Price__c','Recommended_Burst_Price__c'};
  4.     global final static string query =
  5.             'SELECT Id,MRC__c,CurrencyISOCode,Commited_Bandwidth__c,Recommended_Base_Unit_Price__c,' +
  6.                     'Recommended_Burst_Price__c, ' +
  7.                     '(SELECT Id,MRC__c,CurrencyISOCode,Commited_Bandwidth__c,Recommended_Base_Unit_Price__c,' +
  8.                     'Recommended_Burst_Price__c, TECH_Definition_JSON__c ' +
  9.                     'FROM Sold_Service_Modifications__r ' +
  10.                     'WHERE Stage__c = \''+ApplicationConstant.SSM_STAGE_LIVE+'\' AND Action__c != \'Add\' ' +
  11.                     'ORDER BY CreatedDate DESC LIMIT 1) FROM Sold_Service__c ' +
  12.                     'WHERE Stage__c = \'Live Service\' AND Status__c = \'Active\'';
  13.  
  14.     global Integer totalCorruptedNo = 0;
  15.     global Integer totalCorruptedJSONNo = 0;
  16.     global Set<Id> corruptedSSMs = new Set<Id>();
  17.     global Set<Id> corruptedSSMJSON = new Set<Id>();
  18.     Map<Id,List<String>> fieldDiff = new Map<Id, List<String>>();
  19.  
  20.     global Database.QueryLocator start(Database.BatchableContext context) {
  21.         return  Database.getQueryLocator(query);
  22.     }
  23.  
  24.     global void execute(Database.BatchableContext context, List<Sold_Service__c> scope) {
  25.         for(Sold_Service__c ss:scope) {
  26.             Sold_Service_Modification__c ssm;
  27.             if(ss.Sold_Service_Modifications__r.size() > 0) {
  28.                 ssm = ss.Sold_Service_Modifications__r[0];
  29.             }
  30.  
  31.             if(ssm != null && areFieldsDifferent(ss,ssm) && ssm.TECH_Definition_JSON__c != null) {
  32.                 totalCorruptedNo++;
  33.                 corruptedSSMs.add(ssm.Id);
  34.             }
  35.  
  36.             if(ssm != null && ssm.TECH_Definition_JSON__c == null) {
  37.                 totalCorruptedJSONNo++;
  38.                 corruptedSSMJSON.add(ssm.Id);
  39.  
  40.                 String changedFields = '';
  41.                 String modDetails = '';
  42.                 Schema.DescribeSObjectResult obj = Schema.getGlobalDescribe().get('Sold_Service__c').getDescribe();
  43.                 Map<String, Schema.SObjectField> schemaFieldMap = obj.fields.getMap();
  44.                 if(newSS.Item_Action__c != CPQ_PRD04_SoldServiceMappingHlpr.ACTION_ADD)
  45.                     for(String fieldName : schemaFieldMap.keySet()){
  46.                         if(newSS.get(fieldName) != null && !ApplicationConstant.SOLDSERVICE_FIELDS_EXCLUDE.contains(fieldName) && areFieldsDifferent(ss, ssm)) {
  47.                             ssResult.put(obj.Fields.getMap().get(fieldName).getDescribe().getName(), ssm.get(fieldName));
  48.                         }
  49.                     }
  50.                 }
  51.  
  52.                 ssm.TECH_Definition_JSON__c = JSON.Serialize(ssResult);
  53.                 return result;
  54.  
  55.  
  56.             }
  57.  
  58.         }
  59.     }
  60.  
  61.     private Boolean areFieldsDifferent(Sold_Service__c ss,Sold_Service_Modification__c ssm) {
  62.         Boolean different = false;
  63.         for(String fieldName : fields2Check) {
  64.             if(ss.get(fieldName) != ssm.get(fieldName)) {
  65.                 different = true;
  66.                 if(fieldDiff.get(ssm.Id) == null)
  67.                     fieldDiff.put(ssm.Id,new List<String>{fieldName});
  68.                 else
  69.                     fieldDiff.get(ssm.Id).add(fieldName);
  70.             }
  71.         }
  72.  
  73.         return different;
  74.     }
  75.  
  76.     global void finish(Database.BatchableContext BC) {
  77.         AsyncApexJob a = [Select Id, Status,ExtendedStatus,NumberOfErrors,     JobItemsProcessed,
  78.                 TotalJobItems, CreatedBy.Email
  79.         from AsyncApexJob where Id =:BC.getJobId()];
  80.  
  81.         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  82.         String[] toAddresses = new String[] {a.CreatedBy.Email};
  83.         mail.setToAddresses(toAddresses);
  84.         mail.setSubject('Match Merge Batch ' + a.Status);
  85.         String body = 'batches processed: ' + a.TotalJobItems +
  86.                 ' with: '+ a.NumberOfErrors + ' failures. '+totalCorruptedNo +
  87.                 ' corrupted SSMs found. Corrupted SSMs: \n';
  88.         for(Id ssmId : fieldDiff.keyset()){
  89.             body+= ssmId + ' \n Field Difference: \n';
  90.             for(String ssmField : fieldDiff.get(ssmId)){
  91.                 body+= '\t ' + ssmField;
  92.             }
  93.             body+= '\n';
  94.  
  95.         }
  96.         body+= '. \n ' + totalCorruptedJSONNo +
  97.                 ' corrupted SSMs  with no JSON found. Corrupted SSMs: \n'+CSP_REST_Common.idSet2Str(corruptedSSMJSON);
  98.         mail.setPlainTextBody(body);
  99.         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement