How to write to task log from scheduled Functions

I am searching for a way to write to task log from scheduled Function. What is preventing me from doing this is taskNum which is missing and I can’t find it.

I have to imagine that is simply database sequenced from the SysSequence table. They have built in methods for managing those.

//Global
Ice.Lib.NextValue.dll
new Ice.Lib.NextValue(Db).GetNextSequence("SomeSequenceMakeItCustomIfYouWant").ToString("00000"); // Throw a tostring if you need something front padded with zero
 
//Company Specific
Erp.Internal.Lib.Shared.dll
using (var libCompanySequence = new Erp.Internal.Lib.CompanySequence(Db))
{
    var nextCustID= libCompanySequence.GetNextCompanySequence(Session.CompanyID, "SomeSequenceMakeItCustomIfYouWant").ToString();
}

Something to that effect where the quoted string is your key. You would have to go to the sys sequence table to see what that key is.

TaskNumber is assigned when task is executed and passed to RunProcess method. Therefore in custom dll it was possible to get it by i.e. overriding RunProcess method. It is not so simple as sequence which is stored for other things.

Did you have any luck on this one? I have a large function that does a lot of work and I would love to write some better information to the Task Instead of the Epicor log.

You ever figure this one out @Jason_Woods

Nope…

Excuse my ignorance, but what “exactly” are y’all referring to when you say “task log”?

Its a logged task.

ummmm ok…

In the System Monitor under History Tasks → Report/Task Logs

This should get the job done, could convert this to its own function and call it whenever is needed from another function. Should be useful as well to write out logging in cloud when debugging functions (or bpms) with no event viewer access

Action<string> writeToTaskLog = (message) => {
  foreach (var activeTask in Db.SysTask.Where(t => t.Company == Session.CompanyID && t.TaskDescription.ToLower() == "run epicor function" && t.TaskStatus.ToLower() == "active"))
  {
    var taskLog = Db.SysTaskLog.FirstOrDefault(t => t.SysTaskNum == activeTask.SysTaskNum && t.MsgText.ToLower().Contains(this.LibraryID.ToLower()));
    if (taskLog != null)
    {
      this.CallService<Ice.Contracts.SysMonitorTasksSvcContract>(sm =>{sm.WriteToTaskLog(message, taskLog.SysTaskNum, Epicor.ServiceModel.Utilities.MsgType.Info);});
    }
  }
};

I don’t think in the SysTask or SysTaskLog does it indicate the actual function running, hence why I had to find the LibraryID in the active SysTaskLog record

8 Likes

Worked great - thanks!

Just a heads up, this will break in 2023 since the task description now shows the name and library of the function running. I just changed the task description condition to a contains and that seems to work so far.

1 Like

I did see that they changed the description for functions in the newest version. I haven’t had to use this log writing yet there, but I will add to my post to include one for these new changes, thanks for reminding me

I already have tweaked my function schedule with the name of the library so I guess I was ahead of the game for once! I got tired of seeing a bunch of “Run Epicor Function” entries in my history and not knowing which one they were related to.

1 Like