REST Api Scheduling a Job

Hello,

This is my first time using Epicor REST services. I am building a web page that creates and schedules jobs.
The code creates the job, gets the details for the materials but it won’t schedule the job.

This is my code for scheduling the job:

private static void ScheduleJob(string _serviceUrl, string jobNum)
    {
        Console.WriteLine("Schedule Job");
        var scheduleJob = new ScheduleEngineModel
        {
            Company = "MyCompany",
            JobNum = jobNum,
            AssemblySeq = 0,
            OprSeq = 0,
            OpDtlSeq = 0,
            StartDate = DateTime.Now,
            StartTime = 0,
            EndDate = DateTime.Now,
            EndTime = 43200,
            SchedTypeCode = "JJ",
            ScheduleDirection = "End",
            OverrideMtlCon = true,
            OverRideHistDateSetting = 2,
            RowMod = "A"
        };
        string scheduleJobToSend = "{\"ds\":" + JsonConvert.SerializeObject(scheduleJob) + "}";

        var client = new RestClient(string.Format("{0}{1}", _serviceUrl, "Erp.BO.ScheduleEngineSvc/MoveJobItem()"));
        var request = GetNewRequest();
        request.AddParameter("application/json; charset=utf-8", scheduleJobToSend, ParameterType.RequestBody);
        request.RequestFormat = DataFormat.Json;
        var content = GetContent(client, request);
        Console.WriteLine(content);
    }

    private static string GetContent(RestClient client, RestRequest request)
    {
        string content = "";
        try
        {
            IRestResponse response = client.Execute(request);
            content = response.Content;
        }
        catch (Exception ex)
        {
            content = ex.Message;
        }
        return content;
    }

This what the service returns:

{“parameters”:{“l_finished”:true,“c_WarnLogTxt”:""}}

It seems that it finished successfully but when I open the job in Epicor, the job is not scheduled.

I used a workflow as the blueprint for the web page. This is the what the conversion that schedules the job in the workflow looks like:

What am I missing?

Thanks for your help.

Going big for a first attempt! Good for you.

Have you tried using a utility like Postman to get it to work there first and then work your way back into the code? You may get better information messages.

Mark W.

2 Likes

One big piece of advice I can offer is for you to download EpicorREST api developed by @josecgomez and @jgiese.wci. It will make your life much easier. It is available on NuGet

Also, I second @Mark_Wonsil, POSTMAN is invaluable for quickly testing your calls.

Finally, never underestimate the power of a client trace. There are many posts on the forum on how to trace logic. It will expose all of the BO’s\calls needed for a specific action.

2 Likes

@Mark_Wonsil @Chris_Conn
Thanks for the quick response!

@Mark_Wonsil I will try POSTMAN. I will report back the results.

@Chris_Conn I downloaded the utility and created the class for the JobEntrySvc but I don’t know how to use the class generated. Do you have an example on how to use it? Or, can you point me to an example?

Thanks a lot!

Here are some examples

One thing to note is that the scheduling engine is a bit of a bear… The way I’ve done it via REST is to use a UBAQ and write the scheduling code in the UBAQ Update Method. Here’s an example

ScheduleEngineTableset seTs = new ScheduleEngineTableset();
var schedEng =(ScheduleEngineRow) seTs.ScheduleEngine.NewRow();
schedEng.Company = callContextClient.CurrentCompany;
schedEng.JobNum = newJobNum;
schedEng.AssemblySeq = 0;
schedEng.OprSeq = 0;
schedEng.OpDtlSeq = 0;
schedEng.StartDate = DateTime.Now.AddDays(njds.JobHead[0].KitTime);
schedEng.StartTime =0;
schedEng.EndDate = njds.JobHead[0].ReqDueDate;
schedEng.WhatIf = false;
schedEng.Finite = false;
schedEng.OverrideMtlCon = njds.JobHead[0].IgnoreMtlConstraints;
schedEng.OverRideHistDateSetting = 2;
schedEng.SetupComplete =false;
schedEng.ProductionComplete = false;
schedEng.SchedTypeCode = "ja";
schedEng.ScheduleDirection = "End";
schedEng.RecalcExpProdYld = false;
schedEngine.GetSchedulingMultiJobFlags(out _schedulingMultiJobActive, out _minimizeWIPFlag, out _allowMoveJobsAcrossPlants, out _autoLoadParentJobs, out _autoLoadChildJobs);


schedEng.UseSchedulingMultiJob = _schedulingMultiJobActive;
schedEng.SchedulingMultiJobMinimizeWIP = _minimizeWIPFlag;
schedEng.SchedulingMultiJobIgnoreLocks=false;
schedEng.SchedulingMultiJobMoveJobsAcrossPlants = _allowMoveJobsAcrossPlants;
schedEng.SysRowID = njds.JobHead[0].SysRowID;
schedEng.RowMod= "A";
seTs.ScheduleEngine.Add(schedEng);
string txt="";
bool finished;

