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
Pavan SonarePavan Sonare 

how can I prevent to update the 'Due_Date__C' date from updating everyday for below code?

Group_or_Party_Payout__c objGroupPayout = new Group_or_Party_Payout__c(
                        Rebate_Program__c = strPGID,
                        Rebate_Program_Payout_Period__c = strPeriodID,
                        Account__c = strAccountId,
                        Calculation_Date__c = date.today(),
                        Gross_Dollar_Rebates__c = (Decimal)mapDetail.get(GROSS_DOLLAR) == null ? 0 : (Decimal)mapDetail.get(GROSS_DOLLAR),
                        Gross_Per_Pound_Rebates__c = (Decimal)mapDetail.get(GROSS_PER_POUND) == null ? 0 : (Decimal)mapDetail.get(GROSS_PER_POUND),
                        Calculated_Rebate_Amount__c = ((Decimal)mapDetail.get(GROSS_DOLLAR) == null ? 0 : (Decimal)mapDetail.get(GROSS_DOLLAR)) + ((Decimal)mapDetail.get(GROSS_PER_POUND) == null ? 0 : (Decimal)mapDetail.get(GROSS_PER_POUND)),
                        Rebate_23100__c = decRebate23100,
                        Commission_23190_Tax__c = decCommission23100,
                        Advertising_23120__c = decAdvertising,
                        Total_Approved_Adjustment_Amount__c = (Decimal)mapDetail.get(ADJUSTMENT) == null ? 0 : (Decimal)mapDetail.get(ADJUSTMENT),
                        Name = strPGName + '-' + strPeriodName,
                        Due_Date__c = date.today(),
                        External_ID__c = strPGID + '-' + strPeriodID + '-' + strAccountId,
                        Delinquent_Accounts__c = (String)mapDetail.get(DELINQUENT),
                        Org_ID2__c = (String)mapDetail.get(ORG_ID),
                        Invoice_Description__c = strInvoiceDesc,
                        Cash_Discount2__c = decCashDisc,
                        Fuel_Surcharge__c = decFuelSur,
                        Status__c = 'Draft'
                    ); 
                    lstGroupPayout.add(objGroupPayout);
                }
            }
            
            system.debug('List of group or part payouts =>' + lstGroupPayout); // List of group or party payout.

            Integer intSuccessCount = 0;
            Integer intFailureCount = 0;
            String strFailureInfo = '';
            Schema.SObjectField externalId = Group_or_Party_Payout__c.Fields.External_ID__c;
            Database.UpsertResult[] srList = Database.upsert(lstGroupPayout, externalId, false);

I just want the due date to be set at as when the record is created and not update everyday

(This is a batch class that runs everynight)

Julien SalensonJulien Salenson
Hi Pavan,

You should modify your code to set the Due_Date__c field only during the initial creation (insert) and not update it during upsert.

Here's a code that you'll have to adapt to your object Group_or_Party_Payout__c and change the field "External_ID__c". This is just an example of how you can modify your code to set the Due_Date__c only for the insert:
// Check if the record with the same External ID already exists
Map<String, Group_or_Party_Payout__c> existingRecords = new Map<String, Group_or_Party_Payout__c>();
for (Group_or_Party_Payout__c existingRecord : [SELECT External_ID__c, Due_Date__c FROM Group_or_Party_Payout__c WHERE External_ID__c IN :externalIds]) {//change the condition to adapt to your code
    existingRecords.put(existingRecord.External_ID__c, existingRecord);
}

for (Group_or_Party_Payout__c newRecord : lstGroupPayout) {
    String externalId = newRecord.External_ID__c;

    if (existingRecords.containsKey(externalId)) {
        // If the record already exists, don't update the Due_Date__c field
        newRecord.Due_Date__c = existingRecords.get(externalId).Due_Date__c;
    } else {
        // If the record is new, set the Due_Date__c to the current date
        newRecord.Due_Date__c = Date.today();
    }
}

// Perform upsert with the modified records
Database.UpsertResult[] srList = Database.upsert(lstGroupPayout, externalId, false);

This code checks if a record with the same External_ID__c already exists.
  • If it does, it retrieves the existing Due_Date__c and sets it on the new record, effectively preventing the Due_Date__c from being updated.
  • If the record is new, it sets the Due_Date__c to the current date.

Please mark this comment as best answer if it's help you.
 
Pavan SonarePavan Sonare

 


below is the modified class as per your suggestion, but now its not updating any other fields like it used to.

public without sharing class ads_GroupPartyPayoutforGroup_Bat implements Database.Batchable<sObject>, Database.Stateful {
    private static final String GROSS_PER_POUND = 'AmountperUnit';
    private static final String GROSS_DOLLAR = 'PercentageOfRevenue';
    private static final String NON_MARKETING = 'Non Marketing';
    private static final String MARKETING = 'Marketing';
    private static final String ADJUSTMENT = 'Adjustment';
    private static final String DELINQUENT = 'Delinquent';
    private static final String ORG_ID = 'Org ID';
    private static final String CRITERIA = 'criteria';
    private static final String SOURCE_FIELD_NAME = 'sourceFieldName';
    private static final String SHIP_TO_ACCOUNT = 'Ship_to_Account__c';
    private static final String VALUE = 'value';
    private static final String INVOICE_DESC = 'INVOICE_DESC';
    private static final String CASH_DISCOUNT = 'CASH_DISCOUNT';
    private static final String FUEL_SURCHARGE = 'FUEL_SURCHARGE';
    private static final String FIXED_AMOUNT = 'FixedAmount';

    private List<String> lstRebateProgram = new List<String>();

    private String strQuery = 'SELECT Id, Name, Group_Account__c, Rebate_Category__c FROM RebateProgram WHERE Status = \'Active\' AND Group_Account__c != NULL';
    public ads_GroupPartyPayoutforGroup_Bat() {
        strQuery = this.strQuery;
    }

    public ads_GroupPartyPayoutforGroup_Bat(String strProgramId) {
        strQuery = this.strQuery;
        strQuery += ' AND Id = ' + '\'' + strProgramId + '\'';
    }

    public Database.QueryLocator start(Database.BatchableContext objBC) {
        return Database.getQueryLocator(strQuery);
    }

    public void execute(Database.BatchableContext objBC, List<RebateProgram> lstScope) {
        try {

            List<String> lstRebateProgramId2 = new List<String>();
            for (RebateProgram objPG : lstScope) {
                lstRebateProgramId2.add(objPG.Id);
            }

            for (RebateProgram objRebateProgram : lstScope) {
                this.lstRebateProgram.add(objRebateProgram.Id);
            }

            List<ProgramRebateTypePayout> lstRebateTypePayouts = [
                SELECT ProgramRebateType.RebateProgramId, RebateProgramMemberPayout.PeriodId, RebateProgramMemberPayout.Period.Name, 
                        ProgramRebateType.Marketing__c, ProgramRebateType.RebateMeasureType, RebateAmount, RebateProgramMemberPayout.Member.Org_ID__c
                FROM ProgramRebateTypePayout
                WHERE ProgramRebateType.Send_Payout__c = false AND ProgramRebateType.RebateProgramId IN :lstRebateProgramId2
            ];
            Map<String, Map<String, Object>> mapGroupPayoutDetail = new Map<String, Map<String, Object>>();
            for (ProgramRebateTypePayout objRebateTypePayout : lstRebateTypePayouts) {
                String strKey = objRebateTypePayout.ProgramRebateType.RebateProgramId + '-' + objRebateTypePayout.RebateProgramMemberPayout.PeriodId + '-' + objRebateTypePayout.RebateProgramMemberPayout.Period.Name;
                if (mapGroupPayoutDetail.containsKey(strKey)) {
                    Map<String, Object> mapDetail = mapGroupPayoutDetail.get(strKey);
                    if ((String)mapDetail.get(ORG_ID) == null) {
                        mapDetail.put(ORG_ID, objRebateTypePayout.RebateProgramMemberPayout.Member.Org_ID__c);
                        mapGroupPayoutDetail.put(strKey, mapDetail);
                    }
                    if (objRebateTypePayout.ProgramRebateType.Marketing__c == false) {
                        Decimal decNonMarketing = ((Decimal)mapDetail.get(NON_MARKETING) == null ? 0 : (Decimal)mapDetail.get(NON_MARKETING)) + objRebateTypePayout.RebateAmount;
                        mapDetail.put(NON_MARKETING, decNonMarketing);
                        mapGroupPayoutDetail.put(strKey, mapDetail);
                    }
                    if (objRebateTypePayout.ProgramRebateType.Marketing__c == true) {
                        Decimal decMarketing = ((Decimal)mapDetail.get(MARKETING) == null ? 0 : (Decimal)mapDetail.get(MARKETING)) + objRebateTypePayout.RebateAmount;
                        mapDetail.put(MARKETING, decMarketing);
                        mapGroupPayoutDetail.put(strKey, mapDetail);
                    }
                    if (objRebateTypePayout.ProgramRebateType.RebateMeasureType == GROSS_PER_POUND && objRebateTypePayout.ProgramRebateType.Marketing__c == false) {
                        Decimal decPound = ((Decimal)mapDetail.get(GROSS_PER_POUND) == null ? 0 : (Decimal)mapDetail.get(GROSS_PER_POUND)) + objRebateTypePayout.RebateAmount;
                        mapDetail.put(GROSS_PER_POUND, decPound);
                        mapGroupPayoutDetail.put(strKey, mapDetail);
                    }
                    if (objRebateTypePayout.ProgramRebateType.RebateMeasureType == GROSS_DOLLAR && objRebateTypePayout.ProgramRebateType.Marketing__c == false) {
                        Decimal decDollar = ((Decimal)mapDetail.get(GROSS_DOLLAR) == null ? 0 : (Decimal)mapDetail.get(GROSS_DOLLAR)) + objRebateTypePayout.RebateAmount;
                        mapDetail.put(GROSS_DOLLAR, decDollar);
                        mapGroupPayoutDetail.put(strKey, mapDetail);
                    }
                }
                else {
                    Map<String, Object> mapDetail = new Map<String, Object>();
                    if ((String)mapDetail.get(ORG_ID) == null) {
                        mapDetail.put(ORG_ID, objRebateTypePayout.RebateProgramMemberPayout.Member.Org_ID__c);
                        mapGroupPayoutDetail.put(strKey, mapDetail);
                    }
                    if (objRebateTypePayout.ProgramRebateType.Marketing__c == false) {
                        Decimal decNonMarketing = objRebateTypePayout.RebateAmount;
                        mapDetail.put(NON_MARKETING, decNonMarketing);
                        mapGroupPayoutDetail.put(strKey, mapDetail);
                    }
                    if (objRebateTypePayout.ProgramRebateType.Marketing__c == true) {
                        Decimal decMarketing = objRebateTypePayout.RebateAmount;
                        mapDetail.put(MARKETING, decMarketing);
                        mapGroupPayoutDetail.put(strKey, mapDetail);
                    }
                    if (objRebateTypePayout.ProgramRebateType.RebateMeasureType == GROSS_PER_POUND && objRebateTypePayout.ProgramRebateType.Marketing__c == false) {
                        Decimal decPound = objRebateTypePayout.RebateAmount;
                        mapDetail.put(GROSS_PER_POUND, decPound);
                        mapGroupPayoutDetail.put(strKey, mapDetail);
                    }
                    if (objRebateTypePayout.ProgramRebateType.RebateMeasureType == GROSS_DOLLAR && objRebateTypePayout.ProgramRebateType.Marketing__c == false) {
                        Decimal decDollar = objRebateTypePayout.RebateAmount;
                        mapDetail.put(GROSS_DOLLAR, decDollar);
                        mapGroupPayoutDetail.put(strKey, mapDetail);
                    }
                }
            }

            List<RebatePayoutAdjustment> lstPayoutAdjustment = [
                SELECT RebateProgramMemberPayout.Member.RebateProgramId, RebateProgramMemberPayout.PeriodId, 
                        RebateProgramMemberPayout.Period.Name, AdjustmentAmount,
                        RebateProgramMemberPayout.Member.Account.Name
                FROM RebatePayoutAdjustment
                WHERE Status = 'Approved' AND RebateProgramMemberPayout.Member.RebateProgramId IN :lstRebateProgramId2
            ];

            for (RebatePayoutAdjustment objPayoutAdjustment : lstPayoutAdjustment) {
                String strKey = objPayoutAdjustment.RebateProgramMemberPayout.Member.RebateProgramId + '-' + objPayoutAdjustment.RebateProgramMemberPayout.PeriodId + '-' + objPayoutAdjustment.RebateProgramMemberPayout.Period.Name;
                if (mapGroupPayoutDetail.containsKey(strKey)) {
                    Map<String, Object> mapDetail = mapGroupPayoutDetail.get(strKey);
                    Decimal decAdjust = ((Decimal)mapDetail.get(ADJUSTMENT) == null ? 0 : (Decimal)mapDetail.get(ADJUSTMENT)) + objPayoutAdjustment.AdjustmentAmount;
                    String strDelinquent = '';
                    if ((String)mapDetail.get(DELINQUENT) == null) {
                        strDelinquent = objPayoutAdjustment.RebateProgramMemberPayout.Member.Account.Name;
                    }
                    else {
                        strDelinquent = ((String)mapDetail.get(DELINQUENT)) + ';' + objPayoutAdjustment.RebateProgramMemberPayout.Member.Account.Name;
                    }
                    mapDetail.put(ADJUSTMENT, decAdjust);
                    mapDetail.put(DELINQUENT, strDelinquent);
                    mapGroupPayoutDetail.put(strKey, mapDetail);
                }
                else {
                    Map<String, Object> mapDetail = new Map<String, Object>();
                    Decimal decAdjust = objPayoutAdjustment.AdjustmentAmount;
                    String strDelinquent = objPayoutAdjustment.RebateProgramMemberPayout.Member.Account.Name;
                    mapDetail.put(ADJUSTMENT, decAdjust);
                    mapDetail.put(DELINQUENT, strDelinquent);
                    mapGroupPayoutDetail.put(strKey, mapDetail);
                }
            }

            //Add logic for Ship to payout rebate program --start
            Map<String, Map<String, Object>> mapRebateTypeDetails = new Map<String, Map<String, Object>>();
            List<ProgramRebateType> lstRebateType = [
                SELECT Id, FilterCriteria, RebateProgramId, Cash_Discount__c, Fuel_Surcharge__c, RebateProgram.Ship_to_Payout__c, RebateMeasureType
                FROM ProgramRebateType 
                //WHERE RebateProgram.Ship_to_Payout__c = true 
                WHERE RebateProgramId IN :lstRebateProgramId2
            ];

            List<ProgramRebateTypeBenefit> lstBenefit = [
                SELECT Id, BenefitValue, ProgramRebateType.RebateProgramId 
                FROM ProgramRebateTypeBenefit 
                WHERE ProgramRebateType.RebateMeasureType = :FIXED_AMOUNT 
                AND ProgramRebateType.RebateProgramId IN :lstRebateProgramId2
            ];

            Map<String, Decimal> mapBenefit = new Map<String, Decimal>();
            for (ProgramRebateTypeBenefit objBenefit : lstBenefit) {
                if (mapBenefit.containsKey(objBenefit.ProgramRebateType.RebateProgramId)) {
                    Decimal decAmount = mapBenefit.get(objBenefit.ProgramRebateType.RebateProgramId);
                    decAmount += objBenefit.BenefitValue;
                    mapBenefit.put(objBenefit.ProgramRebateType.RebateProgramId, decAmount);
                }
                else {
                    Decimal decAmount = objBenefit.BenefitValue;
                    mapBenefit.put(objBenefit.ProgramRebateType.RebateProgramId, decAmount);
                }
            }

            if (lstRebateType.size() > 0) {
                for (ProgramRebateType objRebateType : lstRebateType) {
                    if (!mapRebateTypeDetails.containsKey(objRebateType.RebateProgramId)) {
                        Map<String, Object> mapDetails = new Map<String, Object>();
                        mapDetails.put(CASH_DISCOUNT, objRebateType.Cash_Discount__c);
                        mapDetails.put(FUEL_SURCHARGE, objRebateType.Fuel_Surcharge__c);
                        mapRebateTypeDetails.put(objRebateType.RebateProgramId, mapDetails);
                    }
                    if (objRebateType.RebateProgram.Ship_to_Payout__c == true) {
                        String strFilter = objRebateType.FilterCriteria;
                        if (strFilter != null) {
                            strFilter = strFilter.replace('\'','"');
                            Map<String,Object> arr =(Map<String,Object>)JSON.deserializeUntyped(strFilter);
                            List<Object> lstDetails = new List<Object>();
                            for (String strKey : arr.keySet()) {
                                if (strKey == CRITERIA) {
                                    lstDetails = (List<Object>)arr.get(strKey);
                                }
                            }
                            if (mapRebateTypeDetails.containsKey(objRebateType.RebateProgramId)) {
                                Map<String, Object> mapInvoiceDsc = mapRebateTypeDetails.get(objRebateType.RebateProgramId);
                                String strInvoiceDsc = mapInvoiceDsc.get(INVOICE_DESC) == null ? '' : (String)mapInvoiceDsc.get(INVOICE_DESC);
                                for (Object mapDetails : lstDetails) {
                                    Map<String, Object> mapDetails1 = (Map<String, Object>)mapDetails;
                                    if (mapDetails1.get(SOURCE_FIELD_NAME) == SHIP_TO_ACCOUNT) {
                                        strInvoiceDsc += (String)mapDetails1.get(VALUE) + ',';
                                    }
                                }
                                mapInvoiceDsc.put(INVOICE_DESC, strInvoiceDsc);
                                mapRebateTypeDetails.put(objRebateType.RebateProgramId, mapInvoiceDsc);
                            }
                            else {
                                String strInvoiceDsc = '';
                                Map<String, Object> mapInvoiceDsc = new Map<String, Object>();
                                for (Object mapDetails : lstDetails) {
                                    Map<String, Object> mapDetails1 = (Map<String, Object>)mapDetails;
                                    if (mapDetails1.get(SOURCE_FIELD_NAME) == SHIP_TO_ACCOUNT) {
                                        strInvoiceDsc += (String)mapDetails1.get(VALUE) + ',';
                                    }
                                }
                                mapInvoiceDsc.put(INVOICE_DESC, strInvoiceDsc);
                                mapRebateTypeDetails.put(objRebateType.RebateProgramId, mapInvoiceDsc);
                            }
                        }
                    }          
                }
            }

            //Add logic for Ship to payout rebate program --end

            Map<String, List<String>> mapAcctPGID = new Map<String, List<String>>();
            for (RebateProgram objPG : lstScope) {
                List<String> lstDetails = new List<String>();
                String strNameAndAcctID = objPG.Name + '-' + objPG.Group_Account__c;
                lstDetails.add(strNameAndAcctID);
                lstDetails.add(objPG.Rebate_Category__c);
                mapAcctPGID.put(objPG.Id, lstDetails);
            }

            List<Group_or_Party_Payout__c> lstGroupPayout = new List<Group_or_Party_Payout__c>();
            
            for (String strKey : mapGroupPayoutDetail.keySet()) {
                Map<String, Object> mapDetail = mapGroupPayoutDetail.get(strKey);
                String strPGID = strKey.substringBefore('-');
                String strPeriodID = strKey.substringBeforeLast('-').substringAfter('-');
                String strPeriodName = strKey.substringAfterLast('-');

                if (mapAcctPGID.containsKey(strPGID)) {
                    
                    String strInvoiceDesc = '';
                    Decimal decCashDisc = 0;
                    Decimal decFuelSur = 0;
                    if (mapRebateTypeDetails.containsKey(strPGID)) {
                        Map<String, Object> mapRebateTypeDetail = mapRebateTypeDetails.get(strPGID);
                        strInvoiceDesc = mapRebateTypeDetail.get(INVOICE_DESC) == null ? '' : ((String)mapRebateTypeDetail.get(INVOICE_DESC)).removeEnd(',');
                        decCashDisc = mapRebateTypeDetail.get(CASH_DISCOUNT) == null ? 0 : (Decimal)mapRebateTypeDetail.get(CASH_DISCOUNT);
                        decFuelSur = mapRebateTypeDetail.get(FUEL_SURCHARGE) == null ? 0 : (Decimal)mapRebateTypeDetail.get(FUEL_SURCHARGE);
                    }
                    List<String> lstAccountDetails = mapAcctPGID.get(strPGID);
                    String strNameAndAcctID = lstAccountDetails[0];
                    String strAccountId = strNameAndAcctID.substringAfterLast('-');
                    String strPGName = strNameAndAcctID.substringBeforeLast('-');
                    Decimal decAdvertising = (Decimal)mapDetail.get(MARKETING) == null ? 0 : (Decimal)mapDetail.get(MARKETING);
                    if (mapBenefit.containsKey(strPGID)) {
                        Decimal decAmount = (Decimal)mapBenefit.get(strPGID) == null ? 0 : (Decimal)mapBenefit.get(strPGID);
                        decAdvertising += decAmount;
                    }
                    /*
                    //Change01
                    String strExternalId = strPGID + '-' + strPeriodID + '-' + strAccountId;
                    Group_or_Party_Payout__c existingRecordDueDate = [SELECT Due_Date__c FROM Group_or_Party_Payout__c 
                                                               WHERE External_ID__c = :strExternalId LIMIT 1];
                    //Change02
                        Due_Date__c = existingRecordDueDate.Due_Date__c, 
                    */
                    Decimal decRebate23100 = 0;
                    Decimal decCommission23100 = 0;
                    if (lstAccountDetails[1] == 'Ditcher') {
                        decCommission23100 = (Decimal)mapDetail.get(NON_MARKETING) == null ? 0 : (Decimal)mapDetail.get(NON_MARKETING);
                    }
                    else {
                        decRebate23100 = (Decimal)mapDetail.get(NON_MARKETING) == null ? 0 : (Decimal)mapDetail.get(NON_MARKETING);
                    }
                    if (decRebate23100 < 0) {
                        decRebate23100 = 0;
                    }
                    if (decCommission23100 < 0) {
                        decCommission23100 = 0;
                    }
                    if (decAdvertising < 0) {
                        decAdvertising = 0;
                    }

                    Group_or_Party_Payout__c objGroupPayout = new Group_or_Party_Payout__c(
                        Rebate_Program__c = strPGID,
                        Rebate_Program_Payout_Period__c = strPeriodID,
                        Account__c = strAccountId,
                        Calculation_Date__c = date.today(),
                        Gross_Dollar_Rebates__c = (Decimal)mapDetail.get(GROSS_DOLLAR) == null ? 0 : (Decimal)mapDetail.get(GROSS_DOLLAR),
                        Gross_Per_Pound_Rebates__c = (Decimal)mapDetail.get(GROSS_PER_POUND) == null ? 0 : (Decimal)mapDetail.get(GROSS_PER_POUND),
                        Calculated_Rebate_Amount__c = ((Decimal)mapDetail.get(GROSS_DOLLAR) == null ? 0 : (Decimal)mapDetail.get(GROSS_DOLLAR)) + ((Decimal)mapDetail.get(GROSS_PER_POUND) == null ? 0 : (Decimal)mapDetail.get(GROSS_PER_POUND)),
                        Rebate_23100__c = decRebate23100,
                        Commission_23190_Tax__c = decCommission23100,
                        Advertising_23120__c = decAdvertising,
                        Total_Approved_Adjustment_Amount__c = (Decimal)mapDetail.get(ADJUSTMENT) == null ? 0 : (Decimal)mapDetail.get(ADJUSTMENT),
                        Name = strPGName + '-' + strPeriodName,
                        Due_Date__c = date.today(),                       
                        External_ID__c = strPGID + '-' + strPeriodID + '-' + strAccountId,
                        Delinquent_Accounts__c = (String)mapDetail.get(DELINQUENT),
                        Org_ID2__c = (String)mapDetail.get(ORG_ID),
                        Invoice_Description__c = strInvoiceDesc,
                        Cash_Discount2__c = decCashDisc,
                        Fuel_Surcharge__c = decFuelSur,
                        Status__c = 'Draft'
                    ); 
                    lstGroupPayout.add(objGroupPayout);
                }
            }
            
            list<group_or_party_Payout__c> payoutList = new list<group_or_party_Payout__c>([SELECT id, External_ID__c from group_or_party_Payout__c]);

                    set<string> setOfExternalIds = new set<string>();
            
                    for(group_or_party_Payout__c eachPayout : payoutList){
                        setOfExternalIds.add(eachPayout.External_ID__c);
                    }
                // Check if the record with the same External ID already exists
                Map<String, Group_or_Party_Payout__c> existingRecords = new Map<String, Group_or_Party_Payout__c>();
                for (Group_or_Party_Payout__c existingRecord : [SELECT External_ID__c, Due_Date__c FROM Group_or_Party_Payout__c WHERE External_ID__c IN :setOfExternalIds]) {//change the condition to adapt to your code
                    existingRecords.put(existingRecord.External_ID__c, existingRecord);
                }
                
                for (Group_or_Party_Payout__c newRecord : lstGroupPayout) {
                    String externalId = newRecord.External_ID__c;
                
                    if (existingRecords.containsKey(externalId)) {
                        // If the record already exists, don't update the Due_Date__c field
                        newRecord.Due_Date__c = existingRecords.get(externalId).Due_Date__c;
                    } else {
                        // If the record is new, set the Due_Date__c to the current date
                        newRecord.Due_Date__c = Date.today();
                    }
                }
            
            Integer intSuccessCount = 0;
            Integer intFailureCount = 0;
            String strFailureInfo = '';
            Schema.SObjectField externalId = Group_or_Party_Payout__c.Fields.External_ID__c;
            Database.UpsertResult[] srList = Database.upsert(lstGroupPayout, externalId, false); 

            for (Database.UpsertResult sr : srList) {
                if (sr.isSuccess()) {
                    intSuccessCount++;
                }
                else {              
                    for(Database.Error err : sr.getErrors()) {
                        intFailureCount++;
                        strFailureInfo = 'The following error has occurred.'                    
                                    + err.getStatusCode() + ': ' + err.getMessage()
                                    + ' fields that affected this error: ' + err.getFields() + '\n';
                    }
                }
            }
            System.debug('intSuccessCount: ' + intSuccessCount);
            System.debug('intFailureCount: ' + intFailureCount);
            System.debug('strFailureInfo: ' + strFailureInfo);
        } catch (Exception e) {
            System.debug('error message:' + e.getMessage() + ' line ' + e.getLineNumber());
        }
    }
    
        public void finish(Database.BatchableContext objBC) {
         system.debug('lstRebateProgram.size: ' + lstRebateProgram.size());
        ads_GroupPartyPayoutCorrelation_Bat b = new ads_GroupPartyPayoutCorrelation_Bat(lstRebateProgram);
        Integer intBatchSize = Integer.valueOf(Label.BatchSize);
        Database.executeBatch(b, intBatchSize);
    }
}