Developer forum

Forum » Dynamicweb 10 » Workflow Change Email on first workflow step

Workflow Change Email on first workflow step

Theodor Perrier
Reply

I'm experiencing some issues, in regards to workflow notifications when the products are created from a custom addin scheduled job.

I'm currently trying to trigger the notification subscriper manually, but this doesn't seem to send an email.

 

_productService.Save(product);

product.WorkflowStateId = workflowState.Id;

NotificationManager.Notify(Ecommerce.Product.ProductWorkflowStateChanged, new Ecommerce.Product.ProductWorkflowStateChangedArgs(product, 0));


Replies

 
Theodor Perrier
Reply

Bump

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Are you sure you are not running in a notification context that does not suppress notifications?

https://doc.dynamicweb.dev/documentation/extending/extensibilitypoints/notifications.html?q=notification%20context#suppressing-notifications

In your custom code, add var something = Dynamicweb.Extensibility.Notifications NotificationContext.NotificationState; and see what the current value is.

 
Theodor Perrier
Reply

Hey Nicolai.

 

When looking at the current context in the notification context, its null

var something = Dynamicweb.Extensibility.Notifications.NotificationContext.Current;

Is default behavior to supress notifications?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Can you share the entire code of your add in?

 
Theodor Perrier
Reply

private async Task<Product> CreateProductAsync(DynamicsModelDto model, Language language)
{
    var workflow = _workflowService.GetById(Convert.ToInt32(WorkflowId));
    var workflowState = workflow is not null ? _workflowStateService.GetStates(workflow).FirstOrDefault() : null;

    var product = new Product()
    {
        Id = RegexExtensions.RemoveNonAlphanumeric(model.No!, "_"),
        Number = model.No,
        Name = HttpUtility.HtmlDecode(await GetItemTranslation(model.No!, language) ?? model.Description),
        Active = true,
        LanguageId = language.LanguageId,
        NeverOutOfStock = true
    };

    if (workflowState is not null)
    {
        _productService.Save(product);
        product.WorkflowStateId = workflowState.Id;

        using(var context = new NotificationContext(NotificationContext.NotificationState.Notify))
        {
            NotificationManager.Notify(Ecommerce.Product.ProductWorkflowStateChanged, new Ecommerce.Product.ProductWorkflowStateChangedArgs(product, 0));
        }
    }

}

 

There is some more code related to other fields, but this part of the add in related to creation of the product and emitting the workflow notification.

I've tried adding a context around the notificationmanager. But doesnt change a thing, when running the code locally i still dont get an email saved to the disk

 
Theodor Perrier
Reply

Bump

 
Anders Ebdrup
Reply

Hi Theodor,

 

Have you tried using a non-async function?

 
Theodor Perrier
Reply

Hi Anders,

Thanks for the reply.

I have just tried your suggestion unfortunately, it doesn't change the outcome :/

 
Theodor Perrier
Reply

After testing, I've tried triggering the workflow notification with a pibeline it works and sends an email

 

using Dynamicweb.Ecommerce.Notifications;
using Dynamicweb.Ecommerce.Products;
using Dynamicweb.Extensibility.Notifications;
using Dynamicweb.Host.Core;

namespace Website.Pibelines;

public class WorkflowNotificationTrigger : IPipeline
{
    public int Rank => 150;

    public void RegisterApplicationComponents(IApplicationBuilder app)
    {

    }

    public void RegisterServices(IServiceCollection services, IMvcCoreBuilder mvcBuilder)
    {
    }

    public void RunInitializers()
    {
        Product theProduct = Dynamicweb.Ecommerce.Services.Products.GetProductById("MT1135_75D_COMFORT_5", "", "LANG1");

        if (theProduct == null)
            return;
        

        NotificationManager.Notify(Ecommerce.Product.ProductWorkflowStateChanged, new Ecommerce.Product.ProductWorkflowStateChangedArgs(theProduct, 0));


    }
}

 
Theodor Perrier
Reply

After doing some debugging, I found, that i get this error in the eventviewer.
But only in the emmit im doing from the scheduled addin.
Is there a difference in how the 

2025-10-10 10:49:15.8393: The notification subscriber threw an unexpected error. System.NullReferenceException: Object reference not set to an instance of an object.
   at Dynamicweb.Ecommerce.Products.ProductWorkflowStateChangedSubscriber.GetHost()
   at Dynamicweb.Ecommerce.Products.ProductWorkflowStateChangedSubscriber.PopulateMailContent(Template template, WorkflowNotification notification, Product product, WorkflowState previousState, WorkflowState currentState)
   at Dynamicweb.Ecommerce.Products.ProductWorkflowStateChangedSubscriber.SendEmailNotification(WorkflowNotificationService notificationService, WorkflowNotification notification, Product Product, WorkflowState previousState, WorkflowState currentState)
   at Dynamicweb.Ecommerce.Products.ProductWorkflowStateChangedSubscriber.OnNotify(String notification, NotificationArgs arguments)
   at Dynamicweb.Extensibility.Notifications.NotificationManager.Notify(String notification, NotificationArgs eventArgs)

 

 

This is a snippet from the notificationsubsciper, is is possible that Context or part of the Context object is null in the addin context? And how do I ensure that it doesnt fail?
        private string GetHost()
        {
            // Write the port if it Is Not default
            bool disablePortNumber = string.Equals(SystemConfiguration.Instance.GetValue("/Globalsettings/System/http/DisableBaseHrefPort"), "True", StringComparison.OrdinalIgnoreCase);
            string portString = Context.Current.Request.Url.IsDefaultPort || disablePortNumber ? string.Empty : string.Format(":{0}", Context.Current.Request.Url.Port);
            return string.Format("{0}://{1}{2}", Context.Current.Request.Url.Scheme, Context.Current.Request.Url.Host, portString);
        }

 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi

I have created https://doc.dynamicweb.dev/documentation/fundamentals/dw10release/releasenotes/workiteminfo.html?workitemid=25918 to make the ProductWorkflowStateChangedSubscriber able to run outside the context of request (a link in the email would be missing though).

Remember to remove that ipipeline thingie... :-)

 
Theodor Perrier
Reply

Hi Nicolai.

Thanks! Looking forward to the fix :)

The IPibline is only in my local version :) But thanks

 

You must be logged in to post in the forum