0

How can I make this function run - also when getUsername() is null

$authSession = $objectManager->get('Magento\Backend\Model\Auth\Session');
$UUsername = $authSession->getUser()->getUsername();
$glsDirName = $this->directoryList->getPath('var') . '/GLS/' . $UUsername;

[2021-01-16 06:25:09] main.ERROR: Cron Job gls_import_tracking has an error: Call to a member function getUsername() on null. Statistics: {"sum":0,"count":1,"realmem":0,"emalloc":0,"realmem_start":223346688,"emalloc_start":220510920} [] [] [2021-01-16 06:25:09] main.CRITICAL: Error when running a cron job {"exception":"[object] (RuntimeException(code: 0): Error when running a cron job at /bbb/bbb/www/vendor/magento/module-cron/Observer/ProcessCronQueueObserver.php:362, Error(code: 0): Call to a member function getUsername() on null at /bbb/bbb/www/vendor/tric/module-gls/Helper/Data.php:453)"} []

1
  • Please mark the answer as accepted, if it helped you resolve the issue. – Vivek Kumar Jan 27 at 8:02
1

You need to check if you're getting user from AuthSession Model first, you can accomplish this by using following code;

$authSession = $objectManager->get('Magento\Backend\Model\Auth\Session');
$user =  $authSession->getUser();
if($user) {
   // does not run if there are no available user in auth session
   $UUsername = $user->getUsername();
   $glsDirName = $this->directoryList->getPath('var') . '/GLS/' . $UUsername;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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