How to modify table content and add table rows from the script rule?

Question

 

How to modify table content and add table rows from the script rule?

 

Answer

  1. In order to modify a table cell content, please create a script rule on the section (top) level:
  2. You would only need your table column to be specified in the rule settings (column alias is  AlternativesCustomerName):
  3. Here is the rule text:
    Context.Field("AlternativesCustomerName").Items[0].Text = "test";
  4. In order to add a table row, please use the event triggered After document rules are checked. Adding a new row is not available from the field rule script:
  5. Here is the script text:
    // if no rows, then add one

    if (Document.Field("Commercial Invoice\\Alternatives").Items.Count < 1) {

        Document.Field("Commercial Invoice\\Alternatives").Items.AddNew(0);

    }

Note: you may use any text field with a “flag” content instead of checking Document.Field("Commercial Invoice\\Alternatives").Items.Count < 1.

Please be careful to avoid endless cycle, because this event is triggered again and again: if it adds new rows to the table => table is changes => rules are checked again => “After document rules are checked” is triggered.

Have more questions? Submit a request

Comments

2 comments

  • Avatar

    Sameera

    using Document.Field gives me the error "The name Document does not exist in the current Context". How can I resolve this. How do I pass the IDocument Argument ?

    0
  • Avatar

    Petya07

    I want to add row/ lineItem only when I have a specific value in one of the items . This is the script that I've added in After document rules are checked”
    string docSection = "SOD ES XXXXX";

    IField lineItem = Context.Field(docSection +\\LineItems);

    int countItems = lineItem.Items.Count;

    bool isMaterialMegabox = false;

    string quantityMegabox="";

    for(int i = 0 ; i< countItems ;i++)

    {

    string CustomerMaterial = lineItem.Items[i].Field("CustomerMaterial").Text;

    if(CustomerMaterial=="XxSpecificNumber")

    {

    isMaterialMegabox=true;

    quantityMegabox = lineItem.Items[i].Field("OrderQuantity").Text;


    if(isMaterialMegabox)

    {

    lineItem.Items.AddNew(countItems);
    lineItem.Items[countItems].Field("Material").Text = "707";

    lineItem.Items[countItems].Field("OrderQuantity").Text = quantityMegabox;

    lineItem.Items[countItems].Field("CustomerMaterial").Text = "";

    lineItem.Items[countItems].Field("SalesUnit").Text = "";

    }

    How can avoid the endless cycle. In the end I have multiple blank lines added .
    The idea here is . If I have specific CustomerNumber to add another row with a specific material number and the quantity of the first one .
    I've to set a static field after looping the items I'm setting the field . What I should set stop the multiple creation ?
    Could you please help me ?

    0

Please sign in to leave a comment.