Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by 1,255

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
    Vile Andreas Rantala 1,255 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
    RaviKashyap 55,410 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
    gdas 50,085 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

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

Featured topics

Product updates

Dynamics 365 release plans