Calling Epicor Business Object Methods with BPM C# code

8 June 2017 - Automation in Epicor, Databases and Programming, Epicor Training and Tips - Luke - Comments Off on Calling Epicor Business Object Methods with BPM C# code

Automate with Epicor BPMs

Using C# code in Epicor BPMs to call Business Objects makes for slick automation.


In a previous blog post I’ve gone in to detail about how you can read, update, delete, and create records in Epicor using C# code in BPMs. However, up to now I’ve demonstrated how this can be done within a transaction block, and this comes with a caveat of only using this approach in specific scenarios where you are sure you will not be breaking business logic (such as User Defined fields and tables). To carry out the same type of steps in standard areas of the Epicor application you will need to call Epicor business object methods.

This blog post will demonstrate a scenario that I was recently required to put a solution in place for. I’ve cut the code down a little for the purposes of this example, but it provides a nice short demonstration of how to read and then update an existing Job Header record in Epicor by using the ‘GetByID’ and ‘Update’ methods of the JobEntry business object.

1 – Automatically release a job when issuing material.

The Epicor error message below is produced whenever a user tries to issue material to a job that is not marked as ‘Released’.

Epicor Error Message

If you wish to automate the process of marking the job as ‘Released’, then the first thing you’ll need to do is identify the business object method that you should be creating your BPM on. You can use the Epicor tracing tool for this – to switch it on just use the option shown below, which you’ll find in the Epicor ‘Settings’ menu (in the latest version of Epicor there is also an icon in the hidden panel at the base of the screen):

Epicor Tracing Tool

Once you have it switched on, just manually reproduce the steps up to the point where you wish to intervene, and then go back to the tracing tool and use the ‘View’ button in order to examine the log. This will help you find out which method you need to add your BPM ‘hook’ on to. I used the tracing tool and found that the ‘PrePerformMaterialMovement’ method is responsible for producing the error message.

You’ll then need to create a Pre Processing BPM Method Directive (via the Method Directives Maintenance screen) on the ‘IssueReturn.PrePerformMaterialMovement’ method, so that you can take action prior to Epicor checking the status of the job. To start off with, your BPM could just consist of a single ‘Execute Custom Code’ step so, once you’ve specified a ‘Directive Name’, hit the ‘Design’ button. You’ll then need to drag on an ‘Execute Custom Code’ element from the ‘Callers’ section of the BPM designer, and link it with the ‘Start’ element as shown below:

BPM Designer Tool

Once you’ve done that just click on the ‘Execute Custom Code’ element then click on the ‘code’ link which will appear further below it, and you can begin to add your code. Before any code you enter will run or compile however, there is an important final step you need to put in place. You need to include a reference to the Epicor ‘JobEntry’ Business Object so that your code is able to access and call the methods within it. You do this by using the ‘References’ tab at the top of the same screen where you enter your custom C# code.  Highlight the ‘Assemblies’ link and then click ‘Add’ as shown below.  You then get a list of all the Epicor Business Objects so scroll through and choose the ‘Erp.Contracts.BO.JobEntry’ reference.

Add Epicor Reference

After that final step you’re ready to go – so take a look at the example code below that demonstrates how you can automatically mark a Job as ‘Released’ whenever material is being issued to it. I’d recommend some minor adjustments to this code if you wish to use this exact example in a real situation, such as checking the job first so to ensure that it is not already marked as ‘Released’, so that you don’t call the ‘Update’ method unnecessarily. You may also want to consider including a ‘Condition’ element in the BPM Designer (prior to the ‘Execute Custom Code’ element) if you wish to restrict this to running only under specific circumstances. I’ll leave those additions to you. I’ve tried to add comments within the code to help explain what specific bits are doing.

// Automatically release the 'To Job' when material is being issued

// Declare variable hJobEntry
Erp.Contracts.JobEntrySvcContract hJobEntry = null;

// Set hJobEntry
hJobEntry = Ice.Assemblies.ServiceRenderer.GetService(Db);

if (hJobEntry != null)

// Grab the ttIssueReturn record - in some cases you may wish to add a where clause here
	var ttIssueReturn_xRow = (from ttIssueReturn_Row in ttIssueReturn
	select ttIssueReturn_Row).FirstOrDefault();

	if (ttIssueReturn_xRow != null)

// Use the Job Number from the ttIssueReturn record to read the Job dataset using the GetByID method
			var JobEntryDataSet = hJobEntry.GetByID(ttIssueReturn_xRow.ToJobNum);

// Pull out the JobHead record from the dataset
			var JobTable_xRow = (from JobTable_Row in JobEntryDataSet.JobHead
			select JobTable_Row).FirstOrDefault();

// If we have a JobHead record then we can set the relevant fields and call the Update method
			if (JobTable_xRow != null)
				JobTable_xRow.JobReleased = true;
				JobTable_xRow.RowMod = "U";
				hJobEntry.Update(ref JobEntryDataSet);

I hope you find the above steps and instructions useful.  The code shown above is meant to be a small example as normally I’d have additional checks in place such as ensuring I successfully retrieved a Job dataset prior to trying to pull out the JobHead record from it. I’d also have some error handling after the ‘Update’ method to check if Epicor returned an error back at that point. Hopefully though this is a good starting point and sets you off on the right path.

Apologies for how long it’s been between blog posts – I thought I’d have more time to blog when working as a freelancer but it’s busy busy right now. I’ll be back with another blog post soon to show you how to do a similar thing in order to create new records in Epicor.

Tags: , , ,

Comments are closed.