<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.idempiere.org/w-en/index.php?action=history&amp;feed=atom&amp;title=HowTo%3A_Run_Process_from_Database_Function</id>
	<title>HowTo: Run Process from Database Function - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.idempiere.org/w-en/index.php?action=history&amp;feed=atom&amp;title=HowTo%3A_Run_Process_from_Database_Function"/>
	<link rel="alternate" type="text/html" href="https://wiki.idempiere.org/w-en/index.php?title=HowTo:_Run_Process_from_Database_Function&amp;action=history"/>
	<updated>2026-09-30T00:45:29Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.2</generator>
	<entry>
		<id>https://wiki.idempiere.org/w-en/index.php?title=HowTo:_Run_Process_from_Database_Function&amp;diff=23506&amp;oldid=prev</id>
		<title>CarlosRuiz: Document</title>
		<link rel="alternate" type="text/html" href="https://wiki.idempiere.org/w-en/index.php?title=HowTo:_Run_Process_from_Database_Function&amp;diff=23506&amp;oldid=prev"/>
		<updated>2025-09-15T16:03:47Z</updated>

		<summary type="html">&lt;p&gt;Document&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In iDempiere is possible to run a process based on a database pl/pgsql postgres function, but the process needs to do certain things to run correctly.&lt;br /&gt;
&lt;br /&gt;
1. The database function receives only one parameter with the AD_PInstance_ID, meaning the instance of the process being executed.&lt;br /&gt;
&lt;br /&gt;
You can derive from AD_PInstance_ID all the information required like AD_Client_ID, CreatedBy (for the user running the process), Record_ID, etc.&lt;br /&gt;
&lt;br /&gt;
You can also get the parameters from AD_PInstance_Para&lt;br /&gt;
&lt;br /&gt;
2. The return of the database function is ignored, so is OK to return simply void.&lt;br /&gt;
&lt;br /&gt;
3. You can inform progress or results inserting into AD_PInstance_Log&lt;br /&gt;
&lt;br /&gt;
4. You MUST update AD_PInstance to report success or error and a message:&lt;br /&gt;
* Result=1 -&amp;gt; Success and the ErrorMsg is shown to the user&lt;br /&gt;
* Result!=1 -&amp;gt; Failure and the ErrorMsg is shown to the user&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
5. In the dictionary you just need to register the name of the database function in the field AD_Process.ProcedureName&lt;br /&gt;
&lt;br /&gt;
6. Be careful about using this approach: As you are executing pure direct SQL here, there is no usage of the PO class, so you don't have any of the advantages of using the iDempiere java model, like:&lt;br /&gt;
* no validations (range, foreign keys, cross tenants, etc)&lt;br /&gt;
* no change log&lt;br /&gt;
* no automatic translations&lt;br /&gt;
* no automatic trees&lt;br /&gt;
* no automatic delete of children&lt;br /&gt;
* etc&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is an example that you can use as template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=sql&amp;gt;&lt;br /&gt;
-- DROP FUNCTION procedure_example();&lt;br /&gt;
&lt;br /&gt;
CREATE OR REPLACE FUNCTION procedure_example(p_instanceid numeric)&lt;br /&gt;
 RETURNS void&lt;br /&gt;
 LANGUAGE plpgsql&lt;br /&gt;
AS $BODY$&lt;br /&gt;
DECLARE&lt;br /&gt;
    p_record_id int;&lt;br /&gt;
    l_charge_name varchar(100);&lt;br /&gt;
    l_cnt integer := 0;&lt;br /&gt;
BEGIN&lt;br /&gt;
    SELECT i.record_id&lt;br /&gt;
        INTO p_record_id&lt;br /&gt;
        FROM ad_pinstance i&lt;br /&gt;
        WHERE i.ad_pinstance_id=p_instanceid;&lt;br /&gt;
&lt;br /&gt;
   SELECT Name&lt;br /&gt;
	       INTO l_charge_name&lt;br /&gt;
	   FROM C_Charge&lt;br /&gt;
	   WHERE C_Charge_ID = p_record_id;&lt;br /&gt;
&lt;br /&gt;
    l_cnt := l_cnt + 1;&lt;br /&gt;
    INSERT INTO AD_PInstance_Log (AD_PInstance_ID, Log_ID, P_Msg, AD_PInstance_Log_UU)&lt;br /&gt;
     VALUES(p_instanceid, 0, 'To record information or progress you can insert into AD_PInstance_Log: ' || l_cnt, generate_uuid());&lt;br /&gt;
&lt;br /&gt;
    /* Register exit message and success updating AD_PInstance with Result=1, use Result!=1 for errors */&lt;br /&gt;
    UPDATE AD_PInstance SET Result=1, ErrorMsg='Everything was OK' WHERE AD_PInstance_ID=p_instanceid;&lt;br /&gt;
&lt;br /&gt;
END;&lt;br /&gt;
$BODY$&lt;br /&gt;
;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:HowTo-Technical]]&lt;/div&gt;</summary>
		<author><name>CarlosRuiz</name></author>
	</entry>
</feed>