• sam
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 7
    Questions
  • 2
    Replies
 public static DateTime addDays(DateTime input, integer days, string timezoneSidId) {
        DateTime result = input.addDays(days);
        Timezone tz = Timezone.getTimezone(timezoneSidId);
        integer inputOffset = tz.getOffset(input) / 60000;
        integer resultOffset = tz.getOffset(result) / 60000;
        result = result.addMinutes(inputOffset - resultOffset);

        return result;
    }

    public static DateTime addMonths(DateTime input, integer months, string timezoneSidId) {
        DateTime result = input.addMonths(months);
        Timezone tz = Timezone.getTimezone(timezoneSidId);
        integer inputOffset = tz.getOffset(input) / 60000;
        integer resultOffset = tz.getOffset(result) / 60000;
        result = result.addMinutes(inputOffset - resultOffset);

        return result;
    }

    public static DateTime addYears(DateTime input, integer years, string timezoneSidId) {
        DateTime result = input.addYears(years);
        Timezone tz = Timezone.getTimezone(timezoneSidId);
        integer inputOffset = tz.getOffset(input) / 60000;
        integer resultOffset = tz.getOffset(result) / 60000;
        result = result.addMinutes(inputOffset - resultOffset);

        return result;
    }

    public static Date getDate(DateTime input, string timezoneSidId) {
        string dateIsoString = input.format(DATE_ISO_FORMAT, timezoneSidId);
        return getDateFromIsoString(dateIsoString);
    }

    // isoString should have the format yyyy-MM-dd
    public static Date getDateFromIsoString(string isoString) {
        return (Date)Json.deserialize('"' + isoString + '"', Date.class);
    }

    public static DateTime getDateTimeFromIsoString(string isoString) {
        return (DateTime)Json.deserialize('"' + isoString + '"', DateTime.class);
    }

    // Get 12:00:00 am from the input isoString date
    public static DateTime getStartOfDate(String isoString, string timezoneSidId) {
        return getStartOfDate(getDateFromIsoString(isoString), timezoneSidId);
        
    }
 
  • July 12, 2022
  • Like
  • 0
 public static void sendContractExpiryEmail(List<Contract> contractList,Map<Id,Contract> oldContractMap){
        Set<Id> accIds = new Set<Id>();
        List<Account> sentemailAccounts = new List<Account>();
        List<Account> accs2Update = new List<Account>();
        for(Contract con : contractList){
            Contract oldCon = oldContractMap.get(con.Id);
            Decimal consumedPercent;
            if(con.Adjusted_Contract_Amount__c != null
               &&con.Consumed_Amount__c != null
               && con.Adjusted_Contract_Amount__c >0 ){
                   consumedPercent = (con.Consumed_Amount__c / con.Adjusted_Contract_Amount__c)*100; 
               }
            if(con.Business_Type__c == 'ACR'
               && con.Contracted_ACR_Package__c != null
               && consumedPercent >= Decimal.valueOf(Label.ContractExpiryEmailThresholdPercent)
               && consumedPercent != null){
                   accIds.add(con.AccountId);
               }
        }
        Boolean expiryByDate = false;
        
        if(accIds.size() >0){
            for(Account acc : [SELECT AccountNumber,Name,Balance_Amount__c,Point_Of_Contact_Email__c,
                                   Annual_Contract_Balance__c,Active_Contract__c,Consumed_ACR_Amount__c,
                                   Active_Contract__r.of_Contract_Consumption__c,Active_Contract__r.Consumed_Amount__c,
                                   Adjusted_Contract_Amount__c,Contract_Start_Date__c,Contract_Amount__c,
                                   Consumed_Amount__c,Contract_End_Date__c,ST_Total_Consumable_ACR_Amount__c,
                                   (SELECT ContactId,AccountId,Contact.Email,Roles FROM AccountContactRelations WHERE Roles includes ('Finance','Invoice Recepient')),
                                   Email_Sent_at_85_Consumption__c,Email_Sent_at_90_Consumption__c,Email_Sent_at_95_Consumption__c,
                                   Email_Sent_at_98_Consumption__c,Email_Sent_at_100_Consumption__c,of_Contract_Consumption__c
                                   FROM Account 
                                   WHERE Id IN :accIds
                                   AND Contracted_ACR_Package__c != null] ){
                                   if(acc.AccountContactRelations.size() >0){
                                       
                                       if(acc.Active_Contract__r.of_Contract_Consumption__c >=85 && acc.Active_Contract__r.of_Contract_Consumption__c <90 && !acc.Email_Sent_at_85_Consumption__c){
                                           sentemailAccounts.add(acc);
                                           acc.Email_Sent_at_85_Consumption__c = true;
                                           accs2Update.add(acc);
                                       }
                                       
                                       else if(acc.Active_Contract__r.of_Contract_Consumption__c >=90 
                                               && acc.Active_Contract__r.of_Contract_Consumption__c <95 
                                               && !acc.Email_Sent_at_90_Consumption__c){
                                                   sentemailAccounts.add(acc);
                                                   acc.Email_Sent_at_90_Consumption__c = true;
                                                   accs2Update.add(acc);
                                               }
                                       
                                       else if(acc.Active_Contract__r.of_Contract_Consumption__c >=95 
                                               && acc.Active_Contract__r.of_Contract_Consumption__c <98 
                                               && !acc.Email_Sent_at_95_Consumption__c){
                                                   sentemailAccounts.add(acc);
                                                   acc.Email_Sent_at_95_Consumption__c = true;
                                                   accs2Update.add(acc);
                                               }
                                       
                                       else if(acc.Active_Contract__r.of_Contract_Consumption__c >=98 
                                               && acc.Active_Contract__r.of_Contract_Consumption__c <100 
                                               && !acc.Email_Sent_at_98_Consumption__c){
                                                   system.debug('Account 95-98'+acc.name);
                                                   sentemailAccounts.add(acc);
                                                   acc.Email_Sent_at_98_Consumption__c = true;
                                                   accs2Update.add(acc);
                                               }
                                       
                                       else if(acc.Active_Contract__r.of_Contract_Consumption__c >=100 
                                               && !acc.Email_Sent_at_100_Consumption__c){
                                                   sentemailAccounts.add(acc);
                                                   acc.Email_Sent_at_100_Consumption__c = true;
                                                   accs2Update.add(acc);
                                               }
                                       
                                   }
                               }
        }
        if(accs2Update.size() > 0){
            try{
                update accs2Update;
            }catch(Exception ex){
                system.debug('Insert Failed for Consumption Account:::'+ex.getMessage());
            }
        }
        if(sentemailAccounts.size() > 0){
            List<Messaging.SingleEmailMessage> mailingList = SendEmailUtility.ContractExpiryEmailSender(Label.ContractConsumptionExpiryEmailTemplateName ,sentemailAccounts,expiryByDate);
            Messaging.SendEmailResult[] results = Messaging.sendEmail(mailingList);
        }       
    }
    
  • June 23, 2022
  • Like
  • 0
public static void sendContractExpiryEmail(List<Contract> contractList,Map<Id,Contract> oldContractMap){
        Set<Id> accIds = new Set<Id>();
        List<Account> sentemailAccounts = new List<Account>();
        List<Account> accs2Update = new List<Account>();
        for(Contract con : contractList){
            Contract oldCon = oldContractMap.get(con.Id);
            Decimal consumedPercent;
            if(con.Adjusted_Contract_Amount__c != null
               &&con.Consumed_Amount__c != null
               && con.Adjusted_Contract_Amount__c >0 ){
                   consumedPercent = (con.Consumed_Amount__c / con.Adjusted_Contract_Amount__c)*100; 
               }
            if(con.Business_Type__c == 'ACR'
               && con.Contracted_ACR_Package__c != null
               && consumedPercent >= Decimal.valueOf(Label.ContractExpiryEmailThresholdPercent)
               && consumedPercent != null){
                   accIds.add(con.AccountId);
               }
        }
        Boolean expiryByDate = false;
        
        if(accIds.size() >0){
            for(Account acc : [SELECT AccountNumber,Name,Balance_Amount__c,Point_Of_Contact_Email__c,
                                   Annual_Contract_Balance__c,Active_Contract__c,Consumed_ACR_Amount__c,
                                   Active_Contract__r.of_Contract_Consumption__c,Active_Contract__r.Consumed_Amount__c,
                                   Adjusted_Contract_Amount__c,Contract_Start_Date__c,Contract_Amount__c,
                                   Consumed_Amount__c,Contract_End_Date__c,ST_Total_Consumable_ACR_Amount__c,
                                   (SELECT ContactId,AccountId,Contact.Email,Roles FROM AccountContactRelations WHERE Roles includes ('Finance','Invoice Recepient')),
                                   Email_Sent_at_85_Consumption__c,Email_Sent_at_90_Consumption__c,Email_Sent_at_95_Consumption__c,
                                   Email_Sent_at_98_Consumption__c,Email_Sent_at_100_Consumption__c,of_Contract_Consumption__c
                                   FROM Account 
                                   WHERE Id IN :accIds
                                   AND Contracted_ACR_Package__c != null] ){
                                   if(acc.AccountContactRelations.size() >0){
                                       
                                       if(acc.Active_Contract__r.of_Contract_Consumption__c >=85 && acc.Active_Contract__r.of_Contract_Consumption__c <90 && !acc.Email_Sent_at_85_Consumption__c){
                                           sentemailAccounts.add(acc);
                                           acc.Email_Sent_at_85_Consumption__c = true;
                                           accs2Update.add(acc);
                                       }
                                       
                                       else if(acc.Active_Contract__r.of_Contract_Consumption__c >=90 
                                               && acc.Active_Contract__r.of_Contract_Consumption__c <95 
                                               && !acc.Email_Sent_at_90_Consumption__c){
                                                   sentemailAccounts.add(acc);
                                                   acc.Email_Sent_at_90_Consumption__c = true;
                                                   accs2Update.add(acc);
                                               }
                                       
                                       else if(acc.Active_Contract__r.of_Contract_Consumption__c >=95 
                                               && acc.Active_Contract__r.of_Contract_Consumption__c <98 
                                               && !acc.Email_Sent_at_95_Consumption__c){
                                                   sentemailAccounts.add(acc);
                                                   acc.Email_Sent_at_95_Consumption__c = true;
                                                   accs2Update.add(acc);
                                               }
                                       
                                       else if(acc.Active_Contract__r.of_Contract_Consumption__c >=98 
                                               && acc.Active_Contract__r.of_Contract_Consumption__c <100 
                                               && !acc.Email_Sent_at_98_Consumption__c){
                                                   system.debug('Account 95-98'+acc.name);
                                                   sentemailAccounts.add(acc);
                                                   acc.Email_Sent_at_98_Consumption__c = true;
                                                   accs2Update.add(acc);
                                               }
                                       
                                       else if(acc.Active_Contract__r.of_Contract_Consumption__c >=100 
                                               && !acc.Email_Sent_at_100_Consumption__c){
                                                   sentemailAccounts.add(acc);
                                                   acc.Email_Sent_at_100_Consumption__c = true;
                                                   accs2Update.add(acc);
                                               }
                                       
                                   }
                               }
        }
        if(accs2Update.size() > 0){
            try{
                update accs2Update;
            }catch(Exception ex){
                system.debug('Insert Failed for Consumption Account:::'+ex.getMessage());
            }
        }
        if(sentemailAccounts.size() > 0){
            List<Messaging.SingleEmailMessage> mailingList = SendEmailUtility.ContractExpiryEmailSender(Label.ContractConsumptionExpiryEmailTemplateName ,sentemailAccounts,expiryByDate);
            Messaging.SendEmailResult[] results = Messaging.sendEmail(mailingList);
        }
        
        
    }
    
    public static void populateinvoice(List<Contract> contractList){
        Map<Id,Contract> orderContractMap = new Map<Id,Contract>();
        Map<Id,blng__Invoice__c> orderInvoiceMap = new Map<Id,blng__Invoice__c>();
        for(Contract con : contractList)
        {
            if(con.Business_Type__c == 'ACR'){
            orderContractMap.put(con.SBQQ__Order__c, con);
            }
        }
        if(!orderContractMap.isEmpty() && orderContractMap.size() > 0)
        {
            for(blng__Invoice__c inv : [Select Id, blng__Order__c, Name
                                        From blng__Invoice__c 
                                        Where blng__Order__c In :orderContractMap.keySet() ])
            {
                orderInvoiceMap.put(inv.blng__Order__c, inv);
            }
            for(Contract con : contractList)
            {
                if(orderInvoiceMap.size() >0 
                   && orderInvoiceMap != null 
                   && con.Invoice__c == null
                   && orderInvoiceMap.containsKey(con.SBQQ__Order__c)){
                       con.Invoice__c = orderInvoiceMap.get(con.SBQQ__Order__c).Id;
                   }
                system.debug('invoice number : '+con.Invoice__c);
            }
            
        }
        
    }
  • June 22, 2022
  • Like
  • 0
