You are viewing limited content. For full access, please sign in.

Question

Question

How does setting dynamic fields using the SDK work?

SDK
asked on November 20, 2019

Does anyone have an example they can provide of using either LFSO or RA to set dynamic fields on a template definition? I've looked at the documentation and the relevant posts in Answers, but some sample code would really be helpful and greatly appreciated.

0 0

Answer

SELECTED ANSWER
replied on November 22, 2019 Show version history

Sorry, I should have read your post more carefully. here is how you define a form logic rule for a field:

string boundTableName = "NameOfExternalTable";
LfExternalTableInfo boundTable = LfExternalTable.GetInfo(boundTableName, session);

string columnName = "NameOfColumnInExternalTable";

List<int> parentFields = new List<int>();
parentFields.Add(Field.GetInfo("Name Of Parent Field1", session).Id);
parentFields.Add(Field.GetInfo("Name Of Parent Field2", session).Id);

string sortColumn = "NameOfSortColumnInExternalTable";

bool validate = false; // Controls whether LFS validates values and blocks invalid values from saving

FormLogicRuleInfo ruleInfo = new FormLogicRuleInfo();
ruleInfo.TemplateId = templateId;
ruleInfo.FieldId = toSaveRule.FieldId;
ruleInfo.Session = session;
ruleInfo.BoundTable = boundTable.Id;
ruleInfo.BoundColumn = columnName;
ruleInfo.ParentFields = parentFields.ToArray();
ruleInfo.SortColumn = sortColumn;
ruleInfo.SortDirection = SortDirection.Ascending;
ruleInfo.Validate = validate;
ruleInfo.Save();

To create the external table definition:

LfExternalTableInfo extTable = new LfExternalTableInfo();
extTable.Session = session;
extTable.LaserficheName = "external table name";
extTable.Schema = "the table schema";
extTable.Database = "the name of the db";
extTable.Table = "the name of the table in the db";
extTable.Save();

 

1 0

Replies

replied on November 21, 2019

For each field in the template that has a dynamic field rule, you retrieve the FormLogicRuleInfo object and call the GetDataReader method with the current FieldValueCollection to get the list of available values:

int i;
using (LfDataReader reader = rule.IsSimple? rule.GetDataReader(fieldData) : rule.GetDataReader(fieldData, ref i))
{
    while (reader.Read())
    {
        Console.WriteLine(reader.GetString(0));
    }
}

The FormLogicRuleInfo objects come from template.GetFormLogicRules().

For example, you have a template with dynamic fields State, City, Zip where each field depends on its parent. To load the values of State (which has no dependency), call GetFieldValues() on the DocumentInfo object to and pass that in to FormLogicRuleInfo.GetDataReader. This should return a list of all states. If the user selects a state of CA, add that value to the FieldValueCollection and call FormLogicRuleInfo.GetDataReader on the FormLogicInfo associated with the City field, using the updated field collection. It will return the list of cities in CA.

 

1 0
replied on November 21, 2019

Thank you for the answer. However, I wasn't clear in what I was asking for. I'm looking for the ability to define fields in a template as dynamic using the SDK. So in your above example, if I had three fields on a template definition that were State, City, Zip, I'd like to programmatically connect them to the appropriate External Table and Column, and then set them up so that the Parent Field of City is State and the Parent Field of Zip is City. Is that possible?

0 0
SELECTED ANSWER
replied on November 22, 2019 Show version history

Sorry, I should have read your post more carefully. here is how you define a form logic rule for a field:

string boundTableName = "NameOfExternalTable";
LfExternalTableInfo boundTable = LfExternalTable.GetInfo(boundTableName, session);

string columnName = "NameOfColumnInExternalTable";

List<int> parentFields = new List<int>();
parentFields.Add(Field.GetInfo("Name Of Parent Field1", session).Id);
parentFields.Add(Field.GetInfo("Name Of Parent Field2", session).Id);

string sortColumn = "NameOfSortColumnInExternalTable";

bool validate = false; // Controls whether LFS validates values and blocks invalid values from saving

FormLogicRuleInfo ruleInfo = new FormLogicRuleInfo();
ruleInfo.TemplateId = templateId;
ruleInfo.FieldId = toSaveRule.FieldId;
ruleInfo.Session = session;
ruleInfo.BoundTable = boundTable.Id;
ruleInfo.BoundColumn = columnName;
ruleInfo.ParentFields = parentFields.ToArray();
ruleInfo.SortColumn = sortColumn;
ruleInfo.SortDirection = SortDirection.Ascending;
ruleInfo.Validate = validate;
ruleInfo.Save();

To create the external table definition:

LfExternalTableInfo extTable = new LfExternalTableInfo();
extTable.Session = session;
extTable.LaserficheName = "external table name";
extTable.Schema = "the table schema";
extTable.Database = "the name of the db";
extTable.Table = "the name of the table in the db";
extTable.Save();

 

1 0
replied on December 9, 2019 Show version history

What is the purpose of using the toSaveRule to pass in the field id? What sort of object is that? I used just a regular FieldInfo object and the Id property and it seems to work. Here's my code. Is there any risk in taking the approach I'm taking?

            TemplateInfo TempInf = Template.GetInfo("Accounts Payable", Sesh);
            FieldInfo FieldInf = Field.GetInfo("UNS Company", Sesh);

            FormLogicRuleInfo RuleInfo = new FormLogicRuleInfo();
            RuleInfo.TemplateId = TempInf.Id;
            RuleInfo.FieldId = FieldInf.Id;
            RuleInfo.Session = Sesh;
            RuleInfo.BoundTable = BoundTable.Id;
            RuleInfo.BoundColumn = ColumnName;
            //RuleInfo.ParentFields = parentFields.ToArray();
            RuleInfo.SortColumn = SortColumn;
            RuleInfo.SortDirection = SortDirection.Ascending;
            RuleInfo.Validate = validate;
            RuleInfo.Save();

 

0 0
replied on October 6, 2021

Can someone please tell me the Library used for creating External Table? The Code throws an error:

 

Error    1    The type or namespace name 'LFExternalTableInfo' could not be found (are you missing a using directive or an assembly reference?)    \SDKScript\Script 1.cs    23    13    dynamic temp
 

0 0
replied on October 6, 2021

Laserfiche.RepositoryAccess.

0 0
replied on October 6, 2021

You might just be missing that LFExternalTableInfo is in the namespace Laserfiche.RepositoryAccess.Data.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.