locked
How to retrieve optionset value from other entity using CRMRestKit? RRS feed

  • Question

  • Hi,

    I am using CRMRestKit solution to make crossentity operations with jscript easier for me. I am able to retrive value of string fields from other entity based on its guid (which I get from current form of incident entity), but I can't get value of an option set from other entity. My code is:

    function SetContactPreferences()
    {
    if (Xrm.Page.getAttribute('customerid').getValue() != null)
    {
    CrmRestKit.Retrieve('Account', Xrm.Page.getAttribute('customerid').getValue()[0].id, ['new_optionset1']).fail(window.globalFail).done(function (data) {
    
    alert(Xrm.Page.getAttribute('data.d.new_optionset1').getValue());
    
    });
    }
    }
    Using this one I get an error that the object is empty or undefined. When I usegetAttribute method without inverted commas:
    Xrm.Page.getAttribute(data.d.new_optionset1).getValue());

    I get an error that the Object does not provide 'getValue' property or method. Is there  a way to get the value of optionset using CRMRestKit? I couldn't find the sample of retrieving data from this type of fields. I would be grateful for your help.

    Monday, May 21, 2012 1:57 PM

Answers

  • Hello Kubas, 

    to complaining about the framework is a bit odd, because you are useing the framework incorrect :)

    Try this one: 

    var id = Xrm.Page.getAttribute('customerid').getValue()[0].id,
    	cols = ['new_optionset1'];
    
    CrmRestKit.Retrieve('Account', id, cols).then(function(data){
    
    	// the the number value of the option-set
    	var optionSetValue = data.d.new_optionset1.Value;
    	
    	// ...
    	
    }, window.globalFail);

    This line of code is strange:

    alert(Xrm.Page.getAttribute('data.d.new_optionset1').getValue());

    The lines tries some linke "get the value of the attr "data.d.new_optionset1" but data.d.new_optionset1 is the property of the result object. 

    With Xrm.Page.getAtttribute() you access the attributes of the current form!

    The CrmServiceToolkit is wondeful, but please take into account the the "old" .asmx webservice are flagged as deprecated. 

    Hope this helps

    Daniel

    • Marked as answer by Kubas_86 Wednesday, May 23, 2012 8:55 AM
    Tuesday, May 22, 2012 12:14 PM
  • Done it! Doing it via Daniel Cai Toolkit worked. The code is below:

    function getOptionSetAttr()
    {
    var accountid = Xrm.Page.getAttribute('customerid').getValue()[0].id;
    
    var columns = ["new_optionset1"];
    var retrievedattr = CrmServiceToolkit.Retrieve("account", accountid, columns);
    alert(retrievedattr.getValue('new_optionset1'))
    }

    Recommending me to use the better tool solved my issue. Thank you MubasherSharif :)

    Regards :)


    • Edited by Kubas_86 Tuesday, May 22, 2012 11:32 AM
    • Marked as answer by Kubas_86 Tuesday, May 22, 2012 11:33 AM
    Tuesday, May 22, 2012 11:32 AM

