500 Error on Email Invoice

One of my imported companies is working fine.

The other company works fine and I can generate PDFs, however when I go to email the PDF I get a 500 error. In the browser console I can see its got a 500 response to the /api/v1/templates

In the larvel.log i see

[2021-08-04 10:24:15] production.ERROR: Attempt to read property "account" on null {"userId":1,"exception":"[object] (ErrorException(code: 0): Attempt to read property \"account\" on null at /var/www/invoiceninja/app/Utils/Helpers.php:37)
[stacktrace]
#0 /var/www/invoiceninja/app/Utils/Helpers.php(37): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 /var/www/invoiceninja/app/Utils/TemplateEngine.php(199): App\\Utils\\Helpers::sharedEmailVariables()
#2 /var/www/invoiceninja/app/Utils/TemplateEngine.php(83): App\\Utils\\TemplateEngine->renderTemplate()
#3 /var/www/invoiceninja/app/Http/Controllers/TemplateController.php(96): App\\Utils\\TemplateEngine->build()
#4 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\\Http\\Controllers\\TemplateController->show()
#5 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Controller->callAction()
#6 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/Route.php(254): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#7 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/Route.php(197): Illuminate\\Routing\\Route->runController()
#8 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/Router.php(695): Illuminate\\Routing\\Route->run()
#9 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#10 /var/www/invoiceninja/app/Http/Middleware/QueryLogging.php(40): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#11 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): App\\Http\\Middleware\\QueryLogging->handle()
#12 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#13 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#14 /var/www/invoiceninja/app/Http/Middleware/Locale.php(34): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#15 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): App\\Http\\Middleware\\Locale->handle()
#16 /var/www/invoiceninja/app/Http/Middleware/TokenAuth.php(80): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#17 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): App\\Http\\Middleware\\TokenAuth->handle()
#18 /var/www/invoiceninja/app/Http/Middleware/SetDb.php(41): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#19 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): App\\Http\\Middleware\\SetDb->handle()
#20 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(127): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#21 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(63): Illuminate\\Routing\\Middleware\\ThrottleRequests->handleRequest()
#22 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Routing\\Middleware\\ThrottleRequests->handle()
#23 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#24 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/Router.php(697): Illuminate\\Pipeline\\Pipeline->then()
#25 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/Router.php(672): Illuminate\\Routing\\Router->runRouteWithinStack()
#26 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\\Routing\\Router->runRoute()
#27 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Routing/Router.php(625): Illuminate\\Routing\\Router->dispatchToRoute()
#28 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\\Routing\\Router->dispatch()
#29 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#30 /var/www/invoiceninja/vendor/sentry/sentry-laravel/src/Sentry/Laravel/Http/SetRequestIpMiddleware.php(55): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#31 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Sentry\\Laravel\\Http\\SetRequestIpMiddleware->handle()
#32 /var/www/invoiceninja/vendor/sentry/sentry-laravel/src/Sentry/Laravel/Http/SetRequestMiddleware.php(52): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#33 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Sentry\\Laravel\\Http\\SetRequestMiddleware->handle()
#34 /var/www/invoiceninja/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(60): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#35 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Barryvdh\\Debugbar\\Middleware\\InjectDebugbar->handle()
#36 /var/www/invoiceninja/app/Http/Middleware/Cors.php(25): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#37 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): App\\Http\\Middleware\\Cors->handle()
#38 /var/www/invoiceninja/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#39 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\\Proxy\\TrustProxies->handle()
#40 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#41 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#42 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle()
#43 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#44 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(40): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle()
#45 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle()
#46 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#47 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle()
#48 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#49 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle()
#50 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#51 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\\Pipeline\\Pipeline->then()
#52 /var/www/invoiceninja/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#53 /var/www/invoiceninja/public/index.php(57): Illuminate\\Foundation\\Http\\Kernel->handle()
#54 {main}
"}

@david do you have any thoughts?

In your logs - did the migration go all the way through ok? It looks like perhaps not all of the the associated users got migrated?

Yep migration competed without issue after i did a fresh install of V5. There was a user in this account that I deleted because it was never used.

how did you delete the user? i think there is a stale reference remaining in your dataset to that user.

Deleted it in the web interface on the V4 instance pre migrate.

How would i remove that reference in the DB?

any suggestions on this? I really can’t afford to lose years worth of invoices remaking this company

As an update to this. It only occurs on clients that were imported - if I create a new client the email invoice works fine

Thanks for the info!

cc @david

@AltF4

I’ll need you to look through the database records. Can you please find the row containing a client you have a problem with. and inspect the user_id column.

You’ll then want to use that id number and search for it in the users table My guess is there is a missing User record somewhere.

The fix would be to change the user_id of the broken client records to a valid user_id for your company.

which table should I be looking in? Would this be clients?

If so the clients that have not been working have a user id of 2 where as companies that work have 1

Looking through, there are a lot of user IDs in a lot of the tables. Do all of these need to be replaced with user ID 1?

@david do i need to update the user_id value in every table that has that field?

So far I can it in:

  • activities
  • client_contacts
  • company_ledgers
  • invoices
  • invoice_invitations

Thats correct, i think the easiest way could be to use tinker for this on the command line:

php artisan tinker
>> Invoice::where('user_id', OLD_USER_ID)->update(['user_id' => NEW_USER_ID]);

You’d want to do this for all the entities

I ran that command with artisan tinker but it didn’t seem to do anything.

I have updated all instances of the user ID 2 to user ID 1 in phpmyadmin and it now seems to let me send the emails

1 Like