schedEngine.MoveJobItem(seTs, out finished, out txt);
schedEngine.AcceptChanges(string.Empty,seTs);
2 Likes

@josecgomez Thanks!

I’ll check it out.

I tried downloading the EpicorRESTAPI package but I get this error (I tried all versions):

Could not install package ‘EpicorRESTAPIStandard 1.1.4’. You are trying to install this package into a project that targets ‘.NETFramework,Version=v4.5.2’, but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

About the BAQ: Which tables did you include in it? In which schema do I find them?

Thanks!

Upgraded your .net version the package needs 4.7.2 you are trying to compile to 4.5
do you need 4.5?
Change your project to 4.7.2 or greater

The table doesn’t matter, is just an excuse to execute the Code… I think I just put Company it it … to put something. But all that I use is the Params to pass in the Jobnum I wanna schedule and the date etc

An Epicor’s agent pointed out the problems with my code. I have to pass the dataset as a list of objects (even though is just one). Not just one object. Notice how I instantiate a list of ScheduleEngine objects:

private static void ScheduleJob(string _serviceUrl, string jobNum)
    {
        Console.WriteLine("Schedule Job");

        string startEndDate = DateTime.Now.ToString("o");

        var scheduleEngineDataSet = new ScheduleEngineDataSet()
        {
            ScheduleEngine = new List<ScheduleEngine>()
            {
                new ScheduleEngine()
                {
                    Company = "TMC",
                    JobNum = jobNum,
                    AssemblySeq = 0,
                    OprSeq = 0,
                    OpDtlSeq = 0,
                    StartDate = startEndDate,
                    StartTime = 0,
                    EndDate = startEndDate,
                    EndTime = 43200,
                    WhatIf = false,
                    Finite = false,
                    SchedTypeCode = "JJ",
                    ScheduleDirection = "END",
                    SetupComplete = false,
                    ProductionComplete = false,
                    OverrideMtlCon = true,
                    OverRideHistDateSetting = 2,
                    RecalcExpProdYld = false,
                    UseSchedulingMultiJob = false,
                    SchedulingMultiJobIgnoreLocks = false,
                    SchedulingMultiJobMinimizeWIP = false,
                    SchedulingMultiJobMoveJobsAcrossPlants = false,
                    RowMod = "A"
                }
            }
        };

        string scheduleJobToSend = "{\"ds\":" + JsonConvert.SerializeObject(scheduleEngineDataSet) + "}";

        var client = new RestClient(string.Format("{0}{1}", _serviceUrl, "Erp.BO.ScheduleEngineSvc/MoveJobItem()"));
        var request = GetNewRequest(Method.POST);
        request.AddParameter("application/json; charset=utf-8", scheduleJobToSend, ParameterType.RequestBody);
        request.RequestFormat = DataFormat.Json;
        var content = GetContent(client, request);
        Console.WriteLine(content);
    }

The JSON looks like this:

     {
  "ds": {
    "ScheduleEngine": [
      {
        "Company": "TMC",
        "JobNum": "2430",
        "AssemblySeq": 0,
        "OprSeq": 0,
        "OpDtlSeq": 0,
        "StartDate": "2019-04-01T22:43:06.555Z",
        "StartTime": 0,
        "EndDate": "2019-04-01T22:43:06.555Z",
        "EndTime": 43200,
        "WhatIf": false,
        "Finite": false,
        "SchedTypeCode": "JJ",
        "ScheduleDirection": "END",
        "SetupComplete": false,
        "ProductionComplete": false,
        "OverrideMtlCon": true,
        "OverRideHistDateSetting": 2,
        "RecalcExpProdYld": false,
        "UseSchedulingMultiJob": false,
        "SchedulingMultiJobIgnoreLocks": false,
        "SchedulingMultiJobMinimizeWIP": false,
        "SchedulingMultiJobMoveJobsAcrossPlants": false,
        "RowMod": "A"
      }
    ],
  
  }
}       

The other issue was the fields I had on the ScheduleEngine object. I was missing several. After adding all the necessary fields, it worked. Those same fields were in the trace I did but I overlooked them.

Thanks everyone for your help.

2 Likes

Thanks for posting the solution @juansalas!

Hi, I am working on something similar to this and was just wondering if you could post the JSON for creating the job. Would really help, thank you.

best way to get this is to do it yourself… that way you will get the correct version of the schema for YOUR system.
The domain name yourservername.com is for sale | Dan.com will take you to the rest help page. once there, you find the API you want to work with. For example, I went and searched for the QuickJob entry (the easiest way to create a job) and saw this screen… below the screen is the JSON that I copied.

