Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to retrieve OptionSet field Value from A entity to B Entity with JS?

(1) ShareShare
ReportReport
Posted on by 1,257

Hi there,

How can i retrieve the OptionSet Value/text from my A entity to my B entity, the fields are not global OptionSet, are just independent but the values and the text values are the same, i cannot use frameworks, just need to use JS to achieve that.

My scenario is  need to set the value from accounts to leads when i select the parentaccountid field on lead i try with this method, but returns me undefined or object...

function GetDataOnChange(){
var account = Xrm.Page.getAttribute("parentaccountid").getValue();

    if (account){
                    
    var AccountInfo = oData.Retriever("Account", null, "OptionSetFieldValue/TextToRetrieve", "AccountId eq (guid'" + account[0].id + "')", false, null, null, null);
                                
			if(AccountInfo != null && AccountInfo != undefined){
			//if(AccountInfo != null && AccountInfo[0].OptionSetFieldValue/TextToRetrieve.value/text != undefined){

				var MyFieldToRetrieve = new Array(); 
				MyFieldToRetrieve[0] = new Object(); 
				MyFieldToRetrieve[0].text = AccountInfo[0].OptionSetFieldValue/TextToRetrieve.text; 
				Employees[0].name = AccountInfo[0].OptionSetFieldValue/TextToRetrieve.value; 
				//Tried with this options all retrieve undefined or object object
				Xrm.Page.getAttribute(MyLeadOptionSetFieldToUpdateWithRetrievedData).setValue(MyFieldToRetrieve);
				Xrm.Page.getAttribute(MyLeadOptionSetFieldToUpdateWithRetrievedData).setValue(MyFieldToRetrieve.text);
				Xrm.Page.getAttribute(MyLeadOptionSetFieldToUpdateWithRetrievedData).setValue(MyFieldToRetrieve.value);

			}
		}
	}
}


Thanks in advance for any help/clue!

Best wishes.

*This post is locked for comments

  • Vile Andreas Rantala Profile Picture
    1,257 on at
    RE: How to retrieve OptionSet field Value from A entity to B Entity with JS?

    So many thanks for your answers, i test them both codes and both works like a charm!

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: How to retrieve OptionSet field Value from A entity to B Entity with JS?

    Hi,

    You can use the below code. Do remmebre to change the the chema name of you optionset field I have used "new_industry" which is there in both lead and account entity.

    ===============

    function OnChangeParentAccount() {

       var accountRef = Xrm.Page.getAttribute("parentaccountid").getValue();

       if (accountRef != null && accountRef[0] != null) {

           var accountId = accountRef[0].id.replace("{", "").replace("}", "");

           var req = new XMLHttpRequest();

           req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(" + accountId + ")?$select=new_industry", true);

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

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

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

           req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

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

           req.onreadystatechange = function () {

               if (this.readyState === 4) {

                   req.onreadystatechange = null;

                   if (this.status === 200) {

                       var result = JSON.parse(this.response);

                       var accountIndustryValue = result["new_industry"];

                       if (accountIndustryValue != null) {                        

                           Xrm.Page.getAttribute("new_industry").setValue(accountIndustryValue);

                       }

                   } else {

                       Xrm.Utility.alertDialog(this.statusText);

                   }

               }

           };

           req.send();

       }

    }

    ==============

    Hope this helps

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: How to retrieve OptionSet field Value from A entity to B Entity with JS?

    Hi,

    Here is sample code -

            function GetDataOnChange()
            {
                var account = Xrm.Page.getAttribute("parentaccountid").getValue()[0].id;
    if(account !=null) { var accounid =account.replace("{","").replace("}",""); var req = new XMLHttpRequest(); req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts("+accounid+")?$select=customertypecode", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function() { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var result = JSON.parse(this.response); var customertypecode = result["customertypecode"]; // will return value example - 8 var customertypecode_formatted = result["customertypecode@OData.Community.Display.V1.FormattedValue"]; //will return text example - "Prospect", // Set the lead optionset value here Xrm.Page.getAttribute("OptionSetFieldNameForLead").setValue(customertypecode); } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(); } }

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans