Zend certified PHP/Magento developer

Magento2 programatically create coupon code error

I have a code I have used in the past. However now I have a cms page that calls in the phtml file and when I then try to go to the page in my browser i just get an error saying “an error occurred while trying to generate this content. Which is not very informative.

I am hoping someone might be able to spot the error (second pair of eyes sometimes help)
I know it is not best practice to use the ObjectManager. I just need this to work and be backwards compatible with something else it is doing later on in this file. Anyway the error seems to be generated after trying to render the below lines

$objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
$state = $objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('adminhtml');  


$objectManager = MagentoFrameworkAppObjectManager::getInstance(); 
$state = $objectManager->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$state->setAreaCode('adminhtml');  

$coupon['name'] = "Warranty test code";
$coupon['desc'] = "Warranty Code For";
$coupon['start'] = date('Y-m-d');
$coupon['end'] = '';
$coupon['max_redemptions'] = 1;
$coupon['discount_type'] ='by_fixed';
$coupon['discount_amount'] = 15;
$coupon['flag_is_free_shipping'] = 'no';
$coupon['redemptions'] = 1;
$coupon['code'] ='AUG92022TEST';

$shoppingCartPriceRule = $objectManager->create('MagentoSalesRuleModelRule');
$shoppingCartPriceRule->setName($coupon['name'])
      ->setDescription($coupon['desc'])
      ->setFromDate($coupon['start'])
      ->setToDate($coupon['end'])
      ->setUsesPerCustomer($coupon['max_redemptions'])
      ->setCustomerGroupIds(array('0','1','2','3'))
      ->setIsActive(1)
      ->setSimpleAction($coupon['discount_type'])
      ->setDiscountAmount($coupon['discount_amount'])
      ->setDiscountQty(1)
      ->setApplyToShipping($coupon['flag_is_free_shipping'])
      ->setTimesUsed($coupon['redemptions'])
      ->setWebsiteIds(array('1','2','6','7'))
      ->setCouponType(2)
      ->setCouponCode($coupon['code'])
      ->setUsesPerCoupon(NULL);
 $shoppingCartPriceRule->save();

 echo "created";