diff --git a/CRM/Pledge/BAO/Pledge.php b/CRM/Pledge/BAO/Pledge.php
index ead406b302d27d2688ad782d1a87ab015d830cb1..a30813cd9945cb2bc24085e2b3c8fe5bbf3f98a1 100644
--- a/CRM/Pledge/BAO/Pledge.php
+++ b/CRM/Pledge/BAO/Pledge.php
@@ -472,12 +472,11 @@ GROUP BY  currency
    * @param array $params
    *   An assoc array of name/value pairs.
    */
-  public static function sendAcknowledgment(&$form, $params) {
+  public static function sendAcknowledgment($form, $params) {
     //handle Acknowledgment.
     $allPayments = $payments = [];
 
     // get All Payments status types.
-    $paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
     $returnProperties = [
       'status_id',
       'scheduled_amount',
@@ -508,14 +507,6 @@ GROUP BY  currency
             'status' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
           ]
         );
-
-        // get the first valid payment id.
-        if (!isset($form->paymentId) && ($paymentStatusTypes[$values['status_id']] == 'Pending' ||
-            $paymentStatusTypes[$values['status_id']] == 'Overdue'
-          )
-        ) {
-          $form->paymentId = $values['id'];
-        }
       }
     }
 
diff --git a/CRM/Pledge/Form/Pledge.php b/CRM/Pledge/Form/Pledge.php
index 880ccaa0bae6a6db9f4b1217bc86e7a580f95610..16aa81a3b624c01db4ea3153c3edc254c4b555e2 100644
--- a/CRM/Pledge/Form/Pledge.php
+++ b/CRM/Pledge/Form/Pledge.php
@@ -15,6 +15,8 @@
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
+use Civi\Api4\PledgePayment;
+
 /**
  * This class generates form components for processing a pledge
  */
@@ -79,9 +81,7 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form {
 
     $this->userDisplayName = $this->userEmail = NULL;
     if ($this->_contactID) {
-      list($this->userDisplayName,
-        $this->userEmail
-        ) = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
+      [$this->userDisplayName, $this->userEmail] = CRM_Contact_BAO_Contact_Location::getEmailDetails($this->_contactID);
     }
 
     $this->setPageTitle(ts('Pledge'));
@@ -416,8 +416,10 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form {
 
   /**
    * Process the form submission.
+   *
+   * @throws \API_Exception
    */
-  public function postProcess() {
+  public function postProcess(): void {
     if ($this->_action & CRM_Core_Action::DELETE) {
       CRM_Pledge_BAO_Pledge::deletePledge($this->_id);
       return;
@@ -457,13 +459,13 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form {
     $dates = ['create_date', 'start_date', 'acknowledge_date', 'cancel_date'];
     foreach ($dates as $d) {
       if ($this->_id && (!$this->_isPending) && !empty($this->_values[$d])) {
-        if ($d == 'start_date') {
+        if ($d === 'start_date') {
           $params['scheduled_date'] = CRM_Utils_Date::processDate($this->_values[$d]);
         }
         $params[$d] = CRM_Utils_Date::processDate($this->_values[$d]);
       }
       elseif (!empty($formValues[$d]) && !CRM_Utils_System::isNull($formValues[$d])) {
-        if ($d == 'start_date') {
+        if ($d === 'start_date') {
           $params['scheduled_date'] = CRM_Utils_Date::processDate($formValues[$d]);
         }
         $params[$d] = CRM_Utils_Date::processDate($formValues[$d]);
@@ -499,10 +501,11 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form {
 
     // create pledge record.
     $pledge = CRM_Pledge_BAO_Pledge::create($params);
+    $pledgeID = $pledge->id;
 
     $statusMsg = NULL;
 
-    if ($pledge->id) {
+    if ($pledgeID) {
       // set the status msg.
       if ($this->_action & CRM_Core_Action::ADD) {
         $statusMsg = ts('Pledge has been recorded and the payment schedule has been created.<br />');
@@ -513,28 +516,31 @@ class CRM_Pledge_Form_Pledge extends CRM_Core_Form {
     }
 
     // handle Acknowledgment.
-    if (!empty($formValues['is_acknowledge']) && $pledge->id) {
+    if (!empty($formValues['is_acknowledge'])) {
 
       // calculate scheduled amount.
       $params['scheduled_amount'] = round($params['amount'] / $params['installments']);
       $params['total_pledge_amount'] = $params['amount'];
       // get some required pledge values in params.
-      $params['id'] = $pledge->id;
+      $params['id'] = $pledgeID;
       $params['acknowledge_date'] = $pledge->acknowledge_date;
       $params['is_test'] = $pledge->is_test;
       $params['currency'] = $pledge->currency;
       // retrieve 'from email id' for acknowledgement
       $params['from_email_id'] = $formValues['from_email_address'];
 
-      $this->paymentId = NULL;
       // send Acknowledgment mail.
       CRM_Pledge_BAO_Pledge::sendAcknowledgment($this, $params);
 
       $statusMsg .= ' ' . ts("An acknowledgment email has been sent to %1.<br />", [1 => $this->userEmail]);
-
+      // get the first valid payment id.
+      $nextPaymentID = PledgePayment::get()
+        ->addWhere('pledge_id', '=', $pledgeID)
+        ->addWhere('status_id:name', 'IN', ['Pending', 'Overdue'])
+        ->addOrderBy('scheduled_date')->setLimit(1)->execute()->first()['id'];
       // build the payment urls.
-      if ($this->paymentId) {
-        $urlParams = "reset=1&action=add&cid={$this->_contactID}&ppid={$this->paymentId}&context=pledge";
+      if ($nextPaymentID) {
+        $urlParams = "reset=1&action=add&cid={$this->_contactID}&ppid={$nextPaymentID}&context=pledge";
         $contribURL = CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams);
         $urlParams .= "&mode=live";
         $creditURL = CRM_Utils_System::url('civicrm/contact/view/contribution', $urlParams);