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

Announcements

News and Announcements icon
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

I have the same question (0)
  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    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(); } }
  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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

  • Vile Andreas Rantala Profile Picture
    1,257 on at

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

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans