Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This article explains how to create the a form to add and insert or update data in the database using a Groovy script.Before we proceed with the instructions, below are the prerequisitethe Run macro.

Prerequisites

  1. Install the following apps in your Confluence instance:
    1. Run Self-Service Reports
    app.
    1. SQL for Confluence (Pro Edition)
    app.
  2. Follow this link to configure the data source profile for SQL for Confluence app.

Instructions

...

  1. Log into Confluence.

  2. Navigate to the required existing page and click  to edit the page or create a new page.
  3. Insert Wiki Markup macro to write the script.and as an example, you can paste the code given below:

    Image Added

    Code Block
    languagegroovy
    themeMidnight
    {wiki}
    {run:titleRun=DATA Display|anchor=result}
    
            {run}
    
              {run:id=update|replace=choice:Add::radio::Add::Update,id:1:Id (for update):integer-select:1:1000,type:dog:Select DB:select::MYSQL_new user:MYSQL_new          user:Mysql_confluence:Mysql_confluence,hand1:default:operational_hand_off_status:text,hand2:default:project_status:text,hand3:default:Resource Type:text|titleRun=Add|autoRun=true|anchor=result|focus=choice}
               {anchor:result}
    
                {hide:showIf=Add:$choice}
    
                {sql:dataSource=Mysql_confluence|contentPropertyPrefix=sql.}
    
                INSERT INTO report (Project_status,Resource_type,operational_hand_off_status)VALUES('$hand2','$hand3','$hand1');
    
                {sql}
    
                *Row Updated*
    
                {hide}
    
                {hide:hideIf=Add:$choice}
                {sql:dataSource=Mysql_confluence|contentPropertyPrefix=sql.}
                UPDATE report set Project_status = '$hand2', Resource_type ='$hand3',operational_hand_off_status = '$hand1' where report_id = $id
                {sql}
                {hide}
            
    {run}
            
      {run-now}
                {hide:showIf=1:%property_sql.update_count%}
                *Row Added*
                {hide}
    
      {run-now}
                ----
                h3. Results
                {sql-query:dataSource=MYSQL_new user}
                select report_id,Project_status,Resource_type,operational_hand_off_status from report
                {sql-query}
           
      
    {wiki}


  4. Click Save and the form will be displayed below:
    Image Removed
  5. Select the option: Add. After entering the data for the required field, Click Add.
    Image Removed
  6. Data will be added to the database and displayed as shown below:
    Image Removed
  7. Select the option: Update. 
     a. Select the Id for which you want to update the record.
     b. Enter the value for the respective field you want to update.
     b. Click Add.
    Image Removed

...

  1. Insert, and then Save. The macro is displayed on the page as follows:
    Image Added
  2. Select the choice field; Add inserts a new record and Update edits the record.
  3. Enter data in the required fields and click Add to save.
    Image Added

Data is added or updated in the database. As per the example mentioned above, the result is displayed as shown:
Image Added

Info
  • Test the function in a non-production environment before deploying it on production.