1

I am trying it as:

Using the collection

\Magento\CustomerBalance\Model\Balance\HistoryFactory $historyFactory

As

$model = $this->_historyFactory->create();
    //$model->setId(2);
    $model->setData('balance_amount', 333);
    $model->save();

But it prompts this exception:

1 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): A balance is needed to save a balance history.

1 Answer 1

1

If you see in file magento/module-customer-balance/Model/Balance/History.php line no: 154. You need to set setBalanceModel and setHistoryAction like this

require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');


// for Update customer balance history set setHistoryAction(2)

$customerBalance = $obj->create(\Magento\CustomerBalance\Model\BalanceFactory::class)->create()->load(2)->setHistoryAction(2);

$customerBalanceHistory = $obj->create(\Magento\CustomerBalance\Model\Balance\History::class)->create()
        ->setBalanceModel($customerBalance)
        ->setId(2)
        ->setAdditionalInfo('test')
        ->save();    

// for new entry use set setHistoryAction(1)

$customerBalance = $obj->create(\Magento\CustomerBalance\Model\BalanceFactory::class)->create()->load(2)->setHistoryAction(1);

$customerBalanceHistory = $obj->create(\Magento\CustomerBalance\Model\Balance\HistoryFactory::class)->create()
        ->setBalanceModel($customerBalance)
        ->setAdditionalInfo('test')
        ->save();

Hope this will solve your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.