locked
CRM 2011: Change the view of a lookup based on the value of another field RRS feed

  • Question

  • Hi there,

    I need to adapt the sales territory entity so it can effectively have different sales territories for different product types. I basically want to add a checkbox for each account type in the sales territory entity. So when adding a sales territory we can say that the territory applies for Account Type A, Account Type B, Account Type C and so on.

    Then I want to add an option set in accounts for the user to select the Account Type. The idea is that based on the selected value of the Account Type Option Set, the view for the Sales Territory lookup should change so it shows the ones for the given Account Type.

    Question: Can I set the view of a lookup based on the value of another field (in this case, an option set)?

    Thanks for the help.

    Cheers,

    P,.


    MCC, MCT, MCP, MCTS
    If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".

    Tuesday, June 5, 2012 1:01 PM

Answers

All replies

  • Create your logic to determine which view should be loaded and use this code to set the view:

    Xrm.Page.ui.controls.get("lookupfield").setDefaultView("Guid_of_View");

    http://msdn.microsoft.com/en-us/library/gg334266.aspx


    Jason Lattimer

    • Proposed as answer by JLattimerMVP, Moderator Tuesday, June 5, 2012 1:18 PM
    • Unproposed as answer by pmdci Tuesday, June 5, 2012 2:20 PM
    • Marked as answer by pmdci Tuesday, June 5, 2012 2:48 PM
    Tuesday, June 5, 2012 1:18 PM
    Moderator
  • Hi Jason!

    Thanks for the quick reply! I will try this right now :)

    Regards,

    P.

    • Edited by pmdci Tuesday, June 5, 2012 2:24 PM
    Tuesday, June 5, 2012 1:31 PM
  • Hi Jason,

    I tried the following code added to the onLoad event of my form, but I get an error :(

    function setTerritory() {
    var CRM_FORM_TYPE = Xrm.Page.ui.getFormType();
    if (CRM_FORM_TYPE == 4 || CRM_FORM_TYPE == 3) { return; }
    
    Xrm.Page.ui.controls.get("territoryid").setDefaultView("7bE33A915E-12AF-E111-B6A0-00155D18B07F");
    
    }

    When I try click on the magnifying glass icon of the lookup field, I get the following error:

    Note: I tried the GUID of the view with and without the brackets "{}".

    Any ideas?

    Regards,

    P.


    • Edited by pmdci Tuesday, June 5, 2012 2:25 PM
    Tuesday, June 5, 2012 2:09 PM
  • Your GUID is invalid - I think it should be "E33A915E-12AF-E111-B6A0-00155D18B07F" - 32 characters (not including the dashes)

    Jason Lattimer

    Tuesday, June 5, 2012 2:33 PM
    Moderator
  • You're quite right, thanks!

    There must be an easier way to get the GUID of a view?

    Regards,

    P.


    MCC, MCT, MCP, MCTS
    If you find this post helpful then please "Vote as Helpful". If I helped you with an answer to a question then please "Mark As Answer".

    Tuesday, June 5, 2012 2:49 PM
  • You might be able to query for the ID based on the view name go that would mean adding additional code to able this happen, and you still would be "hard coding" something into the JavaScript as I'm not sure how you would determine the view name dynamically (unless perhaps your condition field name somehow corresponded to the view name). 

    You might also consider creating a central web resource JavaScript file that holds "global" values in case you want to reference these GUID in other places. That way if something changed, you would only have to touch one file.


    Jason Lattimer

    Tuesday, June 5, 2012 3:05 PM
    Moderator