# Mapping Examples

### Sending a Defects Period to Keyfax

This example demonstrates how to create a Mapping in order to send data to Keyfax and the accompanying [import-xml](https://docs.keyfax.biz/product-suite/admin/entities/databoxes/import-xml "mention") Databox that is necessary to access that data within your scripts.\
\
Assuming there is a **Defects Period** date field on the current form which you wish to access within your scripts. You will need to create an entity level (in this case 'MainCase', or Global Mapping (for all entities) similar to that below. THe name of the form field is **cust\_defectsperiod**:

<figure><img src="https://2882349412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MARm6St_qFGM52R3pBa%2Fuploads%2FQDw543IZIWaEsDumqpz6%2Fimage.png?alt=media&#x26;token=44ca754c-d425-4b44-8d37-6cec1219ce06" alt=""><figcaption></figcaption></figure>

This Mapping will store the Defects Period in the startup data that is sent to Keyfax.\
\
In Keyfax Admin Tools, you will need to create an **Import Databox** (and any related Expressions) to reference this data:

### Using a 'Customer' DataType

The **Customer** DataType is a Lookup field that you can use to specify a customer, which can be an **Account** or **Contact**. In previous releases of Dynamics 365 Customer Engagement (on-premises), several out-of-the-box entities such as the Case, Lead, and Opportunity entities, included a special kind of lookup field that represented a customer. Using this lookup field you could choose between two entities: Account or Contact.&#x20;

With this new capability, you can add the Customer field to any system or custom entity. You can use the Customer field in more entities to track the customer's information in the same way you've used the Customer field in the Case, Lead, and Opportunity entities.

Let's look at an example as to how you might use a Customer data type within KeyNamics...

**The requirement is that you need to set up KeyNamics Mappings to send&#x20;*****either*****&#x20;the Account or Contact name and address details (from the selection in the current Form's Customer Datatype field) to Keyfax in a single CSV string.**\
\
You will need to set up the following **Mappings** and **FetchXml** queries, **six** items in all:\
\
To retrieve the name and address when the selection in the Customer field is the **Account**:

1. '**Bookmark**' Mapping to access the **accountid** on the current Form; this is used by the FetchXml query below
2. **FetchXml** query to retrieve Account details and concatenate the results into a CSV string
3. **Mapping** entry that invokes the FetchXml above and sends the data to Keyfax.

In much the same way, to retrieve the name and address when the selection in the Customer field is the **Contact**:

4. '**Bookmark**' Mapping to access the **contactid** on the current Form; this is used by the FetchXml query below
5. **FetchXml** query to retrieve Contact details and concatenate the results into a CSV string
6. **Mapping** entry that invokes the FetchXml above and sends the data to Keyfax. &#x20;

Clearly, only one 'set' of mappings will work depending on **what is currently selected in the Customer field**. As such, it's important that no **Default** value is supplied in either of the above mapping entries (points **3** and **6**). You may ask why? The reason is that if an Account record is currently selected on the Form and the Contact has a **Default** setting, this will override the Account details when sending data to Keyfax as no data wil be found. Similarly, if a Contact record is currently selected and the Account mapping has a Default setting the COntact details will be overridden by the Default. The whole premise of handling Customer fields is that one of the attempts to locate details will fail.&#x20;

If no selection has been made, nothing will be sent to Keyfax.

Looking further into the **Account** example, the first thing to do is **create a Mapping** entry that accesses the **Id** of the Account. To retrieve this from the current form, see the **Source** field below. Also note that the **Target** is set to **null** and this prevents the mapping entry from being sent to Keyfax. It's a good idea to make clear this Mapping entry is merely used as a BookMark by naming it appropriately:

<figure><img src="https://2882349412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MARm6St_qFGM52R3pBa%2Fuploads%2F1GctflKras3tcGw8cv0o%2Fimage.png?alt=media&#x26;token=fa172956-d029-4ab7-8a87-dd43ed1b84ac" alt=""><figcaption></figcaption></figure>

Next, create the **FetchXml** Statement to access fields as required. The example below retrieves name and address details. Notice that **Bookmark #1** points at the **Account Id** derived from the Mapping above:

<figure><img src="https://2882349412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MARm6St_qFGM52R3pBa%2Fuploads%2FBCp0qAtuSvk4BOrCwTmt%2Fimage.png?alt=media&#x26;token=c4995c0f-0457-4cd2-9c7b-6239c6057ee1" alt=""><figcaption></figcaption></figure>

Finally, create the **Mapping** that invokes the FetchXml above and actualy **sends the data to Keyfax**. The account (or contact) name and address ends up in the **Target**, i.e. the '**Tenant**' node, to be sent to Keyfax:&#x20;

<figure><img src="https://2882349412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MARm6St_qFGM52R3pBa%2Fuploads%2F46wKDT4BXD4k2r7S7xIY%2Fimage.png?alt=media&#x26;token=1e93327a-dcd5-4ee1-a2bd-8d6e7f84ec0b" alt=""><figcaption></figcaption></figure>

You will need to repeat the above to handle the selection of a **Contact** from the Customer field.

### Returning Values from Keyfax

In the below example we demonstrate how to use a "R**eceive from Keyfax**" mapping to access a specific value within the Keyfax results XML and populate an entity form field named `keyfax_receive_category` with this value.&#x20;

Let's say we wanted to access the `Fault_Category` returned by Keyfax 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%2FqORmDsddQC4Lx84uJJjv%2Fimage.png?alt=media&#x26;token=1bb55127-5841-4fe4-b0e0-eb08b7e618e7" alt=""><figcaption></figcaption></figure>

To access the `Fault_Category` value create a new mapping within KeyNamics using the following XPath query to select the `Fault_Category` value as highlighted above...

```
Fault[@name='Fault1']/Fault_Category
```

You can see an example of the full KeyNamics mapping below\...

<figure><img src="https://2882349412-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MARm6St_qFGM52R3pBa%2Fuploads%2FNXu5tnOZmicNRiXiwwAm%2Fimage.png?alt=media&#x26;token=6f96882d-e2aa-499f-9ca7-4e8edcb3ddb6" alt=""><figcaption></figcaption></figure>

To identify the name of entity form fields on your entity form as used within the target value above please see the "Discovering Field Names" section within [mapping-entity-form-fields](https://docs.keyfax.biz/product-suite/keynamics/configuration/mappings/mapping-entity-form-fields "mention").

{% hint style="info" %}
**NOTE** The "Default" value of 0 above will be used if the `Fault_Category` value is not returned or found within the Keyfax results XML. You can review the Keyfax results XML via the "Log" or "Results" links within the KeyNamics model driven app.&#x20;
{% endhint %}
