Groovy Scripting Examples

This topic contains examples of application changes that you can perform using Groovy scripts.

Application Composer leverages Groovy to enable you to enhance your application changes. Groovy is a standard, dynamic scripting language for the Java platform for which the Application Composer provides support.

The following examples are covered in this topic:

  • Using println function

  • Making fields conditionally required

  • Making fields conditionally updatable

  • Adding validation to fields

Note: It is assumed that a custom object called Help Request exists and is available on the Navigator menu.

Using Println Function

In this example, you add a println function to a trigger to view an opportunity's Win Probability. Whenever the Win Probability field is updated, the println function in the trigger performs an update, and you can see the output in the Runtime Messages page within Application Composer.

To view an opportunity's Win Probability:

  1. Navigate to Application Composer.

  2. Expand Standard Objects and then expand Opportunity.

  3. Under Opportunity, click Server Scripts.

  4. Under the Triggers tab, select Action > Add for Field Triggers.

  5. In the Trigger field, select Before Update in Database.

  6. In the Trigger Name field, enter TestPrintln.

  7. Under the Trigger Definition region, enter the following script in the expression text box:

    println("Before Update Trigger. The new value of the Win Probability is" + nvl(WinProb, "Win Probability was null")).

    nvl() ensures that the variables are null-aware.

  8. Click the Validate icon. Confirmation appears when the script is parsed successfully.

  9. Click OK.

  10. In the Navigator menu, click Opportunities.

  11. Click Create Opportunity in the left pane.

  12. In the Name field, enter Opportunity Trigger Test.

  13. In the Win Probability (%) field, enter 50.

  14. Click the Save and Close button.

  15. On the Overview page, select the Opportunities tab.

  16. Click the Opportunity Trigger Test link in the table.

  17. In the Win Probability (%) field, enter 25.

  18. Click the Save and Close button.

  19. Navigate to Application Composer.

  20. On the Overview page, click Runtime Messages.

  21. Click the Get Latest Log Messages button.

  22. Locate the message that you wrote in your println function.

    Tip: Sort the messages in descending order to locate quickly.

Making Fields Conditionally Required

In this example, you add the following two custom fields to the Help Request object. You make one of them conditionally required based on the value you select in the other field.

  1. Priority field of type Fixed Choice List.

  2. Justification field of type text that's conditionally set to required depending on the value in the Priority field.

    If the value in the Priority field is set to Urgent, then the Justification field appears as a mandatory or required field. Else, the Justification field remains optional.

To create fields in application composer and make one of them conditionally required:

  1. Navigate to Application Composer.

  2. Expand Custom Objects.

  3. Expand Help Request and click Fields.

  4. Under Custom tab, select Action > Create.

  5. In the Select Field Type window, select Text and click OK.

  6. On the Create Text Field page, enter Justification in the Display Label.

  7. Click Save and Close.

  8. On the Fields page, select Action > Create under the Custom tab.

  9. In the Select Field Type window, select Choice List (Fixed) and click OK.

  10. On Create Fixed Choice List page, enter Priority in the Display Label.

  11. Under the List of Values region, click the Create a New Lookup Type icon.

  12. In Create Lookup Type dialog, specify the following:

    Field

    Value

    Meaning

    Help Request Priority

    Lookup Type

    HR_PRIORITY

  13. Select Action > Create, and specify the following in the first row:

    Column

    Value

    Meaning

    Urgent

    Lookup Code

    URG

    Display Sequence

    1

  14. Select Action > Create, and specify the following in the second row:

    Column

    Value

    Meaning

    Important

    Lookup Code

    IMP

    Display Sequence

    2

  15. Click Save.

  16. On Create Fixed Choice List page, select the Fixed Value option in the Default Value region, and then select Important as the default value.

  17. Click the Save and Close button.

    Fields page opens.

  18. Under the Custom tab, click the Justification link.

    Edit Text Field: Justification page opens.

  19. Under Constraints region, select Required check box and click the expression builder icon next to it.

    Expression builder dialog opens.

  20. Enter the following into the (script) text box:

    try { if(nvl(Priority_c,"") == "URGENT") { return true } else { return false } } catch(e) { println("Error with the Required property of the Justification field in the Help Request object") }

  21. Click the OK button.

  22. Under Constraints region, select Priority from the Depends On list.

  23. Click the Save and Close button.

  24. Under Help Request in the left pane, click Pages.

    Help Request: Pages page opens.

  25. Click the Edit Summary Form link.

    Edit Details Page Summary Form page opens.

  26. Under Configure Default Summary region, move the Priority and Justification fields to the Selected Fields box. Ensure that Priority field is above the Justification field. If it's not, use the up or down arrow button on the right to adjust the sequence.

  27. Click the Save and Close button.

  28. In the Navigator menu, click Help Request.

  29. Click any active Help Request in the list to open its edit page.

  30. Select Important from the Priority list.

  31. Click the Save button.

    The help request has been saved.

  32. Now select Urgent in the Priority list.

    An asterisk appears next to the Justification field that indicates a required field.

  33. Click the Save button.

    An error message appears, because you didn't enter a value in the Justification field before saving.

  34. Enter Laptop is on fire in the Justification field.

  35. Click the Save and Close button.

    The help request is now saved.