public static void createExhibitionContractedPrice(Set<Id> contractIdSet, Map<Id,Contract> newContractMap){
        system.debug('===createExhibitionContractedPrice called');
        Map<Id, Account> accMap = new Map<Id, Account>();
        Map<Id,Set<Id>> customerExhiMap = new Map<Id,Set<Id>>();
        List<ST_Exhibition_Contracted_Price__c> exhibitionContractedPriceList = new List<ST_Exhibition_Contracted_Price__c>();
        Set<Id> deactivatePrevCont = new Set<Id>();
        Account acc = new Account();
        
        
        for(SBQQ__Subscription__c subs : [select id, SBQQ__ProductId__c, SBQQ__Contract__r.AccountId, SBQQ__Contract__r.Account.Active_Exhibition_Contract__c,ST_Customer_Product_Category__c, ST_Exhibition__c,ST_Liability_Coverage__c,
                                          ST_Offline_Charge__c, ST_Rate_Amount__c, ST_Rate_UOM__c, SBQQ__Account__c, SBQQ__EndDate__c, SBQQ__StartDate__c, SBQQ__QuoteLine__r.SBQQ__Quote__r.Quote_For__c,
                                          SBQQ__Account__r.Active_Contract__c, SBQQ__Contract__c,Exhibition_Standard_Price__r.Level_1__c,Exhibition_Standard_Price__r.Level_2__c,Exhibition_Standard_Price__r.Level_3__c,
                                          Slab_Level_1_Rate__c,Slab_Level_2_Rate__c,Slab_Level_3_Rate__c,Slab_Level_4_Rate__c,Slab_Level_5_Rate__c,Max_Slab_Rate__c,
                                          Exhibition_Standard_Price__r.Level_4__c,Exhibition_Standard_Price__r.Level_5__c
                                          FROM SBQQ__Subscription__c WHERE SBQQ__Contract__c=:contractIdSet ])
        {
            system.debug('===SBQQ__Subscription__c record : '+subs.Id);
            ST_Exhibition_Contracted_Price__c exhibitionContract = new ST_Exhibition_Contracted_Price__c();
            exhibitionContract.ST_Active__c = true;
            exhibitionContract.ST_Contract__c = subs.SBQQ__Contract__c;
            exhibitionContract.ST_Customer_Account__c = subs.SBQQ__Account__c;
            exhibitionContract.ST_Customer_Product_Category__c = subs.ST_Customer_Product_Category__c;
            exhibitionContract.ST_End_Date__c = subs.SBQQ__EndDate__c;
            exhibitionContract.ST_Exhibition__c = subs.ST_Exhibition__c;
            exhibitionContract.ST_Liability_Coverage__c = subs.ST_Liability_Coverage__c;
            exhibitionContract.ST_Offline_Charge__c = subs.ST_Offline_Charge__c;
            exhibitionContract.ST_Product__c = subs.SBQQ__ProductId__c;
            exhibitionContract.ST_Rate_UOM__c = subs.ST_Rate_UOM__c;
            exhibitionContract.Level_1_Rate__c = subs.Slab_Level_1_Rate__c;
            exhibitionContract.Level_2_Rate__c = subs.Slab_Level_2_Rate__c;
            exhibitionContract.Level_3_Rate__c = subs.Slab_Level_3_Rate__c;
            exhibitionContract.Level_4_Rate__c = subs.Slab_Level_4_Rate__c;
            exhibitionContract.Level_5_Rate__c = subs.Slab_Level_5_Rate__c;
            exhibitionContract.Max_Slab_Rate__c = subs.Max_Slab_Rate__c;
            exhibitionContract.ST_Start_Date__c = subs.SBQQ__StartDate__c;
            exhibitionContract.Level_1__c = subs.Exhibition_Standard_Price__r.Level_1__c;
            exhibitionContract.Level_2__c = subs.Exhibition_Standard_Price__r.Level_2__c;
            exhibitionContract.Level_3__c = subs.Exhibition_Standard_Price__r.Level_3__c;
            exhibitionContract.Level_4__c = subs.Exhibition_Standard_Price__r.Level_4__c;
            exhibitionContract.Level_5__c = subs.Exhibition_Standard_Price__r.Level_5__c;
            exhibitionContractedPriceList.add(exhibitionContract);
            if(customerExhiMap.containsKey(subs.SBQQ__Account__c)){
                customerExhiMap.get(subs.SBQQ__Account__c).add(subs.ST_Exhibition__c);
            }
            else{
                Set<Id> exiId = new Set<Id>{subs.ST_Exhibition__c};
                    customerExhiMap.put(subs.SBQQ__Account__c,exiId);
            }
        }
        
        try{
            if(!exhibitionContractedPriceList.isEmpty()){
                system.debug('===exhibitionContractedPriceList size : '+exhibitionContractedPriceList.size());
                insert exhibitionContractedPriceList;
            }
            
            if(!accMap.isEmpty()){
                system.debug('===acc details : '+accMap.values());
                update accMap.values();
            }            
        }
        catch(Exception ex)
        {
            system.debug('===Error occurred : '+ex.getStackTraceString()+', Error Message : '+ex.getMessage()+', Line Number : '+ex.getLineNumber());            
        }
        if(customerExhiMap.size() > 0 && customerExhiMap != null){
            Set<Id> existingContractIds = getExistingContractIds(customerExhiMap,newContractMap);
            if(existingContractIds.size() > 0 && existingContractIds != null){
                deactivateExhibitionPrice(existingContractIds,'DeactivationDueToNewContract');
                contractExpiration(existingContractIds);
            }
            
        }
        
    }
    
    public static void acrContractedPriceDeactivation(Map<Id,Contract> contractMap, String deactivationEvent){
        system.debug('CONT:::acrContractedPriceDeactivation');
        Set<Id> accIds = new Set<Id>();
        Map<Id,Id> orderContractIdMap = new Map<Id,Id>();
        for(Contract c : contractMap.values()){
            accIds.add(c.AccountId);
            orderContractIdMap.put(c.SBQQ__Order__c,c.Id);
        }
        List<ST_ACR_Contracted_Price__c> acrContractPrices2Update = new List<ST_ACR_Contracted_Price__c>();
        for(ST_ACR_Contracted_Price__c price : [SELECT Id,ST_Order__c,ST_Active__c,ST_Deactivation_Date__c,
                                                ST_Product__c,CreatedDate,ST_Contract__c 
                                                FROM ST_ACR_Contracted_Price__c
                                                WHERE ST_Customer_Account__c IN :accIds
                                                ORDER BY CreatedDate desc 
                                               ])
        {
            
            if(price.ST_Active__c && deactivationEvent == 'DeactivationDueToNewContract'
               && (!contractMap.containsKey(price.ST_Contract__c)) 
               //&& price.ST_Contract__c != orderContractIdMap.get(price.ST_Order__c) //Adel, do we need this ?
              ) 
            {
                price.ST_Active__c = false;
                price.ST_Deactivation_Date__c = system.today();
                acrContractPrices2Update.add(price);
            }
            else if(price.ST_Active__c && deactivationEvent == 'DeactivationDueToExpiration'){
                price.ST_Active__c = false;
                price.ST_Deactivation_Date__c = system.today();
                acrContractPrices2Update.add(price);
            }
            
        }
        if(acrContractPrices2Update.size() >0){
            try {
                update acrContractPrices2Update;
            } catch (DmlException ex) {
                System.debug('The Update on contractedPriceActivation Failed Exception***'+ex.getMessage());
            }
        }
    }
    
  • June 22, 2022
  • Like
  • 0
