Developer forum

Forum » Ecommerce - Standard features » Is there a way to create a scheduled task that sets productname of all variants equal to productname of the master product?

Is there a way to create a scheduled task that sets productname of all variants equal to productname of the master product?

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

Is there a way to create a scheduled task that sets productname of all variants  equal to productname of the master product?

if so, how could this be done?

/Hans


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

You probably should change the inheritance of the product name field so variants cannot have different names.

Otherwise you can create a SQL scheduled task instance and update the records like this (untested with real data):

update EcomProducts set productname = (select top 1 ProductName from EcomProducts as secondproducts where secondproducts.ProductID = ecomproducts.ProductID and secondproducts.ProductLanguageID = ecomproducts.ProductLanguageID and secondproducts.ProductVariantID ='') where ProductVariantid <> ''
Votes for this answer: 1
 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thank you Nicolai. We have an import batch of 5 stepst in the Data Integration. Is it possible to run this scheduled task as a part of that batch or right after somehow?

/Hans

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

You can create a Notification Subscriber for JobFinished, check for the correct job name and then  execute your SQL code. Here's an example that does that You need to add the code to a class file in a Class Library project, compile it to a DLL and add that DLL to your site's Bin folder:

using System;
using Dynamicweb.DataIntegration.Integration.Notifications;
using Dynamicweb.Extensibility.Notifications;

namespace Your.Namespace.Here
{
  [Subscribe(Integration.JobFinished)]
  public class SyncVariantNames : NotificationSubscriber
  {
    private Dynamicweb.Logging.ILogger _logger;

    public override void OnNotify(string notification, NotificationArgs args)
    {
      var integrationArgs = (Integration.JobFinishedIntegrationArgs)args;
      _logger = integrationArgs.Job.Logger;
      try
      {
        if (integrationArgs.JobFailed || !integrationArgs.Job.Name.Contains("Your Job Name"))
        {
          _logger.Log($"Job failed: {integrationArgs.JobFailed} Name: {integrationArgs.Job.Name}");
          return;
        }
        var sql = "...";
        var recordsAffected = Dynamicweb.Data.Database.ExecuteNonQuery(sql);
        _logger.Log($"Done syncing variant names. Records affected: {recordsAffected}");
      }
      catch (Exception ex)
      {
        _logger.Error("Error syncing names.", ex);
      }
    }
  }
}

Hope this helps,

Imar

 

Votes for this answer: 1
 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thank you Imar and Nicolai 👍

/Hans

 

You must be logged in to post in the forum