Making Fields Conditionally Updatable

In this example, you add the following two custom fields to your Help Request object and then enter a script to make one of them conditionally updatable.

  1. Executive Sponsor Program check box

  2. Executive Sponsor text field

If the Executive Sponsor Program check box is selected, the Executive Sponsor field can be updated. Else, the Executive Sponsor field is disabled.

To create these fields and make one of them conditionally updatable:

  1. Navigate to Application Composer.

  2. Expand Custom Objects.

  3. Expand Help Request and click Fields.

  4. Under Custom tab, select Action > Create.

  5. In the Select Field Type window, select Text and click OK.

  6. On the Create Text Field page, enter Executive Sponsor in the Display Label.

  7. Click Save and Close.

  8. On the Fields page, select Action > Create under the Custom tab.

  9. In the Select Field Type window, select Check box and click OK.

  10. On the Create Text Field page, enter Executive Sponsor Program in the Display Label.

  11. Click Save and Close.

  12. On the Fields page, click the Executive Sponsor link.

    Edit Check box Field: Executive Sponsor page opens.

  13. Under Constraints region, select the Updatable check box and click the expression builder icon next to it.

    Expression builder dialog opens.

  14. Enter the following into the (script) text box:

    try{ if(nvl(ExecutiveSponsorProgram_c,"") == "N") { return false } else { return true } } catch(e) { println("Error with the Updatable property of Executive Sponsor field in the Help Request object") }

  15. Click the OK button.

  16. Under Constraints region, select Executive Sponsor Program from the Depends On list.

  17. Click the Save and Close button.

  18. Under Help Request in the left pane, click Pages.

    Help Request: Pages page opens.

  19. Click the Edit Summary Form link.

    Edit Details Page Summary Form page opens.

  20. Under Configure Default Summary region, move the Executive Sponsor and Executive Sponsor Program fields to the Selected Fields box.

    Ensure that Executive Sponsor Program field is above the Executive Sponsor field. If it's not, use the up or down arrow button on the right to adjust the sequence.

  21. Click the Save and Close button.

  22. In the Navigator menu, click Help Request.

  23. Click any active Help Request in the list to open its edit page.

  24. Select the Executive Sponsor Program check box, and click the Save button.

    At this point, you can enter a name into the Executive Sponsor field.

  25. Now clear the selection in the Executive Sponsor Program check box, and then click the Save button.

    You can't enter anything in the Executive Sponsor field, because you haven't selected the Executive Sponsor Program check box.

Adding Validations to Fields

In this example, you add a validation to a field using an expression. You first add a custom text field called Corporate E-Mail to your Help Request object, and then you add a validation to check the syntax of the e-mail address.

To create a field and add validation:

  1. Navigate to Application Composer.

  2. Expand Custom Objects.

  3. Expand Help Request and click Fields.

  4. Under Custom tab, select Action > Create.

  5. In the Select Field Type window, select Text and click OK.

  6. On the Create Text Field page, enter Corporate E-Mail in the Display Label.

  7. Click Save and Close.

  8. In the left pane, click Pages under Help Request.

    Help Request: Pages opens.

  9. Under Creation Page region, click Edit Creation Page.

    Edit Creation Page opens.

  10. Under Configure Creation page region, move Corporate E-Mail to the Selected Fields box.

  11. Click the Save and Close button.

  12. Under Details Page region, click Edit Summary Form.

    Edit Details Page Summary Form page opens.

  13. In the Configure Default Summary region, move the Corporate E-Mail to the Selected Fields box.

  14. Click the Save and Close button.

  15. In the left pane, click Server Scripts under Help Request.

  16. Under Validation Rules tab, select Action > Add under the Field Rules region.

  17. Specify the following values under Create Field Validation Rule page:

    Field Name

    Value

    Field Name

    Corporate E-Mail

    Rule Name

    CheckValid

    Error Message

    Invalid syntax in Corporate E-Mail. Enter a valid syntax for e-mail address

  18. In the script textbox, enter:

    newValue == null || newValue ==~ /[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})/

  19. Click the Save and Close button.

  20. In the Navigator menu, click Help Request.

  21. Click any active Help Request in the list to open its edit page.

  22. Enter mhoope.oracle.com in the Corporate E-Mail field.

  23. Click the Save button.

    The error message that you had specified appears, because the e-mail address doesn't have proper syntax.

  24. Enter mhoope@oracle.com in the Corporate E-Mail field.

  25. Click the Save button.

    You are now able to save the Help Request after entering a valid syntax for e-mail address.