Difference between revisions of "Script Callout"

From iDempiere en
(→‎See Also: hmm both Process and Callout the same.. how to make redirect?)
 
(29 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Status: working in version > 3.3.1b<br>
+
= Creating the Rule =
  
Contributed by:<br>
+
Within the script you can use:
Quality Systems & Solutions - QSS Ltda.<br>
+
* Window context variables start with a W_ prefix
[[User:CarlosRuiz|Carlos Ruiz]]
+
* Login context variables start with G_ prefix
 +
* Parameters for callout start with A_ prefix
 +
** A_WindowNo
 +
** A_Tab
 +
** A_Field
 +
** A_Value
 +
** A_OldValue
 +
** A_Ctx
  
Improvements by:<br>
+
[[Image:01_BeanShell.png]]
E-Evolution<br>
 
[[User:vpj-cd|Víctor Pérez]]<br>
 
make it work with any java scripting language [https://scripting.dev.java.net https://scripting.dev.java.net]
 
  
 +
= Configure Callout from Table/Column=
  
== Create the Rule ==
+
[[Image:02_BeanShellCallout.png]]
  
Within the script you can use:
+
*This '''Callout''' can also be called from the '''[[Script Process|Report & Process Window]]'''. Just remember to set in the '''Rule''' window from which ''Event Type'' are you calling this Rule Script.
* Window context variables start with a single underscore _
 
* Login context variables start with two underscores __
 
* Parameters for callout start with three underscores ___
 
** ___WindowNo
 
** ___Tab
 
** ___Field
 
** ___Value
 
** ___OldValue
 
  
In standard Adempiere were uploaded jars to work with [http://groovy.codehaus.org/ groovy], [http://www.jython.org/Project/index.html jython] and [http://www.beanshell.org/ beanshell]
+
= Sample Code provided for copy/paste testing =
  
[[Image:01_BeanShell.png]]
+
On the Table and Column, callout reference:
 +
@script:beanshell:forceUpper
  
== Configure the callout ==
+
On the Rule Search Key:
 +
beanshell:forceUpper
  
[[Image:02_BeanShellCallout.png]]
+
On the Rule Script:
 +
if (A_Value != null && A_Value instanceof String) {
 +
    A_Tab.setValue(A_Field, ((String)A_Value).toUpperCase());
 +
}
 +
result = "";
  
 +
= Sample Code for Setting Payment Bank Account =
  
== Code provided for copy/paste testing ==
+
This script will set the bank account on the Payment window based on the Tender Type and Credit Card Type.
  
On the Table and Column, callout reference:
+
On the Payment Table and Column, callout reference for Tender Type and Credit Card Type:
  @script:beanshell:BP_fillDescriptionFromName
+
  @script:beanshell:payment_setpaymentprocessor
  
 
On the Rule Search Key:
 
On the Rule Search Key:
  beanshell:BP_fillDescriptionFromName
+
  beanshell:payment_setpaymentprocessor
  
 
On the Rule Script:
 
On the Rule Script:
  if (___Tab.getValue("Name") != null) {
+
import org.compiere.model.MPayment;
    ___Tab.setValue("Description", ___Tab.getValue("Name"));
+
 +
  if(A_Tab.getValue("TenderType") != null && A_Tab.getValue("CreditCardType") != null && A_Tab.getValue("CreditCardType") != "")
 +
{
 +
    MPayment pmt = new MPayment(A_Ctx, 0,null);
 +
    pmt.setTenderType(A_Tab.getValue("TenderType"));
 +
    pmt.setC_Currency_ID(A_Tab.getValue("C_Currency_ID"));
 +
    pmt.setCreditCardType(A_Tab.getValue("CreditCardType"));
 +
    pmt.setPaymentProcessor();
 +
    A_Tab.setValue("C_BankAccount_ID", pmt.getC_BankAccount_ID());
 +
    pmt = null;
 
  }
 
  }
  result = "";
+
else
 +
{
 +
    A_Tab.setValue("C_BankAccount_ID", 0);
 +
}   
 +
  result="";
 +
 
 +
=Scripting Languages=
 +
*Standard iDempiere has uploaded jars to work with [http://groovy.codehaus.org/ groovy], [http://www.jython.org/Project/index.html jython] and [http://www.beanshell.org/ beanshell]
 +
*to call a script from a '''Callout''' follow these sample syntax:
 +
**@script:beanshell:ValidateQtyOnHand
 +
**@script:groovy:ValidateQtyOnHand
 +
**@script:jython:ValidateQtyOnHand
 +
 +
*When you create the rule, you have to set in the '''Search Key''' such as:
 +
**Search Key : beanshell:ValidateQtyOnHand
 +
**Search Key : groovy:ValidateQtyOnHand
 +
**Search Key : jython:ValidateQtyOnHand
 +
 +
*Set the '''Event Type''' as ''Callout'' and '''Rule Type''' as ''JSR 223 Scripting APIs''
  
 
=See Also=
 
=See Also=
 +
*[[Script Process]] for event rule example from a '''Report & Process''' window.
 
*[[Script ModelValidatorLogin]]
 
*[[Script ModelValidatorLogin]]
 +
*[[Script ModelValidator]]
 +
*[http://red1.org/adempiere/viewtopic.php?f=45&t=1821&start=25 Aladdin Callout]
 +
[[Category:Development]]
 +
[[Category:Code snippets]]

Latest revision as of 23:35, 8 September 2016

Creating the Rule

Within the script you can use:

  • Window context variables start with a W_ prefix
  • Login context variables start with G_ prefix
  • Parameters for callout start with A_ prefix
    • A_WindowNo
    • A_Tab
    • A_Field
    • A_Value
    • A_OldValue
    • A_Ctx

01 BeanShell.png

Configure Callout from Table/Column

02 BeanShellCallout.png

  • This Callout can also be called from the Report & Process Window. Just remember to set in the Rule window from which Event Type are you calling this Rule Script.

Sample Code provided for copy/paste testing

On the Table and Column, callout reference:

@script:beanshell:forceUpper

On the Rule Search Key:

beanshell:forceUpper

On the Rule Script:

if (A_Value != null && A_Value instanceof String) {
    A_Tab.setValue(A_Field, ((String)A_Value).toUpperCase());
}
result = "";

Sample Code for Setting Payment Bank Account

This script will set the bank account on the Payment window based on the Tender Type and Credit Card Type.

On the Payment Table and Column, callout reference for Tender Type and Credit Card Type:

@script:beanshell:payment_setpaymentprocessor

On the Rule Search Key:

beanshell:payment_setpaymentprocessor

On the Rule Script:

import org.compiere.model.MPayment;

if(A_Tab.getValue("TenderType") != null && A_Tab.getValue("CreditCardType") != null && A_Tab.getValue("CreditCardType") != "")
{
   MPayment pmt = new MPayment(A_Ctx, 0,null);
   pmt.setTenderType(A_Tab.getValue("TenderType"));
   pmt.setC_Currency_ID(A_Tab.getValue("C_Currency_ID"));
   pmt.setCreditCardType(A_Tab.getValue("CreditCardType"));
   pmt.setPaymentProcessor();
   A_Tab.setValue("C_BankAccount_ID", pmt.getC_BankAccount_ID());
   pmt = null;
}
else
{
   A_Tab.setValue("C_BankAccount_ID", 0);
}    
result="";

Scripting Languages

  • Standard iDempiere has uploaded jars to work with groovy, jython and beanshell
  • to call a script from a Callout follow these sample syntax:
    • @script:beanshell:ValidateQtyOnHand
    • @script:groovy:ValidateQtyOnHand
    • @script:jython:ValidateQtyOnHand
  • When you create the rule, you have to set in the Search Key such as:
    • Search Key : beanshell:ValidateQtyOnHand
    • Search Key : groovy:ValidateQtyOnHand
    • Search Key : jython:ValidateQtyOnHand
  • Set the Event Type as Callout and Rule Type as JSR 223 Scripting APIs

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.