web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Get field value from related entity not working

(0) ShareShare
ReportReport
Posted on by 140

I am trying to get the values of market segment from lead from and use it to disable fields on the related entity, but my code does not work please help if you could i am new to javascript 

market segment is option set

 

function GetMarketSegment(executionContext)
{
var formContext = executionContext.getFormContext()

if(new_marketsegmentcode = 359030000,359030003,359030004)
{
disableFormFields;
}
}
function disableFormFields(executionContext) {
var formContext = executionContext.getFormContext()
executionContext.getFormContext.getAttribute.forEach(function (control, index) {
var controlType = control.getControlType();
if (controlType != 'iframe' && controlType != 'webresource' && controlType != 'subgrid')
control.setDisabled(true);
});
}

$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: Xrm.Page.context.getClientUrl()   "/api/data/v9.1/leads(new_marketsegmentcode)?$select=new_marketsegmentcode",
beforeSend: function(XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
XMLHttpRequest.setRequestHeader("Accept", "application/json");
XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
},
async: true,
success: function(data, textStatus, xhr) {
var result = data;
var new_marketsegmentcode = result["new_marketsegmentcode"];
var new_marketsegmentcode_formatted = result["zipari_marketsegmentcode@OData.Community.Display.V1.FormattedValue"];
},
error: function(xhr, textStatus, errorThrown) {
Xrm.Utility.alertDialog(textStatus   " "   errorThrown);
}
});

I have the same question (0)
  • cloflyMao Profile Picture
    25,210 on at

    Hi msboy,

    1. What the exact type of your option set field? Is it multiple select?

    If so, as your code shows, the value of new_marketsegmentcode variable should be in array format: [359030000,359030003,359030004 

    and you need some extra code to check equality of two arrays.

    Here is a sample solution from StackOverflow:

    https://stackoverflow.com/questions/22395357/how-to-compare-two-arrays-are-equal-using-javascript

    var array1 = new_marketsegmentcode;
    var array2 = [359030000,359030003,359030004];
    
    var is_same = (array1.length == array2.length) && array1.every(function(element, index) {
        return element === array2[index]; 
    });
    
    if (is_same) 
    {
      disableFormFields;
    }

    2. Is GetMarketSegment the function to trigger when the form loads?

  • msboy Profile Picture
    140 on at

    Yaa market segment is option set and I need function to trigger on load 

  • cloflyMao Profile Picture
    25,210 on at

    Hi msboy,

    I have modified your original code, please copy it to overwrite your JScript web resource.

    Prerequisite: 

    Import JQuery to the form due to you are using JQuery's ajax method to retrieve data.

    (You can copy JQ source code from any of its CDN file and paste to the new web resource.)

    3364.m1.JPG

    function GetMarketSegment(executionContext) {
    
        var formContext = executionContext.getFormContext();
    
        var recordID = formContext.data.entity.getId().replace(/[{}]/g, "");
    
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: formContext.context.getClientUrl()   "/api/data/v9.1/leads("   recordID   ")?$select=new_marketsegmentcode",
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
                XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
                XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
            },
            async: true,
            success: function (data, textStatus, xhr) {
    
                var result = data;
    
                new_marketsegmentcode = result["new_marketsegmentcode"];
    
                if (new_marketsegmentcode == [359030000,359030003,359030004]) {
                    disableFormFields(formContext);
                }
                
            },
            error: function (xhr, textStatus, errorThrown) {
                Xrm.Utility.alertDialog(textStatus   " "   errorThrown);
            }
        });
    
    }
    
    function disableFormFields(formContext) {
    
        var formContext = formContext;
    
        formContext.ui.controls.forEach(function (control, index) {
    
            var controlType = control.getControlType();
    
            if (controlType != "iframe" && controlType != "webresource" && controlType != "subgrid") {
    
                control.setDisabled(true);
    
            }
    
        });
        
    }

    Process of the code:

    1. Run GetMarketSegment.

    2. Retrieve new_marketsegmentcode field value of current lead.

    3. If the condition is met: some of items are selected, then disable all form fields.

    (disableFormFields should be called within success callback function of the ajax request.)

  • cloflyMao Profile Picture
    25,210 on at

    Hi msboy,

    Please let me know whether the solution could work for you. :)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans