• Resolved flagada

    (@flagada)


    Hello,

    I’m new to the plugin.
    I created a custom form to create new contacts.
    In this form, I want to display a dropdown list for contacts to choose their nationality.
    I get the list of nationalities via a FetchXML query that I display in the select tag.
    The nationality field in the crm is linked to an entity, emn_nationalite.
    In the contact entity, the nationality field is called “emn_nationalite_id”.
    In the entity nationality, the ID is in the field “emn_nationalite_id” and the name of the nationality corresponds to the field “emn_name”.
    The contact is created well when I submit the form but the nationality field remains empty in the CRM.
    How can I ensure that the nationality is correctly transmitted to the CRM?

    Here is my code:

    [msdyncrm_twig]
    {% form entity="contact" mode="create" required=["lastname", "emailaddress1", "description"] %}
    <form method="POST">
    <label for="firstname">First Name:</label>
    <input name="firstname" required="" placeholder="First Name">
    <label for="lastname">Last Name:</label>
    <input name="lastname" required="" placeholder="Last Name">
    <label for="mobilephone">Phone Number:</label>
    <input name="mobilephone" type="tel" placeholder="Phone Number">
    <label for="emailaddress2">email:</label>
    <input name="emailaddress2" type="tel" placeholder="email">
    <label for="nationalite">Nationalité:</label>
    <select name="emn_nationalite_id">
     {% for a in nartionality.results.entities %}
            <option value ="{{a.emn_nationaliteid}}">{{a.emn_name}}</option>
          {% endfor %}
    </select>
    
    <button type="submit">Submit</button>
    </form>
    {% endform %}
    
    [/msdyncrm_twig]
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author alexacrm

    (@alexacrm)

    Hi @flagada

    the value for a lookup needs to be a serialized entity reference. Try this code:

    <select name="emn_nationalite_id">
     {% for a in nartionality.results.entities %}
       {% set nationalite = {LogicalName: "emn_nationalite", Id: a.emn_nationaliteid, DisplayName: a.emn_name } %}
        <option value='{{nationalite | json_encode}}'>{{a.emn_name}}</option>
          {% endfor %}
    </select>

    You may want to add raw filter if values do not come across correctly when containing characters requiring escaping: value ="{{nationalite | json_encode | raw }}". See twig documentation on autoescaping and raw.

    Hope it helps
    AlexaCRM Support

    Thread Starter flagada

    (@flagada)

    Thank you for your quick answer, it works!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom form – select entity’ is closed to new replies.