Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Error when sending email from custom codeunit using codeunit 8901 "Email"

Posted on by 25

I created a custom codeunit to subscribe to OnAfterWhseActivLineInsert so I can send an email notification if certain criteria match.

The emails are not sending and not sure why. This is what i found in azure app insights

codeunit 50400 EmailUsedPick
{
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Create Pick", 'OnAfterWhseActivLineInsert', '', false, false)]


    local procedure OnAfterWhseActivLineInsert(var WarehouseActivityLine: Record "Warehouse Activity Line")
    var
        Customer: Record "Customer";
        SalesHeader: Record "Sales Header";
        SalesPerson: Record "Salesperson/Purchaser";
        EmailMessage: Codeunit "Email Message";
        Email: Codeunit Email;
        Salesman: Text;
        SalesmanName: Text;
        CustomerName: Text;
        Recipients: List of [Text];
        Subject: Text;
        Body: Text;
        Title: Label 'PICK %1';
        Msg: Label 'Used Part pick has been created

The Sales Document %1 for Customer %2 has been created.
Customer wants %3 - %4.
Bin Code is %5.
Salesman is %6'; begin Customer.SetFilter("No.", WarehouseActivityLine."Destination No."); if Customer.FindSet() then CustomerName := Customer.Name; SalesHeader.SetFilter("No.", WarehouseActivityLine."Source No."); if SalesHeader.FindSet() then Salesman := SalesHeader."Salesperson Code"; SalesPerson.SetFilter(Code, Salesman); if SalesPerson.FindSet() then SalesmanName := SalesPerson.Name; if (WarehouseActivityLine."Source Document" = "Warehouse Activity Source Document"::"Sales Order") then if (WarehouseActivityLine."Location Code" = 'KS') then case (WarehouseActivityLine."Zone Code") of 'WHSE 5': begin Recipients.Add('***'); end; 'WHSE 9': begin Recipients.Add('***'); end; 'WHSE 10': begin Recipients.Add('***'); Recipients.Add('***'); end; end; if (WarehouseActivityLine."Bin Code" = 'SC - WHS B') then Recipients.Add('***'); if (Recipients.Count > 0) then begin Subject := StrSubstNo(Title, WarehouseActivityLine.Description); Body := StrSubstNo(Msg, WarehouseActivityLine."Source No.", CustomerName, WarehouseActivityLine."Qty. (Base)", WarehouseActivityLine.Description, WarehouseActivityLine."Bin Code", SalesmanName); EmailMessage.Create(Recipients, Subject, Body, true); Email.Send(EmailMessage, Enum::"Email Scenario"::Notification); end; end; }

Web Service Called (Outgoing): graph.microsoft.com/.../sendMail

aadTenantId
****
alAuthenticationMethod
AccessControlService
alHttpTimeout
00:01:40
alObjectId
4508
alObjectName
Email - Outlook API Client
alObjectType
CodeUnit
alStackTrace
"Email - Outlook API Client"(CodeUnit 4508).SendMailSingleRequest - Email - Outlook REST API by Microsoft
"Email - Outlook API Client"(CodeUnit 4508).SendEmail - Email - Outlook REST API by Microsoft
"Email - Outlook API Helper"(CodeUnit 4509).Send line 14 - Email - Outlook REST API by Microsoft
"Microsoft 365 Connector"(CodeUnit 4503).Send line 4 - Email - Microsoft 365 Connector by Microsoft
"Send Email"(CodeUnit 8890).OnRun(Trigger) line 5 - System Application by Microsoft
"Email Dispatcher"(CodeUnit 8888).SendEmail line 25 - System Application by Microsoft
"Email Dispatcher"(CodeUnit 8888).OnRun(Trigger) line 20 - System Application by Microsoft
"Email Impl"(CodeUnit 8900).Send line 41 - System Application by Microsoft
"Email Impl"(CodeUnit 8900).Send line 4 - System Application by Microsoft
"Email Impl"(CodeUnit 8900).Send line 7 - System Application by Microsoft
Email(CodeUnit 8901).Send line 2 - System Application by Microsoft
EmailUsedPick(CodeUnit 50400).OnAfterWhseActivLineInsert line 62 - AM Internal Extension by Addie
"Create Pick"(CodeUnit 7312).OnAfterWhseActivLineInsert(Event) line 2 - Base Application by Microsoft
"Create Pick"(CodeUnit 7312).CreateWhseDocLine line 42 - Base Application by Microsoft
"Create Pick"(CodeUnit 7312).CreateNewWhseDoc line 11 - Base Application by Microsoft
"Create Pick"(CodeUnit 7312).CreateWhseDocument line 85 - Base Application by Microsoft
"Whse.-Shipment - Create Pick"(Report 7318).OnPostReport(Trigger) line 8 - Base Application by Microsoft
"Warehouse Shipment Line"(Table 7321).CreatePickDocFromWhseShpt line 11 - Base Application by Microsoft
"Warehouse Shipment Line"(Table 7321).CreatePickDoc line 13 - Base Application by Microsoft
"Whse. Shipment Subform"(Page 7336).PickCreate line 12 - Base Application by Microsoft
"Warehouse Shipment"(Page 7335)."Create Pick - OnAction"(Trigger) line 3 - Base Application by Microsoft
companyName
****
component
Dynamics 365 Business Central Server
componentVersion
21.0.49939.51627
endpoint
https://graph.microsoft.com/v1.0/me/sendMail
environmentName
Production
environmentType
Production
eventId
RT0019
extensionId
e6328152-bb29-4664-9dae-3bc7eaae1fd8
extensionName
Email - Outlook REST API
extensionPublisher
Microsoft
extensionVersion
21.2.49946.50300
httpHeaders
{"Authorization":["Bearer "]}
httpMethod
POST
httpStatusCode
404
serverExecutionTime
00:00:00.0826007
sqlExecutes
0
sqlRowsRead
0
telemetrySchemaVersion
0.3
totalTime
00:00:00.0826007

Use ERROR with a text constant to improve telemetry details

aadTenantId
***
alCallerAppName
Base Application
alCallerAppVersion
21.2.49946.51746
alCallerAppVersionMajor
21
alCallerAppVersionMinor
2
alCallerPublisher
Microsoft
alCategory
FeatureTelemetry
alClientType
Web
alCompany
Abilene Machine LLC
alCountryCode
US
alDataClassification
SystemMetadata
alErrorCallStack
"Email - Outlook API Client"(CodeUnit 4508).ProcessErrorMessageResponse line 4 - Email - Outlook REST API by Microsoft\"Email - Outlook API Client"(CodeUnit 4508).SendMailSingleRequest - Email - Outlook REST API by Microsoft\"Email - Outlook API Client"(CodeUnit 4508).SendEmail - Email - Outlook REST API by Microsoft\"Email - Outlook API Helper"(CodeUnit 4509).Send line 14 - Email - Outlook REST API by Microsoft\"Microsoft 365 Connector"(CodeUnit 4503).Send line 4 - Email - Microsoft 365 Connector by Microsoft\"Send Email"(CodeUnit 8890).OnRun(Trigger) line 5 - System Application by Microsoft\"Email Dispatcher"(CodeUnit 8888).SendEmail line 25 - System Application by Microsoft\"Email Dispatcher"(CodeUnit 8888).OnRun(Trigger) line 20 - System Application by Microsoft\"Email Impl"(CodeUnit 8900).Send line 41 - System Application by Microsoft\"Email Impl"(CodeUnit 8900).Send line 4 - System Application by Microsoft\"Email Impl"(CodeUnit 8900).Send line 7 - System Application by Microsoft\Email(CodeUnit 8901).Send line 2 - System Application by Microsoft\EmailUsedPick(CodeUnit 50400).OnAfterWhseActivLineInsert line 62 - AM Internal Extension by Addie\"Create Pick"(CodeUnit 7312).OnAfterWhseActivLineInsert(Event) line 2 - Base Application by Microsoft\"Create Pick"(CodeUnit 7312).CreateWhseDocLine line 42 - Base Application by Microsoft\"Create Pick"(CodeUnit 7312).CreateNewWhseDoc line 11 - Base Application by Microsoft\"Create Pick"(CodeUnit 7312).CreateWhseDocument line 85 - Base Application by Microsoft\"Whse.-Shipment - Create Pick"(Report 7318).OnPostReport(Trigger) line 8 - Base Application by Microsoft\"Warehouse Shipment Line"(Table 7321).CreatePickDocFromWhseShpt line 11 - Base Application by Microsoft\"Warehouse Shipment Line"(Table 7321).CreatePickDoc line 13 - Base Application by Microsoft\"Whse. Shipment Subform"(Page 7336).PickCreate line 12 - Base Application by Microsoft\"Warehouse Shipment"(Page 7335)."Create Pick - OnAction"(Trigger) line 3 - Base Application by Microsoft\
alErrorText
Use ERROR with a text constant to improve telemetry details
alEventName
Failed to send email
alFeatureName
Emailing
alIsAdmin
No
alIsEvaluationCompany
No
alObjectId
8713
alObjectName
System Telemetry Logger
alObjectType
CodeUnit
alSubCategory
Error
alTenantLicenseState
Paid
alUserRole
SHIPPING AND RECEIVING - WMS
clientType
WebClient
companyName
Abilene Machine LLC
component
Dynamics 365 Business Central Server
componentVersion
21.0.49939.51627
environmentName
Production
environmentType
Production
eventId
AL0000CTP
extensionId
63ca2fa4-4f03-4f2b-a480-172fef340d3f
extensionName
System Application
extensionPublisher
Microsoft
extensionVersion
21.2.49946.51673
telemetrySchemaVersion
1.2

I assigned the scenario of notification which is what I used in my codeunit to use the shared mailbox account in BC

pastedimage1673628621509v1.png

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: Error when sending email from custom codeunit using codeunit 8901 "Email"

    Yes, if the e-mail is enqueued then you should see a job in the job queue that is in an error state.

  • bignadad Profile Picture
    bignadad 25 on at
    RE: Error when sending email from custom codeunit using codeunit 8901 "Email"

    okay. I did that in my sandbox first. How would I detect what happened? the job queue?

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: Error when sending email from custom codeunit using codeunit 8901 "Email"

    In the trigger OnAction please try to put the excact same code as you show in the screenshot from the Toolbox screenshot.

    Then you should have a whsline to feed the codeunit.

  • bignadad Profile Picture
    bignadad 25 on at
    RE: Error when sending email from custom codeunit using codeunit 8901 "Email"

    Forgive my lack of knowledge. I am new to this.

    I created a page extension on the Warehouse Pick itself to add the action to trigger the codeunit.

    pageextension 50150 TestAction extends "Warehouse Pick"

    {

       actions

       {

           addfirst(processing)

           {

               action(TestAction)

                   {

                       ApplicationArea = All;

                       Caption = 'Test Email';

                       trigger OnAction()

                       var

                           testCu : Codeunit "EmailUsedPick";

                       begin

                       end;

                   }

           }

       }

    }

    How would I call the codeunit 'EmailUsedPick'?

    if i use testCu.run() it won't have the activity line to process.

    I can't use testCu.OnAfterWhseActivLineInsert() because its protection level.

    Do I have to create a method in the EmailUsedPick codeunit? if so, what does that look like?

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: Error when sending email from custom codeunit using codeunit 8901 "Email"

    I am not familiar with Eriks toolbox.

    What i would do is to simply add an action to a page that trigges the codeunit and see if it runs into any errors.

  • bignadad Profile Picture
    bignadad 25 on at
    RE: Error when sending email from custom codeunit using codeunit 8901 "Email"

    I tried that using Erik's toolbox, but I don't get any error.

    Maybe I should be testing this a different way?

    pastedimage1673696122358v1.png

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: Error when sending email from custom codeunit using codeunit 8901 "Email"

    Yes, those were the ones I was talking about.

    Maybe you can try to execute the codeunit manually so you maybe get an error message displayed in the user interface?

  • bignadad Profile Picture
    bignadad 25 on at
    RE: Error when sending email from custom codeunit using codeunit 8901 "Email"

    I'm assuming you mean here? I checked the sent emails and they are not the ones sending from this codeunit.

    pastedimage1673661582638v1.png

  • Suggested answer
    Inge M. Bruvik Profile Picture
    Inge M. Bruvik 32,748 Super User 2024 Season 1 on at
    RE: Error when sending email from custom codeunit using codeunit 8901 "Email"

    The mails that does not show do they show up in the queue that shows unsent e-mails?

    If so what error are you getting if you try to resendt them from there?

    Those queues should show up if you use the business administrator role.

  • bignadad Profile Picture
    bignadad 25 on at
    RE: Error when sending email from custom codeunit using codeunit 8901 "Email"

    I forgot to mention that this is working in my sandbox environment.

    pastedimage1673630290254v1.png

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,134 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,928 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans