Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Setting Optionset as Text via Sdk.MetaData

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Good afternoon,

I'm trying to combine SDK.REST.retrieverecord to retrieve records from a related entity, AND using SDK.MetaData to change the optionset values to text.

I'm new to Javascript and am confused about how to combine the retrieved value from Sdk.REST and the optionset text with SDK.MetaData.

See code below:

function test() {

var contact = Xrm.Page.getControl("header_process_parentcontactid").getAttribute().getValue();

SDK.REST.retrieveRecord(
contact[0].id,
"Contact",
"JobTitle,EMailAddress1,FirstName,FullName,LastName,MobilePhone,new_BusinessFunction,new_SeniorityLevel,Telephone1",
null,
function (result) {
var eMailAddress1 = result.EMailAddress1;
var firstName = result.FirstName;
var fullName = result.FullName;
var jobTitle = result.JobTitle;
var lastName = result.LastName;
var mobilePhone = result.MobilePhone;
var new_BusinessFunction = result.new_BusinessFunction;
var new_SeniorityLevel = result.new_SeniorityLevel;
var telephone1 = result.Telephone1;
Xrm.Page.getAttribute("fullname").setValue(fullName);
Xrm.Page.getAttribute("firstname").setValue(firstName);
Xrm.Page.getAttribute("emailaddress1").setValue(eMailAddress1);
Xrm.Page.getAttribute("jobtitle").setValue(jobTitle);
Xrm.Page.getAttribute("lastname").setValue(lastName);
Xrm.Page.getAttribute("mobilephone").setValue(mobilePhone);
Xrm.Page.getAttribute("telephone1").setValue(telephone1);
},
 function (error) {
 alert(error.message);
 }
 );
}

function ptest() {

var languageArray = [];

// this.RetrieveAttribute = function (EntityLogicalName, AttributeLogicalName, MetadataId, RetrieveAsIfPublished, successCallBack, errorCallBack, passThroughObject

 SDK.Metadata.RetrieveAttribute("contact", "new_BusinessFunction", null, true, function (result) {

for (var i = 0; i < result.OptionSet.Options.length; i++) {

 languageArray[result.OptionSet.Options[i].Value] = result.OptionSet.Options[i].Label.LocalizedLabels[0].Label;

 }

 },

 function (error) {

 alert(error.message);

 }

 );

}

I need to set the optionset value of new_BusinessFunction as text on the Lead Form.

Using Dynamics 365 online.

Thanks in advance.

*This post is locked for comments

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting Optionset as Text via Sdk.MetaData

    Hi all,

    Problem solved... see the final code below for anyone who's interested !

    This will retrieve information related to the contact entity... though it could manipulated for any entity retrieval !

    CODE:

    function test1() {

    var req = new XMLHttpRequest();
    var contactID = Xrm.Page.getAttribute("parentcontactid").getValue()[0].id;
    contactID = contactID.replace('{', '').replace('}', '');
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts(" + contactID + ")?$select=emailaddress1,firstname,fullname,jobtitle,lastname,mobilephone,new_businessfunction,new_senioritylevel,telephone1", 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 jobtitle = result["jobtitle"];
    var emailaddress1 = result["emailaddress1"];
    var firstname = result["firstname"];
    var fullname = result["fullname"];
    var lastname = result["lastname"];
    var mobilephone = result["mobilephone"];
    var new_businessfunction = result["new_businessfunction"];
    var new_senioritylevel = result["new_senioritylevel"];
    var telephone1 = result["telephone1"];
    Xrm.Page.getAttribute("jobtitle").setValue(jobtitle);
    Xrm.Page.getAttribute("fullname").setValue(fullname);
    Xrm.Page.getAttribute("firstname").setValue(firstname);
    Xrm.Page.getAttribute("emailaddress1").setValue(emailaddress1);
    Xrm.Page.getAttribute("lastname").setValue(lastname);
    Xrm.Page.getAttribute("mobilephone").setValue(mobilephone);
    Xrm.Page.getAttribute("telephone1").setValue(telephone1);
    Xrm.Page.getAttribute("new_senioritylevel").setValue(new_senioritylevel);
    Xrm.Page.getAttribute("new_businessfunction").setValue(new_businessfunction);
    } else {
    Xrm.Utility.alertDialog(this.statusText);
    }
    }
    };
    req.send();
    }

    Thanks everyone for the support.

  • Suggested answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: Setting Optionset as Text via Sdk.MetaData

    Hi,

    If you are using eNum concept, then it will be easy. If you create one separate file and then using values and Labels, then you can compare your getting value and then you can retrieve Label of an option set. This will be easy.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting Optionset as Text via Sdk.MetaData

    I'm having the same issue as before with webAPI - if I change the contact, it will still search on the previous contact information... e.g. if the original was person X and I then change to person Y, it will search on person X.

    Code below:

    BLANK

    Would appreciate anyone's assistance on this.

    Thanks in advance.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting Optionset as Text via Sdk.MetaData

    Hi Andreas,

    Now testing it ... it seems to be pulling it through!

    I'll give WebAPI another try then and see if I can solve it.

    Thanks for your help.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting Optionset as Text via Sdk.MetaData

    Hi Andreas,

    I'd like both... though testing on an existing record.

    Thanks,

  • Andreas Cieslik Profile Picture
    Andreas Cieslik 9,265 on at
    RE: Setting Optionset as Text via Sdk.MetaData

    Are you trying to retrieve the data while being in a create or update form of that record?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting Optionset as Text via Sdk.MetaData

    Hi Andreas,

    I had it both on onload and onchange and it wasn't instant - I also tried synchronous and asynchronous and still wasn't instant and waited until I saved before it provided the data.

  • Andreas Cieslik Profile Picture
    Andreas Cieslik 9,265 on at
    RE: Setting Optionset as Text via Sdk.MetaData

    You can register that script in the onload method of your form.

    Then you should instantly see the data.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting Optionset as Text via Sdk.MetaData

    Hi Andreas,

    Thanks for your email. I've tried to use that method before, however, it will only retrieve the records when I save the record...?

    See my previous post:

    community.dynamics.com/.../243887

    Thanks in advance.

  • Suggested answer
    Andreas Cieslik Profile Picture
    Andreas Cieslik 9,265 on at
    RE: Setting Optionset as Text via Sdk.MetaData

    I recommend you to use the new Web API and follow this guide to retrieve your optionset value:

    http://himbap.com/blog/?p=2077

    it will always contain the optionset text language of the user that is logged in.

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans