# Update a business rule draft

> For the complete machine-readable documentation index, see [llms.txt](https://apidocs.chargebee.com/llms.txt).


[Idempotency Supported](/docs/api/idempotency)

Updates the draft version of a business rule. The first edit creates a draft from the released version, and every later edit updates that same draft. Changes made to a draft do not affect rule evaluation until the draft is released.

Send the full definition of the rule rather than only the attributes you are changing. The values you pass replace the draft instead of being merged into it, so an attribute you leave out is dropped from the draft rather than carried over.

Use this operation to revise a rule that is already in use. The released version stays in effect while you prepare the next one, which lets you stage a change and review it before it takes effect.

### Prerequisites & Constraints

Business rules must be enabled for the site.

### Impacts

**

Business rule

**

The draft version of the rule is replaced with the values you pass, and `updated_at` and `updated_by` are set. `latest_version`, `released_at`, `released_by`, and `active` are unchanged, and Chargebee keeps evaluating the released version.

### Implementation Notes

-   Retrieve the current definition before you send this request, so that you can pass it back in full. Use [Retrieve a business rule draft](/docs/api/business_rules/retrieve-a-business-rule-draft) if the rule already has a draft, or [Retrieve a business rule](/docs/api/business_rules/retrieve-a-business-rule) if it does not, which returns the released version the draft will be created from.
-   Call [Release a business rule](/docs/api/business_rules/release-a-business-rule) to promote the draft to the version that Chargebee evaluates, or [Delete a business rule draft](/docs/api/business_rules/delete-a-business-rule-draft) to discard it.

## Sample Request

#### cURL

```bash
curl  https://{site}.chargebee.com/api/v2/business_rules/custom-uuid-1/draft \
     -u {site_api_key}:\
     -d name="Apply 15 percentage discount at invoice level" \
     -d description="Apply 15 percentage discount at invoice level" \
     -d structured_expression='{"type":"GROUP","operation":"AND","children":[{"type":"CONDITION","field":"customer.language","operator":"CONTAINS","value":"en"},{"type":"CONDITION","field":"quote.shipping_address_country","operator":"ANY_OF","values":["IN","US","GB"]}]}' \
     -d actions_on_success='[{"input":{"apply_on":"invoice_amount","duration_type":"one_time","discount":15.0,"discount_type":"percentage"},"action_template_id":"action-apply-discount","type":"APPLY_DISCOUNT"}]'
```

#### .NET

```dotnet
using ChargeBee.Api;
using ChargeBee.Models;
using Newtonsoft.Json.Linq;

ApiConfig.Configure("{site}","{site_api_key}");
var structuredExpression = new JObject {
  ["type"] = "GROUP",
  ["operation"] = "AND",
  ["children"] = "[{\"type\":\"CONDITION\",\"field\":\"customer.language\",\"operator\":\"CONTAINS\",\"value\":\"en\"},{\"type\":\"CONDITION\",\"field\":\"quote.shipping_address_country\",\"operator\":\"ANY_OF\",\"values\":[\"IN\",\"US\",\"GB\"]}]"
};
EntityResult result = BusinessRule.UpdateDraft("custom-uuid-1")
		.Name("Apply 15 percentage discount at invoice level")
		.Description("Apply 15 percentage discount at invoice level")
		.StructuredExpression(structuredExpression)
		.ActionsOnSuccess(new JArray { "{\"input\":{\"apply_on\":\"invoice_amount\",\"duration_type\":\"one_time\",\"discount\":15,\"discount_type\":\"percentage\"},\"action_template_id\":\"action-apply-discount\",\"type\":\"APPLY_DISCOUNT\"}" })
		.Request();

BusinessRule businessRule = result.BusinessRule;
```

#### Go

```go
package main
import (
    "fmt"
    "github.com/chargebee/chargebee-go/v3"
    businessruleAction "github.com/chargebee/chargebee-go/v3/actions/businessrule"
    "github.com/chargebee/chargebee-go/v3/models/businessrule"
)
func main() {
    chargebee.Configure("{site_api_key}","{site}");
    res,err := businessruleAction.UpdateDraft("custom-uuid-1", &businessrule.UpdateDraftRequestParams{
        Name : "Apply 15 percentage discount at invoice level",
        Description : "Apply 15 percentage discount at invoice level",
        StructuredExpression : &businessrule.UpdateDraftStructuredExpressionParams{
            Type : "GROUP",
            Operation : "AND",
            Children : [
    map[string]interface{}{
        "type" : "CONDITION",
        "field" : "customer.language",
        "operator" : "CONTAINS",
        "value" : "en",
    },
    map[string]interface{}{
        "type" : "CONDITION",
        "field" : "quote.shipping_address_country",
        "operator" : "ANY_OF",
        "values" : []interface{}{
            "IN",
            "US",
            "GB",
        },
    }
],
        },
        ActionsOnSuccess : []*businessrule.UpdateDraftActionsOnSuccessParams{
            {
                Input : map[string]interface{}{
    "apply_on" : "invoice_amount",
    "duration_type" : "one_time",
    "discount" : 15,
    "discount_type" : "percentage",
},
                ActionTemplateId : "action-apply-discount",
                Type : "APPLY_DISCOUNT",
            },
        },
    }).Request()
    if err != nil {
        fmt.Println(err)
    } else {
        BusinessRule := res.BusinessRule
    }
}
```

#### Go

```go
package main

import (
  "fmt"
  "github.com/chargebee/chargebee-go/v4"
)

func main() {
  config := &chargebee.ClientConfig{
    SiteName: "{site}",
    ApiKey: "{site_api_key}",
  }    
  client := chargebee.NewClient(config)
  req := &chargebee.BusinessRuleUpdateDraftRequest{
    Name : "Apply 15 percentage discount at invoice level",
    Description : "Apply 15 percentage discount at invoice level",
    StructuredExpression : &chargebee.BusinessRuleUpdateDraftStructuredExpression{
        Type : "GROUP",
        Operation : "AND",
        Children : [
map[string]interface{}{
    "type" : "CONDITION",
    "field" : "customer.language",
    "operator" : "CONTAINS",
    "value" : "en",
},
map[string]interface{}{
    "type" : "CONDITION",
    "field" : "quote.shipping_address_country",
    "operator" : "ANY_OF",
    "values" : []interface{}{
        "IN",
        "US",
        "GB",
    },
}
],
    },
    ActionsOnSuccess : []*chargebee.BusinessRuleUpdateDraftActionsOnSuccess{
        {
            Input : map[string]interface{}{
"apply_on" : "invoice_amount",
"duration_type" : "one_time",
"discount" : 15,
"discount_type" : "percentage",
},
            ActionTemplateId : "action-apply-discount",
            Type : "APPLY_DISCOUNT",
        },
    },
}
  res, err := client.BusinessRule.UpdateDraft("custom-uuid-1", req)
      if err != nil {
        fmt.Println(err)
    } else {
        BusinessRule := res.BusinessRule
    }
}
```

#### Java

```java
import com.chargebee.*;
import com.chargebee.ListResult;
import com.chargebee.models.*;
import com.chargebee.models.enums.*;
import java.io.IOException;
import java.util.List;
import com.chargebee.org.json.JSONArray;
import com.chargebee.org.json.JSONObject;

public class Sample {

    public static void main(String args[]) throws IOException, Exception {
        Environment.configure("{site}", "{site_api_key}");
        Result result = BusinessRule.updateDraft("custom-uuid-1")
            .name("Apply 15 percentage discount at invoice level")
            .description("Apply 15 percentage discount at invoice level")
            .structuredExpression(new JSONObject("{\"type\":\"GROUP\",\"operation\":\"AND\",\"children\":[{\"type\":\"CONDITION\",\"field\":\"customer.language\",\"operator\":\"CONTAINS\",\"value\":\"en\"},{\"type\":\"CONDITION\",\"field\":\"quote.shipping_address_country\",\"operator\":\"ANY_OF\",\"values\":[\"IN\",\"US\",\"GB\"]}]}"))
            .actionsOnSuccess(new JSONArray("[{\"input\":{\"apply_on\":\"invoice_amount\",\"duration_type\":\"one_time\",\"discount\":15.0,\"discount_type\":\"percentage\"},\"action_template_id\":\"action-apply-discount\",\"type\":\"APPLY_DISCOUNT\"}]"))
            .request();

        BusinessRule businessRule = result.businessRule();
    }
}
```

#### Java

```java
import com.chargebee.v4.client.ChargebeeClient;
import com.chargebee.v4.models.businessRule.BusinessRule;
import com.chargebee.v4.models.businessRule.params.BusinessRuleUpdateDraftParams;
import com.chargebee.v4.models.businessRule.responses.BusinessRuleUpdateDraftResponse;
import java.util.List;
import java.util.Map;

public class BusinessRuleUpdateDraft {

    public static void main(String[] args) {
        ChargebeeClient client = ChargebeeClient.builder()
            .apiKey("{site_api_key}")
            .siteName("{site}")
            .build();

        BusinessRuleUpdateDraftParams params = BusinessRuleUpdateDraftParams.builder()
            .name("Apply 15 percentage discount at invoice level")
            .description("Apply 15 percentage discount at invoice level")
            .structuredExpression(Map.of("type", "GROUP", "operation", "AND", "children", List.of(Map.of("type", "CONDITION", "field", "customer.language", "operator", "CONTAINS", "value", "en"), Map.of("type", "CONDITION", "field", "quote.shipping_address_country", "operator", "ANY_OF", "values", List.of("IN", "US", "GB")))))
            .actionsOnSuccess(List.of("[{\"input\":{\"apply_on\":\"invoice_amount\",\"duration_type\":\"one_time\",\"discount\":15.0,\"discount_type\":\"percentage\"},\"action_template_id\":\"action-apply-discount\",\"type\":\"APPLY_DISCOUNT\"}]"))
            .build();

        BusinessRuleUpdateDraftResponse response = client
            .businessRules()
            .updateDraft("custom-uuid-1", params);

        BusinessRule businessRule = response.getBusinessRule();
    }
}
```

#### Node.js

```node
import Chargebee from "chargebee";

const chargebee = new Chargebee({
    site: "{site}",
    apiKey: "{site_api_key}",
});

try {
    const result = await chargebee.businessRule.updateDraft("custom-uuid-1", {
        name: "Apply 15 percentage discount at invoice level",
        description: "Apply 15 percentage discount at invoice level",
        structured_expression: {
            type: "GROUP",
            operation: "AND",
            children: [
                {
                    type: "CONDITION",
                    field: "customer.language",
                    operator: "CONTAINS",
                    value: "en"
                },
                {
                    type: "CONDITION",
                    field: "quote.shipping_address_country",
                    operator: "ANY_OF",
                    values: ["IN", "US", "GB"]
                }
            ]
        },
        actions_on_success: [
            {
                input: {
                    apply_on: "invoice_amount",
                    duration_type: "one_time",
                    discount: 15,
                    discount_type: "percentage"
                },
                action_template_id: "action-apply-discount",
                type: "APPLY_DISCOUNT"
            }
        ]
    });

    console.log(result);
    const businessRule = result.business_rule;
} catch (err) {
    console.log(err);
}
```

#### PHP

```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Chargebee\ChargebeeClient;

$chargebee = new ChargebeeClient(options: [
    "site" => "{site}",
    "apiKey" => "{site_api_key}",
]);
$result = $chargebee->businessRule()->updateDraft("custom-uuid-1", [
    "name" => "Apply 15 percentage discount at invoice level",
    "description" => "Apply 15 percentage discount at invoice level",
    "structured_expression" => '{"type":"GROUP","operation":"AND","children":[{"type":"CONDITION","field":"customer.language","operator":"CONTAINS","value":"en"},{"type":"CONDITION","field":"quote.shipping_address_country","operator":"ANY_OF","values":["IN","US","GB"]}]}',
    "actions_on_success" => [
        [
            "input" => [
                "apply_on" => "invoice_amount",
                "duration_type" => "one_time",
                "discount" => 15,
                "discount_type" => "percentage"
            ],
            "action_template_id" => "action-apply-discount",
            "type" => "APPLY_DISCOUNT"
        ]
    ]
]);
$businessRule = $result->business_rule;
```

#### Python

```python
from chargebee import Chargebee

cb_client = Chargebee(api_key="{site_api_key}", site="{site}")
response = cb_client.BusinessRule.update_draft("custom-uuid-1",
    cb_client.BusinessRule.UpdateDraftParams(
        name="Apply 15 percentage discount at invoice level",
        description="Apply 15 percentage discount at invoice level",
        structured_expression={
            "type": "GROUP",
            "operation": "AND",
            "children": [
    {
        "type": "CONDITION",
        "field": "customer.language",
        "operator": "CONTAINS",
        "value": "en"
    },
    {
        "type": "CONDITION",
        "field": "quote.shipping_address_country",
        "operator": "ANY_OF",
        "values": ["IN", "US", "GB"]
    }
]
        },
        actions_on_success=[
            cb_client.BusinessRule.UpdateDraftActionsOnSuccessParams(
              input={
    "apply_on": "invoice_amount",
    "duration_type": "one_time",
    "discount": 15,
    "discount_type": "percentage"
},
              action_template_id="action-apply-discount",
              type="APPLY_DISCOUNT"
            )
        ]
    )
)
business_rule = response.business_rule
```

#### Ruby

```ruby
require 'chargebee'

ChargeBee.configure(:site => "{site}",
  :api_key => "{site_api_key}")

result = ChargeBee::BusinessRule.update_draft("custom-uuid-1",{
  :name => "Apply 15 percentage discount at invoice level",
  :description => "Apply 15 percentage discount at invoice level",
  :structured_expression => {:type => "GROUP",:operation => "AND",:children => [{:type => "CONDITION",:field => "customer.language",:operator => "CONTAINS",:value => "en"},{:type => "CONDITION",:field => "quote.shipping_address_country",:operator => "ANY_OF",:values => ["IN","US","GB"]}]},
  :actions_on_success => "[{\"input\":{\"apply_on\":\"invoice_amount\",\"duration_type\":\"one_time\",\"discount\":15.0,\"discount_type\":\"percentage\"},\"action_template_id\":\"action-apply-discount\",\"type\":\"APPLY_DISCOUNT\"}]"
})

business_rule = result.business_rule
```

## Sample Response

```json
{
  "business_rule": {
    "id": "custom-uuid-1",
    "name": "Apply 15 percentage discount at invoice level",
    "description": "Apply 15 percentage discount at invoice level",
    "latest_version": 1,
    "active": true,
    "released_at": 1788510782,
    "released_by": "full_access_key_v1",
    "updated_at": 1788597182,
    "updated_by": "full_access_key_v1",
    "created_by": "full_access_key_v1",
    "created_at": 1788510782,
    "tags": [
      "CPQ",
      {..}
    ],
    "structured_expression": {
      "type": "GROUP",
      "operation": "AND",
      "children": [
        {
          "type": "CONDITION",
          "field": "customer.language",
          "operator": "CONTAINS",
          "value": "en"
        },
        {..}
      ]
    },
    "actions_on_success": [
      {
        "input": {
          "apply_on": "invoice_amount",
          "duration_type": "one_time",
          "discount": 15,
          "discount_type": "percentage"
        },
        "action_template_id": "action-apply-discount",
        "type": "APPLY_DISCOUNT"
      },
      {..}
    ],
    "resource_version": 1788597182412,
    "object": "business_rule"
  }
}
```

## URL Format

**POST** https://[site].chargebee.com/api/v2/business_rules/{business-rule-id}/draft

## Input Parameters

- `name` (required, string, max chars=500)
  Display name of the business rule.

- `description` (optional, string, max chars=1000)
  Description of what the business rule does.

- `structured_expression` (required, jsonobject)
  A structured JSON representation of the rule logic, designed for visual editors and dynamic builders. Chargebee validates and compiles it when the draft is updated. Pass it as a JSON object.
  
  See [Expressions](/docs/api/business_rules#expressions) for the node types and the operators each field type supports, and [Context](/docs/api/business_rules#context) for the fields a condition can reference.
  
  **Impacts**
  
  -   A condition whose `field` is absent from the context passed to [Apply business rules](/docs/api/business_rules/apply-business-rules) evaluates to `false`, so the rule never matches and no error is returned.
  
  **Example →** `structured_expression = {"type":"GROUP","operation":"AND","children":[{"type":"CONDITION","field":"customer.language","operator":"CONTAINS","value":"en"},{"type":"CONDITION","field":"quote.shipping_address_country","operator":"ANY_OF","values":["IN","US","GB"]}]}`

- `actions_on_success` (optional)
  The actions to execute when the rule expression evaluates to `true`, passed as a JSON array. Each action takes the `action_template_id` of the template it is built from and that template's parameters in `input`.
  
  See [Actions](/docs/api/business_rules#actions) for the templates available, the parameters each one takes, and the optional action-level `structured_expression` that narrows the items an action applies to.
  
  **Example →** `actions_on_success = [{"action_template_id":"action-apply-discount","input":{"apply_on":"invoice_amount","duration_type":"one_time","discount":15.0,"discount_type":"percentage"}}]`

## Returns

- `business_rule` (Business rule object)
  The business rule with its draft updated. `latest_version`, `released_at`, `released_by`, and `active` are unchanged, so Chargebee keeps evaluating the released version until the draft is [released](/docs/api/business_rules/release-a-business-rule).
