Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Set Related value for Lookup field on form laod?

(0) ShareShare
ReportReport
Posted on by

Hi All,

 In my case form i have the Department from which the case is generated.

In the user form i am entering the Department in which the user belongs.

I need to show the department of the user who logged in by default in the case form when he creates a case.

Ex: User Nidhin belongs to IT Department. And Nidhin logs in and creates a case by default the Department field should be filled as IT Department.

Please help me for this scenario..

Thanks

*This post is locked for comments

  • RE: CRM Set Related value for Lookup field on form laod?

    Thanks for all .. its done

  • Verified answer
    RE: CRM Set Related value for Lookup field on form laod?

    try this : userRequest.new_UserDepartment.name

    use name instead of Name

  • Verified answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: CRM Set Related value for Lookup field on form laod?

    Hi,

    In debugger I can see that the field name is new_UserDepartment and in the code you are using new_userdepartment. Javascript is case sensitive, you need to be careful and use exactly the names that you see in debugger.

  • RE: CRM Set Related value for Lookup field on form laod?

    function warningonload(){

    //Xrm.Page.ui.setFormNotification("Information on loading the form.","INFORMATION","1");

    var userid=Xrm.Page.context.getUserId();

    var user1=Xrm.Page.context.getUserName();

    var dep= getCurrentUserDep();

    //Xrm.Page.getAttribute("new_requesteddepartment").setValue(dep);

    Xrm.Page.ui.setFormNotification(dep,"INFORMATION","1");

    }

    function getCurrentUserDep() {

      var serverUrl;

      if (Xrm.Page.context.getClientUrl !== undefined) {

          serverUrl = Xrm.Page.context.getClientUrl();

      } else {

          serverUrl = Xrm.Page.context.getServerUrl();

      }

      var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";

      var userRequest = new XMLHttpRequest();

      userRequest.open("GET", ODataPath + "/SystemUserSet(guid'" + Xrm.Page.context.getUserId() + "')", false);

      userRequest.setRequestHeader("Accept", "application/json");

      userRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");

      userRequest.send();

      if (userRequest.status == 200) {

          var retrievedUser = JSON.parse(userRequest.responseText).d;

          var userFullname = retrievedUser.FullaName;

          var userDep = retrievedUser.new_userdepartment;

          Xrm.Page.ui.setFormNotification("reached here " + userDep ,"INFORMATION","1");

          return userDep ;

      }

      else {

          return "error";

      }

    }

    In this code you can see

    var userFullname = retrievedUser.FullaName; //this one gives the fullname correctly

    var userDep = retrievedUser.userdepartment;// this is a custome field 'userdepartment' this is the field name but result is undefined

    This is the full code and i am calling this on page load of my case main form. I need to set the department of case by default. The department will be the logged in users department. I have department entity. In user form i added one custon lookup field userdepartment. I need to assign the user department(who logged in) to the department lookup in the case main form.

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: CRM Set Related value for Lookup field on form laod?

    Can you post full code? On the screenshot there is not enough information. It's not possible that in debugger you have this value and without debugger you don't have this value. Unless you are calling this asynchronously and simply in debugger it finished request before you check and normally it does not.

  • RE: CRM Set Related value for Lookup field on form laod?

    jserr.jpg

    Here I am getting the user object through Javascript.  Check the right side of the image there the department "Networking Department" on the name part i need to fetch.

    But when i give userRequest.new_UserDepartment.Name it shows undefined. Any one can optimize how to get the value in javascript?

  • RE: CRM Set Related value for Lookup field on form laod?

    I need to get the logged in user's department and set it to Requested Department in my Request form(case main form).

  • Suggested answer
    tw0sh3ds Profile Picture
    tw0sh3ds 5,600 on at
    RE: CRM Set Related value for Lookup field on form laod?

    Hi,

    user department is a lookup, so you are getting an EntityReference type not string. So you should use userDep.Name instead of just userDep

  • RE: CRM Set Related value for Lookup field on form laod?

    @Sartaj Husain Thanks for your replies.

    When i am giving  schema name retrievedUser.new_UserDepartment;

    the resulted string is [objec object] . Any other way ?

  • Suggested answer
    Sartaj Profile Picture
    Sartaj 880 on at
    RE: CRM Set Related value for Lookup field on form laod?

    Hi Nidin,

    Is   new_userdepartment is logical name or schema name?

    It should be schema name. Also add form type check condition on top of your code as suggested in my earlier post.

    Regards,

    Sartaj

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans