Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Select competitor Populate field & another forms field

Posted on by 340

Hello,

so I went to workplace and select opportunities and select one of the case from there (now I am in the case). However we want to close this case because we unfortunately we didn’t get it (close as lost).

AA4905F5_2D00_FF93_2D00_4949_2D00_A28E_2D00_1CC8E47CA488.png

A pop up appears and tells us we can select the reason for close as well as choose the competitor.

CC7B25A3_2D00_193D_2D00_41B6_2D00_BEDA_2D00_B1B2EED70A2E.png

When I select the competitor a drop down appears with many selection. Once I choose the competitor out of the list I just wanted what I choose to appear in another field on the form, competitor field to show up the selection in the field. The competitor field is already there. 

Then when I select the contact person of who we do business with in this form - 

the problem here is I don’t have a competitor field in this form. Nor do I have the option to select competitor field because it didn’t exist. Can I create a competitor field for this form. After creating this field, I wanted when we select the competitor to populate this field of the selection as well. 

It will be in the contact form of the particular contact.

what do you advise on how I do this ?

  • Johnseito Profile Picture
    Johnseito 340 on at
    RE: Select competitor Populate field & another forms field

    Yes thanks !

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Select competitor Populate field & another forms field

    Hi John,

    It's glad that your problem has been solved.

    As per my understanding for your second part, would you like to also fill the contact's competitor field when an opportunity is closed?

    6811.png

    And the competitor field for contact is also single line text data type ?

    If so, it would be better to open a new thread for new topic and it would help others who have same question,

    I'll reply you the first time in new thread, thanks.

    Regards,

    Clofly

  • Johnseito Profile Picture
    Johnseito 340 on at
    RE: Select competitor Populate field & another forms field

    Great, that work. I see the value changed - competitor field changed. Amazing.

    I think the second part is more challenging. While it changed on the form that we want with the competitor valued that we selected.

    How about can it fill the competitor field on the contact form - for the related contact on the form it has the competitor value fill with.

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Select competitor Populate field & another forms field

    Hi John,

    1. From your screenshot, your Competitor field type is SIngle Line of Text. We could set field value by different methods based on their Data Type.

    pastedimage1572936903783v1.png

    2. It seems that there was no error in code.

    Please add 2 break points to around the setValue function: 

    alert("Competitor is: " results.value[0["_competitorid_value@OData.Community.Display.V1.FormattedValue");

    and 

    alert(formContext.getAttribute("new_competitor").getValue());

    as below:

            if (results.value[0]["_competitorid_value"] != null) {
    
              alert("Competitor is: "   results.value[0]["_competitorid_value@OData.Community.Display.V1.FormattedValue"]);
    
              formContext.getAttribute("new_competitor").setValue(results.value[0]["_competitorid_value@OData.Community.Display.V1.FormattedValue"]);
    
              alert("Competitor field has been changed to: "   formContext.getAttribute("new_competitor").getValue());
    
            }

    Try to re-open > close > click Refresh butto to an closed opportunity for twice to test whether alert value would change in each time modification.

    Regards,

    Clofly

  • Johnseito Profile Picture
    Johnseito 340 on at
    RE: Select competitor Populate field & another forms field

    this is the code. 
    How do you know your environment is the same as mine ? I am using it in a development environment.

    [code]
    function setCompetitor(executionContext) {

    var formContext = executionContext.getFormContext();

      var formType = formContext.ui.getFormType();

      // Check current opportunity is closed

      if (formType == 3 || formType == 4) {

        var opportunityid = formContext.data.entity.getId();

        opportunityid = opportunityid.replace('{', '').replace('}', '');

        var url = Xrm.Utility.getGlobalContext().getClientUrl();

        var req = new XMLHttpRequest();

        req.open(

          "GET",

          url + "/api/data/v9.1/opportunitycloses?$filter=_regardingobjectid_value eq " + opportunityid + " and statecode eq 1",

          false

        );

        req.setRequestHeader("OData-MaxVersion", "4.0");

        req.setRequestHeader("OData-Version", "4.0");

        req.setRequestHeader("Accept", "application/json");

        req.setRequestHeader("Prefer", 'odata.include-annotations="*"');

        req.send();

        if (req.readyState === 4) {

          if (req.status === 200) {

            var results = JSON.parse(req.response);

            if (results.value[0]["_competitorid_value"] != null) {

                                      formContext.getAttribute("new_competitor").setValue(results.value[0]["_competitorid_value@OData.Community.Display.V1.FormattedValue"]);

            }

          } else {

            var alertStrings = { confirmButtonLabel: "Close", text: JSON.parse(req.response).error, title: "Error occured" };

            var alertOptions = { height: 150, width: 250 };

            Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(

              function success(result) {

                console.log("Closed");

              },

              function(error) {

                console.log(error.message);

              }

            );

          }

        }

      }

    }

    [|code]

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Select competitor Populate field & another forms field

    Hi John,

    1. The previous code is for setting lookup field value. If we would like to set a lookup field value: record id, record name and record entity type are required.

    If we would like to set a single line text field, we just set it with text content. 

    In youir scenario, your competitor field is a single line text field, so we just need a text content, results.value[0]["_competitorid_value@OData.Community.Display.V1.FormattedValue"] is the opportunity close record regarding competitor name.

    2. Could you share me your setCompetitor code? Then I'll test yours in my envrionment to find is there any issue.

    Regards,

    Clofly

  • Johnseito Profile Picture
    Johnseito 340 on at
    RE: Select competitor Populate field & another forms field

    I replace it with this code:

    formContext.getAttribute("new_competitor").setValue(results.value[0]["_competitorid_value@OData.Community.Display.V1.FormattedValue"]);

    Unfortunately it still doesn’t work. The field is still not fill. Why do we replace with this new code though. I thought the previous one is what we wanted.

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Select competitor Populate field & another forms field

    Hi John,

    Thanks for sharing the key information.

    My code is for lookup field(so for my Competitor field type), while yours is Single Line Of Text.

    Please change the previous snippet 

    formContext.getAttribute("new_competitor").setValue(
      [{
        id: results.value[0]["_competitorid_value"],
        name: results.value[0]["_competitorid_value@OData.Community.Display.V1.FormattedValue"],
        entityType: results.value[0]["_competitorid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]
      }]
    );

    to new below:

    formContext.getAttribute("new_competitor").setValue(results.value[0]["_competitorid_value@OData.Community.Display.V1.FormattedValue"]);

    Then test whether it could works.

    (And don't forget this:

    pastedimage1572850485350v1.png

    )

    Regards,

    Clofly

  • Johnseito Profile Picture
    Johnseito 340 on at
    RE: Select competitor Populate field & another forms field

    The top portion I have the same as yours. Name as new_competitor. Your bottom have data type, record type and relationship name. I have data type, field type, and format. We have it differently. Maybe this is why the competitor field was never fill with the selected value ?

    B4F68281_2D00_340E_2D00_420B_2D00_8AB0_2D00_BBFBC55EF601.png

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Select competitor Populate field & another forms field

    Hi John,

    1. You can locate it in Settings > Customization > Customize the system > Components > Entities > Opportunity > Fields,

    the gray name field is the field's logical name.

    pastedimage1572831385918v1.png

    2. In my envrionment, "new_competitor" is my custom Competitor field logical name, you could check what's yours. 

    Regards,

    Clofly

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans