# Mapping JSON Results

{% hint style="info" %}
**NOTE** The JSON results detailed below are generated on the Keyfax web server using Newtonsoft.JSON. This JSON is more reliable than the client side generated JSON used via the JSON dump field as detailed within [mapping-dump-fields](https://docs.keyfax.biz/product-suite/keynamics/advanced/mapping-dump-fields "mention"). It's recommended you use the approach detailed below to return JSON if you wish to use JSON to reliably process Keyfax results.
{% endhint %}

The JSON stored within the `keyfax_json` column within the `keyfax_result` entity within KeyNamics is populated via the `<ResultJson/>` element returned from the `GetResults` SOAP based web service ran on the Keyfax web server.&#x20;

To update a field on an entity form using "Receive from Keyfax" mappings with the JSON stored within the `keyfax_result.keyfax_json` column please follow the steps below.

### Prerequisites

1. Ensure your Keyfax installation is using the "KeyNamics" export templates. This will ensure all JSON properties that should be represented as arrays are actually returned as arrays even if there is only 1 item returned.&#x20;
2. Ensure Keyfax is set-up to return JSON results from the `GetResult` SOAP API end-point on the Keyfax web server. Ensure the `SOAP/GetResult/@resultJson` attribute within Keyfax configuration on the web server is set to 1.&#x20;

### Instructions

1. Ensure the KeyNamics environment has been updated to KeyNamics 3.1.0+ or is running the latest `Keyfax.CRM.Solution.dll` assembly - see "Updating existing environments" below.
2. Add a new multi-line text field to the desired entity form, this field will be populated with the value from `keyfax_result.keyfax_json`. This field should have a maximum length of `1,048,576` as shown below\...

<figure><img src="https://2882349412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MARm6St_qFGM52R3pBa%2Fuploads%2FJD3Y31cM021wY7YiPqsi%2F212869343-0888c66b-acd5-45f1-8cd8-c503c7372243.png?alt=media&#x26;token=3751a0f5-dc4a-44f6-b7ca-49340421de7c" alt=""><figcaption></figcaption></figure>

Make a note of the logical field name. We''ll need this later for the target within our final mapping. For now ensure the field is added to the entity form and the entity form is published after being updated. You can see an example below of the field we added during testing...

<figure><img src="https://2882349412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MARm6St_qFGM52R3pBa%2Fuploads%2FFSApwjSZ8JGJ4ar3982r%2F212872168-b26807f8-97c8-4b2f-b7bf-b213959bcd10.png?alt=media&#x26;token=37a381c9-f09d-4c2c-b1da-8286fa57441d" alt=""><figcaption></figcaption></figure>

3. Create a new "Receive from Keyfax" mapping with a name & source of "GUID" as shown below\...

<figure><img src="https://2882349412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MARm6St_qFGM52R3pBa%2Fuploads%2FCwo0fdRMfoY6PwQOvAfh%2F212869967-233151eb-757e-4a77-a773-97078c44808e.png?alt=media&#x26;token=05864d5e-6801-4c64-85cf-0dfdeb6edb46" alt=""><figcaption></figcaption></figure>

Ensure the `target` is `null` as we'll only use this GUID mapping to obtain the Keyfax GUID to later perform a [fetchxml](https://docs.keyfax.biz/product-suite/keynamics/configuration/fetchxml "mention") query. This won't be used to update any entity form fields.

4. Create a new [fetchxml](https://docs.keyfax.biz/product-suite/keynamics/configuration/fetchxml "mention") statement using the previously created GUID mapping as a bookmark as shown below\...

<figure><img src="https://2882349412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MARm6St_qFGM52R3pBa%2Fuploads%2FENXKl3LDxhGqwGdEG6Xw%2F212870551-dde5ca9b-95e1-4167-8a41-90c1fcf5dd3e.png?alt=media&#x26;token=881eacd0-b141-4d0c-b56d-d723f85bfb0c" alt=""><figcaption></figcaption></figure>

The [fetchxml](https://docs.keyfax.biz/product-suite/keynamics/configuration/fetchxml "mention") statement is included below\...

* **Name**: `Fetch keyfax_result.keyfax_json using Keyfax GUID`
* **Fetch XML**:

```
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
  <entity name="keyfax_session">
    <link-entity name="keyfax_result" to="keyfax_sessionid" from="keyfax_session_id" link-type="inner">
      <attribute name="keyfax_json" />
    </link-entity>
    <filter type="and">
      <condition attribute="keyfax_guid" operator="eq" value="[BOOKMARK#1]" />
    </filter>
  </entity>
</fetch>
```

* **Bookmark 1**: GUID

This [fetchxml](https://docs.keyfax.biz/product-suite/keynamics/configuration/fetchxml "mention") simply selects the `keyfax_json` column from the `keyfax_result` entity using the Keyfax GUID and the relationship that exists between sessions and results within KeyNamics.

5. Finally create a new mapping to populate the multi-line text field we added to the entity form in step 2 with the result produced via the FetchXML statement created in step 4. above

<figure><img src="https://2882349412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MARm6St_qFGM52R3pBa%2Fuploads%2FSwdxwQWPC552uIwDKQ7S%2F212871588-28c282e1-4c56-4a75-a686-daafa287e821.png?alt=media&#x26;token=f11b32f0-e43f-4d10-a3c5-cbfbecf89255" alt=""><figcaption></figcaption></figure>

The "Target" field should be the logical field name of the field we wish to update on the entity form.

6. Verify Results

Complete a Keyfax script and you should see the entity form field created in step 2 above is updated with the value from the `keyfax_result.keyfax_json` column....

<figure><img src="https://2882349412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MARm6St_qFGM52R3pBa%2Fuploads%2FEKy9aqQjtBc3coLjUsvO%2Fimage.png?alt=media&#x26;token=a213e7a1-0735-4944-92df-27c8aecdc13b" alt=""><figcaption></figcaption></figure>

### Updating existing environments

At the time of writing KeyNamics 3.1.0 has not been released. If this update needs to be applied within a D365 environment before 3.1.0 is released we would need to update the `Keyfax.CRM.Solution.dll` assembly within the target environment with a version from source control. The plug-in registration tool should be used to update this assembly as shown below\...

<figure><img src="https://user-images.githubusercontent.com/65894527/212874926-9f68ef75-465a-47be-8c79-0d3b95ca52a8.png" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
**NOTE** The above step will not be necessary once 3.1.0 has officially released and we compile a tested 3.1.0 solution.
{% endhint %}

### Why is a 3.1.0 update necessary?

To allow mappings to query the `keyfax_result` entity using FetchXML the order in which the results are created has been changed. Results are now added to the `keyfax_result` entity before "Receive from Keyfax" mappings are constructed.

### That's It

If we can assist further please don't hesitate to [support](https://docs.keyfax.biz/links/support "mention").
