FormAttributeToValue() and FormDataToValue(). Description and examples of usage.

In the Syntax Assistant it is very sparingly written about the procedure FormDataToValue(), a little more - about FormAttributeToValue(). And there is absolutely no example of use. Let’s try to examine it.

FormAttributeToValue() is a method of managed form, compiled only &onServer, form context is necessary for it, so the compilation &OnServerWithoutContext is unavailable. On the client it does not work, because the result is an application object.

Where and when it should be used?
Method FormAttributeToValue() is necessary if it is required to call from the form module a standard object method or a method (procedure, function) from the object module.
This method will work with data received from the form, i.e. not yet written to the base.
In order to return the changed data on the form, a method ValueToFormAttribute() is used.
The second parameter of method FormAttributeToValue() is a value type. This is a optional parameter. If the processed attribute is not a composite type, the type will be automatically obtained from the form attribute. Otherwise, an exception of execution time is generated.

Examples:
1)

&AtServer
Procedure FillAccordingToLatestDocument(Customer)
    DocObject = FormAttributeToValue("Object");
    DocObject.FillAccordingToLatestDocument(Customer);
    ValueToFormAttribute(DocObject, "Object");
EndProcedure

2)

&AtServer
Procedure ClearLinesWithEmptyPriceOnServer()
    DocObject = FormAttributeToValue("Object");
    DocObject.ClearLinesWithEmptyPrice();
    ValueToFormAttribute(DocObject, "Object");
EndProcedure

3)

&AtServer
Function ObjectFilled()
    Return FormAttributeToValue("Object").FillCheck();
EndFunction

In the listed above examples a method FormAttributeToValue() looks similar as a method GetObject(). The difference is that the first receives an object filled by the form data and the second – an object with data from base.

FormDataToValue()
Unlike the managed form method FormAttributeToValue(), the procedure of global context FormDataToValue() can work without the form context, but that is why, in particular, it required to specify the type of converted data.

The reverse procedure is ValueToFormData().

Examples:
1)

&AtServer
Procedure ReadConstants()
    // Initializing a set of constants 
    SetConstantsObject = FormDataToValue(ConstantsSet, Type("ConstantsSet"));
    SetConstantsObject.Read();
    ValueToFormData(SetConstantsObject, ConstantsSet);
EndProcedure

2)

&AtServer
Procedure ChangeActive()
    SetForm = Object.RegisterRecords.RegisterManagement;
    Set = FormDataToValue(SetForm, Type("AccountingRegisterRecordSet.RegisterManagement"));
    If Set.Count()=0 Then
        Return;
    EndIf; 
    Set.SetActive(Not Set[0].Active);
    ValueToFormData(Set, SetForm);
EndProcedure

In this example, a set of accounting records is displayed on the form. When executing the procedure, a set activity changes on the form, at he same time, nothing happens in the base. Activity of the set of register records in the base will change only after execution of operation Write.

3)

&AtClient
Procedure Fill(Command)
    MainAttribute = Object;
    RunOnServerWithoutContext(MainAttribute);
    CopyFormData(MainAttribute, Object)
EndProcedure

&AtServerWithoutContext
Procedure RunOnServerWithoutContext(MainAttribute)
    ObjectValue = FormDataToValue(MainAttribute, Type("DocumentObject.ReconciliationActSettlement"));
    //...Execution of operations with the Object "ObjectValue"
    ValueToFormData(ObjectValue, MainAttribute);
EndProcedure

In this example, MainAttribute is returned to the client containing the changed data. But it should be also “cramed” into the displayed form. To do this, the procedure of global context CopyFormData() is used.

In most cases, the procedures FormDataToValue() and FormAttributeToValue() are interchangeable. In this case, FormAttributeToValue() is easier to use. But if it will be necessary to use &AtServerWithoutContext – then only FormDataToValue().
As an illustration of interchangeability, Example 2 in two more versions:

VERSION 1

&AtClient
Procedure Activity(Command)
    MainPropsMovement = Object.RegisterRecords.RegisterManagement;
    ActivityOnServerWithoutContext(MainPropsMovement);
    CopyFormData(MainPropsMovement, Object.RegisterRecords.RegisterManagement);
EndProcedure // Activity()

&AtServerWithoutContext
Procedure ActivityOnServerWithoutContext(MainPropsMovement)
    Set = FormDataToValue(MainPropsMovement, Type("AccountingRegisterRecordSet.RegisterManagement"));
    If Set.Count()=0 Then
        Return;
    EndIf; 
    Set.SetActive(Not Set[0].Active);
    ValueToFormData(Set, MainPropsMovement);
EndProcedure // ActivityOnServerWithoutContext()

VERSION 2

&AtServer
Procedure ActivityOnServer()
    SetForm = FormAttributeToValue("Object");
    Set = SetForm.RegisterRecords.RegisterManagement;

    If Set.Count()=0 Then
        Return;
    EndIf; 

    Set.SetActive(Not Set[0].Active);
    ValueToFormAttribute(SetForm, "Object");
EndProcedure // ActivityOnServer()
[Total: 0    Average: 0/5]

Leave a Reply

Your email address will not be published. Required fields are marked *