{
  "ds": {
    "QuickJob": [
      {
        "JobNum": "string",
        "PartNum": "string",
        "RevisionNum": "string",
        "PartDesc": "string",
        "Quantity": 0,
        "Firm": true,
        "Engineered": true,
        "Released": true,
        "WarehouseCode": "string",
        "WarehouseDesc": "string",
        "BinNum": "string",
        "BinDesc": "string",
        "LegalNumber": "string",
        "JobDate": "2022-06-15T13:12:21.153Z",
        "EnableEngineered": true,
        "EnableReleased": true,
        "AlternateMethod": "string",
        "QuantityUOM": "string",
        "Scheduled": true,
        "ContractID": "string",
        "TravelerReadyToPrint": true,
        "TranDocTypeID": "string",
        "PartAttrClassID": "string",
        "DynAttrValueSetDescription": "string",
        "DynAttrValueSetShortDescription": "string",
        "AttributeSetID": 0,
        "PartTrackInventoryAttributes": true,
        "SysRowID": "00000000-0000-0000-0000-000000000000",
        "RowMod": "string"
      }
    ],
    "LegalNumGenOpts": [
      {
        "Company": "string",
        "LegalNumberID": "string",
        "TransYear": 0,
        "TransYearSuffix": "string",
        "DspTransYear": "string",
        "ShowDspTransYear": true,
        "Prefix": "string",
        "PrefixList": "string",
        "NumberSuffix": "string",
        "EnablePrefix": true,
        "EnableSuffix": true,
        "NumberOption": "string",
        "DocumentDate": "2022-06-15T13:12:21.153Z",
        "GenerationType": "string",
        "Description": "string",
        "TransPeriod": 0,
        "PeriodPrefix": "string",
        "ShowTransPeriod": true,
        "LegalNumber": "string",
        "TranDocTypeID": "string",
        "TranDocTypeID2": "string",
        "GenerationOption": "string",
        "SysRowID": "00000000-0000-0000-0000-000000000000",
        "RowMod": "string"
      }
    ],
    "SelectedSerialNumbers": [
      {
        "Company": "string",
        "SerialNumber": "string",
        "Scrapped": true,
        "ScrappedReasonCode": "string",
        "Voided": true,
        "Reference": "string",
        "ReasonCodeType": "string",
        "ReasonCodeDesc": "string",
        "PartNum": "string",
        "SNPrefix": "string",
        "SNBaseNumber": "string",
        "SourceRowID": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
        "TransType": "string",
        "PassedInspection": true,
        "Deselected": true,
        "KitWhseList": "string",
        "RawSerialNum": "string",
        "KBLbrAction": 0,
        "KBLbrActionDesc": "string",
        "PreventDeselect": true,
        "XRefPartNum": "string",
        "XRefPartType": "string",
        "PreDeselected": true,
        "poLinkValues": "string",
        "SNMask": "string",
        "NotSavedToDB": true,
        "RowSelected": true,
        "SysRowID": "00000000-0000-0000-0000-000000000000",
        "RowMod": "string"
      }
    ],
    "SNFormat": [
      {
        "Company": "string",
        "Plant": "string",
        "PartNum": "string",
        "NumberOfDigits": 0,
        "SNMask": "string",
        "SNBaseDataType": "string",
        "SNFormat": "string",
        "LeadingZeroes": true,
        "SNPrefix": "string",
        "SNMaskSuffix": "string",
        "SNMaskPrefix": "string",
        "SNLastUsedSeq": "string",
        "HasSerialNumbers": true,
        "SysRowID": "00000000-0000-0000-0000-000000000000",
        "BitFlag": 0,
        "PartPricePerCode": "string",
        "PartTrackLots": true,
        "PartTrackSerialNum": true,
        "PartTrackDimension": true,
        "PartSalesUM": "string",
        "PartIUM": "string",
        "PartSellingFactor": 0,
        "PartPartDescription": "string",
        "SerialMaskMaskType": 0,
        "SerialMaskMask": "string",
        "SerialMaskExample": "string",
        "SerialMaskDescription": "string",
        "RowMod": "string"
      }
    ],
    "ExtensionTables": [
      {
        "Table": [
          {
            "ColumnValues": [
              {}
            ],
            "RowMod": "string",
            "SysRowID": "00000000-0000-0000-0000-000000000000"
          }
        ],
        "SystemCode": "string",
        "TableName": "string",
        "Columns": [
          {
            "ColumnName": "string",
            "ColumnType": "string"
          }
        ],
        "PrimaryKeyColumns": [
          "string"
        ],
        "PeerTableSystemCode": "string",
        "PeerTableName": "string"
      }
    ]
  }
}

3 Likes