public static void contractExpiration(Set<Id> contIds){
        
        List<Contract> contractsToBeUpdated = new List<Contract>();
        for(Contract cont : [select Id,EndDate, Status from Contract where Id =:  contIds]){
            if(cont.Status!='Expired' && cont.Status!='Cancelled'){
                cont.EndDate = system.today();
                cont.Status = 'Expired';
                contractsToBeUpdated.add(cont);  
            }
            
        }
        if(!contractsToBeUpdated.isEmpty()){
            UtilClass.isFirstTime = false;
            update contractsToBeUpdated;
        }
    }
  • June 22, 2022
  • Like
  • 0
public static void createExhibitionContractedPrice(Set<Id> contractIdSet, Map<Id,Contract> newContractMap){
        system.debug('===createExhibitionContractedPrice called');
        Map<Id, Account> accMap = new Map<Id, Account>();
        Map<Id,Set<Id>> customerExhiMap = new Map<Id,Set<Id>>();
        List<ST_Exhibition_Contracted_Price__c> exhibitionContractedPriceList = new List<ST_Exhibition_Contracted_Price__c>();
        Set<Id> deactivatePrevCont = new Set<Id>();
        Account acc = new Account();
        
        
        for(SBQQ__Subscription__c subs : [select id, SBQQ__ProductId__c, SBQQ__Contract__r.AccountId, SBQQ__Contract__r.Account.Active_Exhibition_Contract__c,ST_Customer_Product_Category__c, ST_Exhibition__c,ST_Liability_Coverage__c,
                                          ST_Offline_Charge__c, ST_Rate_Amount__c, ST_Rate_UOM__c, SBQQ__Account__c, SBQQ__EndDate__c, SBQQ__StartDate__c, SBQQ__QuoteLine__r.SBQQ__Quote__r.Quote_For__c,
                                          SBQQ__Account__r.Active_Contract__c, SBQQ__Contract__c,Exhibition_Standard_Price__r.Level_1__c,Exhibition_Standard_Price__r.Level_2__c,Exhibition_Standard_Price__r.Level_3__c,
                                          Slab_Level_1_Rate__c,Slab_Level_2_Rate__c,Slab_Level_3_Rate__c,Slab_Level_4_Rate__c,Slab_Level_5_Rate__c,Max_Slab_Rate__c,
                                          Exhibition_Standard_Price__r.Level_4__c,Exhibition_Standard_Price__r.Level_5__c
                                          FROM SBQQ__Subscription__c WHERE SBQQ__Contract__c=:contractIdSet ])
        {
            system.debug('===SBQQ__Subscription__c record : '+subs.Id);
            ST_Exhibition_Contracted_Price__c exhibitionContract = new ST_Exhibition_Contracted_Price__c();
            exhibitionContract.ST_Active__c = true;
            exhibitionContract.ST_Contract__c = subs.SBQQ__Contract__c;
            exhibitionContract.ST_Customer_Account__c = subs.SBQQ__Account__c;
            exhibitionContract.ST_Customer_Product_Category__c = subs.ST_Customer_Product_Category__c;
            exhibitionContract.ST_End_Date__c = subs.SBQQ__EndDate__c;
            exhibitionContract.ST_Exhibition__c = subs.ST_Exhibition__c;
            exhibitionContract.ST_Liability_Coverage__c = subs.ST_Liability_Coverage__c;
            exhibitionContract.ST_Offline_Charge__c = subs.ST_Offline_Charge__c;
            exhibitionContract.ST_Product__c = subs.SBQQ__ProductId__c;
            exhibitionContract.ST_Rate_UOM__c = subs.ST_Rate_UOM__c;
            exhibitionContract.Level_1_Rate__c = subs.Slab_Level_1_Rate__c;
            exhibitionContract.Level_2_Rate__c = subs.Slab_Level_2_Rate__c;
            exhibitionContract.Level_3_Rate__c = subs.Slab_Level_3_Rate__c;
            exhibitionContract.Level_4_Rate__c = subs.Slab_Level_4_Rate__c;
            exhibitionContract.Level_5_Rate__c = subs.Slab_Level_5_Rate__c;
            exhibitionContract.Max_Slab_Rate__c = subs.Max_Slab_Rate__c;
            exhibitionContract.ST_Start_Date__c = subs.SBQQ__StartDate__c;
            exhibitionContract.Level_1__c = subs.Exhibition_Standard_Price__r.Level_1__c;
            exhibitionContract.Level_2__c = subs.Exhibition_Standard_Price__r.Level_2__c;
            exhibitionContract.Level_3__c = subs.Exhibition_Standard_Price__r.Level_3__c;
            exhibitionContract.Level_4__c = subs.Exhibition_Standard_Price__r.Level_4__c;
            exhibitionContract.Level_5__c = subs.Exhibition_Standard_Price__r.Level_5__c;
            exhibitionContractedPriceList.add(exhibitionContract);
            if(customerExhiMap.containsKey(subs.SBQQ__Account__c)){
                customerExhiMap.get(subs.SBQQ__Account__c).add(subs.ST_Exhibition__c);
            }
            else{
                Set<Id> exiId = new Set<Id>{subs.ST_Exhibition__c};
                    customerExhiMap.put(subs.SBQQ__Account__c,exiId);
            }
        }
        
        try{
            if(!exhibitionContractedPriceList.isEmpty()){
                system.debug('===exhibitionContractedPriceList size : '+exhibitionContractedPriceList.size());
                insert exhibitionContractedPriceList;
            }
            
            if(!accMap.isEmpty()){
                system.debug('===acc details : '+accMap.values());
                update accMap.values();
            }            
        }
        catch(Exception ex)
        {
            system.debug('===Error occurred : '+ex.getStackTraceString()+', Error Message : '+ex.getMessage()+', Line Number : '+ex.getLineNumber());            
        }
        if(customerExhiMap.size() > 0 && customerExhiMap != null){
            Set<Id> existingContractIds = getExistingContractIds(customerExhiMap,newContractMap);
            if(existingContractIds.size() > 0 && existingContractIds != null){
                deactivateExhibitionPrice(existingContractIds,'DeactivationDueToNewContract');
                contractExpiration(existingContractIds);
            }
            
        }
        
    }
    
    public static void acrContractedPriceDeactivation(Map<Id,Contract> contractMap, String deactivationEvent){
        system.debug('CONT:::acrContractedPriceDeactivation');
        Set<Id> accIds = new Set<Id>();
        Map<Id,Id> orderContractIdMap = new Map<Id,Id>();
        for(Contract c : contractMap.values()){
            accIds.add(c.AccountId);
            orderContractIdMap.put(c.SBQQ__Order__c,c.Id);
        }
        List<ST_ACR_Contracted_Price__c> acrContractPrices2Update = new List<ST_ACR_Contracted_Price__c>();
        for(ST_ACR_Contracted_Price__c price : [SELECT Id,ST_Order__c,ST_Active__c,ST_Deactivation_Date__c,
                                                ST_Product__c,CreatedDate,ST_Contract__c 
                                                FROM ST_ACR_Contracted_Price__c
                                                WHERE ST_Customer_Account__c IN :accIds
                                                ORDER BY CreatedDate desc 
                                               ])
        {
            
            if(price.ST_Active__c && deactivationEvent == 'DeactivationDueToNewContract'
               && (!contractMap.containsKey(price.ST_Contract__c)) 
               //&& price.ST_Contract__c != orderContractIdMap.get(price.ST_Order__c) //Adel, do we need this ?
              ) 
            {
                price.ST_Active__c = false;
                price.ST_Deactivation_Date__c = system.today();
                acrContractPrices2Update.add(price);
            }
            else if(price.ST_Active__c && deactivationEvent == 'DeactivationDueToExpiration'){
                price.ST_Active__c = false;
                price.ST_Deactivation_Date__c = system.today();
                acrContractPrices2Update.add(price);
            }
            
        }
        if(acrContractPrices2Update.size() >0){
            try {
                update acrContractPrices2Update;
            } catch (DmlException ex) {
                System.debug('The Update on contractedPriceActivation Failed Exception***'+ex.getMessage());
            }
        }
    }
    
  • June 22, 2022
  • Like
  • 0