All replies

  • Proper way to get the value of optionset is

    For option set value

        var AddressType = Xrm.Page.data.entity.attributes.get("address1_addresstypecode"); 
        AddressTypeDisplayValue = AddressType.getValue(); 
        if (AddressTypeDisplayValue != null) { 
            alert(AddressTypeDisplayValue); 
        } 

    For option set Text

       var AddressType = Xrm.Page.data.entity.attributes.get("data.d.new_optionset1"); 
        AddressTypeDisplayValue = AddressType.getSelectedOption().text;
        if (AddressTypeDisplayValue != null) { 
            alert(AddressTypeDisplayValue); 
        } 




    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!


    Monday, May 21, 2012 2:30 PM
  • Thank you for quick response, MubasherSharif, but it didn't help me. I get the same errors I have described in the first post according to using quote signs with variable. getValue() method gives me the same result as getSelectedOption(). It seems that there is a difference in getting value from variable which is placed on currently open form and the retrieved from related entity one.
    • Edited by Kubas_86 Monday, May 21, 2012 2:53 PM
    Monday, May 21, 2012 2:53 PM
  • I always use Daniel Cai Toolkit so if you are just retrieving the single record i prefer you to use the toolkit. It works for both crm 2011 - 4.0
    If you need more information please let me know

    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!

    Tuesday, May 22, 2012 9:48 AM
  • Then I would be grateful if you could show a little sample code using this toolkit which would make the operation I have described in first post. So, on the incident entity form, I have got lookup field (customerid) with account records to choose. I get the id from it by using

    Xrm.Page.getAttribute('customerid').getValue()[0].id

    above line.

    Now, I would like to get the value of optionset (new_optionset1) from the account entity with the guid I have just got. Ofcourse I will try to do it now by myself, but any help of someone, who use this tool often would be appreciated and make my task easier :)

    Tuesday, May 22, 2012 11:02 AM
  • Done it! Doing it via Daniel Cai Toolkit worked. The code is below:

    function getOptionSetAttr()
    {
    var accountid = Xrm.Page.getAttribute('customerid').getValue()[0].id;
    
    var columns = ["new_optionset1"];
    var retrievedattr = CrmServiceToolkit.Retrieve("account", accountid, columns);
    alert(retrievedattr.getValue('new_optionset1'))
    }

    Recommending me to use the better tool solved my issue. Thank you MubasherSharif :)

    Regards :)


    • Edited by Kubas_86 Tuesday, May 22, 2012 11:32 AM
    • Marked as answer by Kubas_86 Tuesday, May 22, 2012 11:33 AM
    Tuesday, May 22, 2012 11:32 AM
  • I am glad that it will works for you.


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!


    Tuesday, May 22, 2012 12:06 PM
  • Hello Kubas, 

    to complaining about the framework is a bit odd, because you are useing the framework incorrect :)

    Try this one: 

    var id = Xrm.Page.getAttribute('customerid').getValue()[0].id,
    	cols = ['new_optionset1'];
    
    CrmRestKit.Retrieve('Account', id, cols).then(function(data){
    
    	// the the number value of the option-set
    	var optionSetValue = data.d.new_optionset1.Value;
    	
    	// ...
    	
    }, window.globalFail);

    This line of code is strange:

    alert(Xrm.Page.getAttribute('data.d.new_optionset1').getValue());

    The lines tries some linke "get the value of the attr "data.d.new_optionset1" but data.d.new_optionset1 is the property of the result object. 

    With Xrm.Page.getAtttribute() you access the attributes of the current form!

    The CrmServiceToolkit is wondeful, but please take into account the the "old" .asmx webservice are flagged as deprecated. 

    Hope this helps

    Daniel

    • Marked as answer by Kubas_86 Wednesday, May 23, 2012 8:55 AM
    Tuesday, May 22, 2012 12:14 PM
  • Hi toold, thank you for your interest :)

    Yes, I know that by Xrm.Page syntax I can only get attributes from current form, thats why the first post of Mubasher didn't help me as I mentioned after his response. However your suggestion sounds good :), as I didn't try to make it this way, so I am going to check it as soon, as I get access to my CRM domain. Again thanks for another piece of help :)

    Regards:)

    Jakub

    Tuesday, May 22, 2012 9:07 PM
  • @toold

    As I said, I tried to use CRMRestKit in the way you have described. It works perfect :) So the modified code from my first post should look like this:

    function SetContactPreferences()
    {
    if (Xrm.Page.getAttribute('customerid').getValue() != null)
    {
    CrmRestKit.Retrieve('Account', Xrm.Page.getAttribute('customerid').getValue()[0].id, ['new_optionset1']).fail(window.globalFail).done(function (data) {
    alert(data.d.new_optionset1.Value);
    });
    }
    }
    Which means that your post fully answers my question (solution given in CRMRestKit tool) :) Thanks a lot for help :) 

    Wednesday, May 23, 2012 8:55 AM
  • Hi Kubas, 

    glad that the solution works for you! 

    Daniel

    Thursday, May 24, 2012 5:39 AM
  • Hi,

    Whereas...

    var optionSetValue = data.d.new_optionset1.Value;

    ...will give the index of the selected item, how do I retrieve the text of the selected item? 

    data.d.new_optionset1.???

    Thanks!

    Wednesday, August 14, 2013 4:14 PM