locked
CRM 2013 - Get the value of a field from a different form to the one your using RRS feed

  • Question

  • Hi

    There have been similar questions asked and answered to mine but I was hoping for a more direct answer

    I basically have a 'user' field that within contains a lookup called group, each user must be assigned to a group

    What I want is, when creating a account I can retrieve the users group and create an if statement from there.

    Just not sure how to actually retrieve the data I need

    Thanks, Shaun


    S.Harrison

    Tuesday, June 10, 2014 10:35 AM

Answers

  • Hi Shaun,

    You can execute a REST Call using OData endpoint to fetch the users group (similar to how you did previously for Account Code) if you are using JavaScript. If you're going to implement your code in Plugin, you can use the RetreiveMultiple method to fetch user group.


    Admin QuikView Solution for CRM 2013

    Tuesday, June 10, 2014 10:54 AM
  • Hi Shaun,

    Here's the complete code, try this should work:

    businessUnitId = "";
    businessUnitName = "";
    
    function fetchBusinessUnit()
    {
    	var userId= Xrm.Page.context.getUserId();
    	var options ="$select=BusinessUnitId&$filter=SystemUserId eq guid'" + userId + "'";
    
    	SDK.REST.retrieveMultipleRecords("SystemUser", options, retreiveUserCallBack, function (error) { alert(error.message); }, function(){});
    	
    	//Do whatever validation you need to do with businessUnitId and businessUnitName
    }
    
    function retreiveUserCallBack(retrievedUser) 
    {
    	if (retrievedUser.length > 0 && retrievedUser[0]!=null && retrievedUser[0].BusinessUnitId!= null)
    	{
    		businessUnitName =retrievedUser[0].BusinessUnitId.Name;
    		businessUnitId = retrievedUser[0].BusinessUnitId.Id;
    	}
    }


    Admin QuikView Solution for CRM 2013

    • Marked as answer by Shaun Harrison Wednesday, June 11, 2014 10:01 AM
    Wednesday, June 11, 2014 9:51 AM
  • Hi Shaun,

    Some typos and silly mistakes. :)

    function getAccountNumber()
    {
    	var custId = Xrm.Page.getAttribute("customerid");
    	var CustomerID = "";
    	
    	if (custId !=null)
    	{
    		var custIDObject = custId.getValue();
    		
    		if (custIDObject !=null)
    		{
    			CustomerID = custIDObject[0].id;
    		}
    	}
    
    	var option = "$select=new_AccountCode&$filter=AccountId eq guid'" + CustomerID + "'";
    	SDK.REST.retrieveMultipleRecords("Account", option, retrieveAccountsCallBack, function(error) { alert(error.message); }, function(){});
    }
    
    function retrieveAccountsCallBack(customerIDObject)
    {
    	if ((customerIDObject!= null))
    	{
    		var _customerID = customerIDObject[0].new_AccountCode;
    		alert(_customerID);
    	}
    	else
    	{
    		alert("Error");
    	}
    }


    Admin QuikView Solution for CRM 2013

    Thursday, June 12, 2014 2:22 PM
  • Hi Shaun,

    There is actually a way, using the REST OData endpoint. Similar to how you queried for Accounts using the OData, you can do the same for Addresses, the name in this case would be "CustomerAddressSet" and you'd need to select the Address Name and whatever fields you need. Once you have that information, you can create a custom HTML web resource and populate the data in a HTML dropdown. Then you can go ahead and embed that HTML web resource in any entity form you wish to.


    Admin QuikView Solution for CRM 2013

    Tuesday, June 17, 2014 3:30 PM

All replies

  • Hi Shaun,

    You can execute a REST Call using OData endpoint to fetch the users group (similar to how you did previously for Account Code) if you are using JavaScript. If you're going to implement your code in Plugin, you can use the RetreiveMultiple method to fetch user group.


    Admin QuikView Solution for CRM 2013

    Tuesday, June 10, 2014 10:54 AM
  • Hi Again,

    Im still using JavaScript, on the previous one I was checking it against a field that the user inputted on the actual form

    This time I just want to retrieve the data in a field that is on a different form

    Can I still do this and if so what would my Odata query look like?

    Thanks, Shaun


    S.Harrison

    Tuesday, June 10, 2014 12:36 PM
  • Hi Shaun,

    What is your exact query? Do you want to fetch all users in your CRM organization? That would help me assist you with the exact query.

    Also, the OData Query Designer tool can assist you form your query which you can download from here : http://crm2011odatatool.codeplex.com/


    Admin QuikView Solution for CRM 2013

    Tuesday, June 10, 2014 1:04 PM
  • Within the users entity there is a lookup field called BusinessUnitID, I want to select using the OData Query what the user who is creating the account's business unit id

    If it is a certain value then a alert box will appear

    Thanks, Shaun


    S.Harrison

    Tuesday, June 10, 2014 1:08 PM
  • Hi,

    The oData query would look like the following:

    /BusinessUnitSet?$select=CreatedBy&$filter=BusinessUnitId eq guid'your_businessunit_id'

    You will get your_businessunit_id from the lookup by Xrm.Page.getAttribute("your_field_name").getValue()[0].id


    Admin QuikView Solution for CRM 2013

    Tuesday, June 10, 2014 2:25 PM
  • Hi,

    To make sure im going right I firstly have just entered;

    var businessID = Xrm.Page.getAttribute("businessunitid").getValue()[0].id

    But im getting the error - Unable to get property 'getValue' of undefined or null reference

    Thanks, Shaun


    S.Harrison

    Tuesday, June 10, 2014 2:49 PM
  • Hi,

    Is the field name "businessunitid" and also is the field present on the form? If it's not present on the form, you'll need to add it.


    Admin QuikView Solution for CRM 2013

    Tuesday, June 10, 2014 3:09 PM
  • Sorry I can't have been clear at the start

    The field isn't on the form, I was wondering if there was a way of accessing it without it being there

    Thanks, Shaun


    S.Harrison

    Tuesday, June 10, 2014 3:13 PM
  • Well yes, you can definitely access it. You can do the same oData concept but a different query this time similar to the following (considering the entity is contact, you can change accordingly):

    ContactSet?$select=BusinessUnitId&$filter=ContactId eq guid 'your_contact_id'

    But, you can only use this query after your record is created (saved) since without being saved the data wont flow to the CRM database and the query wont fetch anything from the CRM database. I would suggest adding the field to your form and hiding it if possible if you want to do your stuff before saving the record.


    Admin QuikView Solution for CRM 2013

    Tuesday, June 10, 2014 3:25 PM
  • I have no problem having the field on the form and hidden, but would the field automatically contain the data of the user?

    To try help ill explain further;

    Each user who can create records belong to different business groups, I need to know what business group the user belongs to so that I can state in an alert box where to save their CSV file if necessary. 

    Also, if i was to add the field how would i do it, as when i looked in the form design to add a field it wasn't there. Assuming I create a new field for it again, I would have the problem that is, the data going to be there or not? 

    Thanks, Shaun


    S.Harrison

    Tuesday, June 10, 2014 3:30 PM
  • Hi Shaun,

    What you're trying to do can be achieved in a simpler way:

    a. Fetch the user id of the current user

    b. Query the Business Unit of the current user

    c. Display message accordingly

    JavaScript codes are as follows:

    a. To get current user (creating the record). This will give you user id.

    var userId = Xrm.Page.context.getUserId;

    b. To get business unit of the current user. This will return both Business Unit Id and Business Unit Name so you can use whatever you're comfortable at

    SystemUserSet?$select=BusinessUnitId,business_unit_system_users/Name&$expand=business_unit_system_users&$filter=SystemUserId eq guid'userId'


    Admin QuikView Solution for CRM 2013

    Tuesday, June 10, 2014 3:51 PM
  • Hi,

    The code you provided came back with the following error when used with the SDK.REST code used previously except the changing of set

    The error was;

    Error : 400: Bad Request: No property 'guid' exists in type Microsoft.Xrm.Sdk.Entity' at position 16

    The code I used is as follows;

    var userID = Xrm.Page.context.getUserId;
    var option ="http://crmtest/siderise/xrmservices/2011/OrganizationData.svc/SystemUserSet?$select=BusinessUnitId,business_unit_system_users/Name&$expand=business_unit_system_users&$filter=SystemUserId eq quid'userID' ";

    ( I also tried the following; $select=BusinessUnitId,business_unit_system_users/Name&$expand=business_unit_system_users&$filter=SystemUserId eq quid''" + userID+ "'"; )

      SDK.REST.retrieveMultipleRecords("SystemUser", option, retrieveAccountsCallBack, function(error) { alert(error.message); }, function(){});

    Thanks, Shaun


    S.Harrison

    Wednesday, June 11, 2014 8:21 AM
  • Hi,

    Your code should be :

    var userID = Xrm.Page.context.getUserId;
    var option ="$select=BusinessUnitId,business_unit_system_users/Name&$expand=business_unit_system_users&$filter=SystemUserId eq guid'" + userID + "'";

    Also, your call back function needs to retrieve the necessary data, so you will need to tweak your "retrieveAccountsCallBack" method that you used for your previous functionality.


    Admin QuikView Solution for CRM 2013


    Wednesday, June 11, 2014 8:28 AM
  • Did the changes as suggested

    New error - Error : 400: Bad Request: Unrecognized 'Edm.Guid' literal 'guid'function(){return window.USER_GUID} " in '16'

    var userID = Xrm.Page.context.getUserId;
    var option ="SystemUserSet?$select=BusinessUnitId,business_unit_system_users/Name&$expand=business_unit_system_users&$filter=SystemUserId eq guid'" + userID +"'";

     SDK.REST.retrieveMultipleRecords("SystemUser", option, retrieveSystemUserCallBack, function(error) { alert(error.message); }, function(){});


    S.Harrison

    Wednesday, June 11, 2014 8:39 AM
  • Hi Shaun,

    The code will be

    var option ="$select=BusinessUnitId,business_unit_system_users/Name&$expand=business_unit_system_users&$filter=SystemUserId eq guid'" + userID + "'";

    instead of

    var option ="SystemUserSet?$select=BusinessUnitId,business_unit_system_users/Name&$expand=business_unit_system_users&$filter=SystemUserId eq guid'" + userID +"'";

    No "SystemUserSet" is required in the option.


    Admin QuikView Solution for CRM 2013

    Wednesday, June 11, 2014 9:04 AM
  • Hi,

    Done suggested changes, still same error

    Could it be the follow up function that's causing the problem, so far its as follows

     function retrieveSystemUserCallBack(retrievedUser)
      {
                if(retrievedUser!= null)
                {
                    alert("Hello");
                }
    }

    Thanks, Shaun


    S.Harrison

    Wednesday, June 11, 2014 9:20 AM
  • Hi Shaun,

    Here's the complete code, try this should work:

    businessUnitId = "";
    businessUnitName = "";
    
    function fetchBusinessUnit()
    {
    	var userId= Xrm.Page.context.getUserId();
    	var options ="$select=BusinessUnitId&$filter=SystemUserId eq guid'" + userId + "'";
    
    	SDK.REST.retrieveMultipleRecords("SystemUser", options, retreiveUserCallBack, function (error) { alert(error.message); }, function(){});
    	
    	//Do whatever validation you need to do with businessUnitId and businessUnitName
    }
    
    function retreiveUserCallBack(retrievedUser) 
    {
    	if (retrievedUser.length > 0 && retrievedUser[0]!=null && retrievedUser[0].BusinessUnitId!= null)
    	{
    		businessUnitName =retrievedUser[0].BusinessUnitId.Name;
    		businessUnitId = retrievedUser[0].BusinessUnitId.Id;
    	}
    }


    Admin QuikView Solution for CRM 2013

    • Marked as answer by Shaun Harrison Wednesday, June 11, 2014 10:01 AM
    Wednesday, June 11, 2014 9:51 AM
  • Worked perfectly, thanks for you help

    Thanks, Shaun


    S.Harrison

    Wednesday, June 11, 2014 10:01 AM
  • Hi again,

    This time im trying to access certain data that is stored in the entity 'Account'

    When a order is being placed the user has to input the 'Account Name' when they do this im trying to run the similar odata query you provided to access data such as Account Code

    My code so far is as follows;

    var customerID= Xrm.Page.getAttribute("customerid");
    if (customerID !=null)
    {
    var customerIDObject = customerID.getValue();
    if((customerIDObject !=null))
    {
    var CustomerID = customerIDObject[0].name;
    alert(CustomerID);
    }
    }


    var option = "$select=new_AccountCode&$filter=AccountSetname eq'" + CustomerID+ "'";

    SDK.REST.retrieveMultipleRecords("Account", option, retrieveAccountsCallBack, function(error) { alert(error.message); }, function(){});

    if(accountCode.length > 0)
    {
    alert(accountCode);
    }

    }

    function retrieveAccountsCallBack(retrievedAccount)

    {
       if (retrievedAccount.length > 0)
    {
    accountCode = retrievedAccount[0].new_AccountCode.Name;
    }
    else
    {
    alert("Error");
    }

    Im getting the error 'AccountSetname' doesn't exist in type 'Microsoft.xrm.sdk.entity'

    Thanks, Shaun


    S.Harrison

    Thursday, June 12, 2014 10:30 AM
  • Hi Shaun,

    Your oData query is incorrect. It should be:

    var option = "$select=new_AccountCode&$filter=AccountId eq guid'" + CustomerID+ "'";

    There is no field in Account OOB named "AccountSetName" which is why you're getting the error.


    Admin QuikView Solution for CRM 2013

    Thursday, June 12, 2014 10:42 AM
  • Hi, thanks that's seems to have done it

    Can you however spot anything else that's wrong?

    The accountCode is being returned as undefined

    Thanks, Shaun


    S.Harrison

    Thursday, June 12, 2014 10:53 AM
  • Hi,

    Just spotted another probable error:

    if((customerIDObject !=null))
     {
     var CustomerID = customerIDObject[0].name;
     alert(CustomerID);
     }

    should be

    if((customerIDObject !=null))
     {
     var CustomerID = customerIDObject[0].id;
     alert(CustomerID);
     }

    Since you're querying with the Account Id, you need to select the Id from the lookup field, not the name. Even then if the value is null, you might want to check the field name is correct and that the actual record has the field value.


    Admin QuikView Solution for CRM 2013



    Thursday, June 12, 2014 10:58 AM
  • Hi,

    Still getting undefined back, code is as follows;

    var customerID= Xrm.Page.getAttribute("customerid");
    if (customerID !=null)
    {
    var customerIDObject = customerID.getValue();
    if((customerIDObject !=null))
    {
    var CustomerID = customerIDObject[0].id;
    }
    }


    var option = "$select=new_AccountCode&$filter=AccountId eq guid'" + CustomerID+ "'";

    SDK.REST.retrieveMultipleRecords("Account", option, retrieveAccountsCallBack, function(error) { alert(error.message); }, function(){});

    }

    function retrieveAccountsCallBack(customerIDObject)

    {
       if ((customerIDObject!= null))
    {
       var CustomerID = customerIDObject[0].id;
    alert(CustomerID);
    }
    else
    {
    alert("Error");
    }

    }


    S.Harrison

    Thursday, June 12, 2014 12:07 PM
  • Hi Shaun,

    Are you sure you're selecting an Account in your Order form and not a Contact? Also, have you checked an Account actually exists with that ID?


    Admin QuikView Solution for CRM 2013

    Thursday, June 12, 2014 12:31 PM
  • Hi, In the order form I'm using a look up field that does point to accounts

    In the look up I am typing the name of a account name, which for example is Morrow. I'm assuming that ID is unique to each account name?

    Therefore using the ID should allow me to retireve any information I want from the account Morrow?

    Thanks, Shaun


    S.Harrison

    Thursday, June 12, 2014 12:40 PM
  • Yes, the ID (which is a GUID) is unique for every account. I think the issue is in the line

    var CustomerID = customerIDObject[0].id;

    Declare the CustomerID outside the if-else; else the var makes it local to the if-else.

    Cleaned and restructured code for you, removing same variable names, etc.

    function getAccountNumber()
    {
    	var custId = Xrm.Page.getAttribute("customerid");
    	var CustomerID = "";
    	
    	if (custId !=null)
    	{
    		var custIDObject = custId.getValue();
    		
    		if (custIDObject !=null)
    		{
    			CustomerID = customerIDObject[0].id;
    		}
    	}
    
    	var option = "$select=new_AccountCode&$filter=AccountId eq guid'" + CustomerID + "'";
    	SDK.REST.retrieveMultipleRecords("Account", option, retrieveAccountsCallBack, function(error) { alert(error.message); }, function(){});
    }
    
    function retrieveAccountsCallBack(customerIDObject)
    {
    	if ((customerIDObject!= null))
    	{
    		var _customerID = customerIDObject[0].id;
    		alert(_customerID);
    	}
    	else
    	{
    		alert("Error");
    	}
    }
    


    Admin QuikView Solution for CRM 2013

    Thursday, June 12, 2014 1:11 PM
  • Hi,

    Copied and pasted the code in

    Error message saying customerIDObject is undefinded

    Thanks, Shaun


    S.Harrison

    Thursday, June 12, 2014 1:34 PM
  • Hi Shaun,

    Some typos and silly mistakes. :)

    function getAccountNumber()
    {
    	var custId = Xrm.Page.getAttribute("customerid");
    	var CustomerID = "";
    	
    	if (custId !=null)
    	{
    		var custIDObject = custId.getValue();
    		
    		if (custIDObject !=null)
    		{
    			CustomerID = custIDObject[0].id;
    		}
    	}
    
    	var option = "$select=new_AccountCode&$filter=AccountId eq guid'" + CustomerID + "'";
    	SDK.REST.retrieveMultipleRecords("Account", option, retrieveAccountsCallBack, function(error) { alert(error.message); }, function(){});
    }
    
    function retrieveAccountsCallBack(customerIDObject)
    {
    	if ((customerIDObject!= null))
    	{
    		var _customerID = customerIDObject[0].new_AccountCode;
    		alert(_customerID);
    	}
    	else
    	{
    		alert("Error");
    	}
    }


    Admin QuikView Solution for CRM 2013

    Thursday, June 12, 2014 2:22 PM
  • Thanks for all your help!

    Shaun


    S.Harrison

    Thursday, June 12, 2014 2:25 PM
  • Hi Dynamotion

    I have selected all of the account names from the entity Account

    Is it possible to put all the account names retrieved into a option set that is on the form?

    Thanks, Shaun


    S.Harrison

    Monday, June 16, 2014 2:52 PM
  • Hi Shaun,

    You can only set options in an OptionSet if the options (values) are in the metadata; meaning while creating the OptionSet you need to have added those data. Now since your set of data is dependent on another set of variable data (account records), it's not possible to have a CRM OptionSet populated with Account Names.

    However, you can create a webresource HTML page, and have a HTML dropdown, use REST query just like you used earlier and populate the HTML dropdown. You can then have the HTML webresource embedded on the form.

    But I would suggest a different approach. Instead of using a dropdown, why not use a lookup field. Doing that would save you tons of development time, require no code and should be a walk in the park! Either way if you're using Account Name, you'd get that in the Lookup itself. If you need to have any more fields like say Account Number, etc. you can add them to the lookup view and you should be good to go.


    Admin QuikView Solution for CRM 2013

    Tuesday, June 17, 2014 5:38 AM
  • Hi 

    Thanks for the reply

    A look-up was my first option, but was not possible as when I try putting a lookup on the form it doesn't allow me to pick a Target Record Type

    The entity I'm using is Addresses, which is out of the box entity which has a relationship with Accounts and Contacts, but have no idea why I can't put a lookup on the form

    My workaround at the moment is to have the user to input the account name into a text field and I'm getting the data from there, which isn't ideal as mistakes or the wrong account could be inputted. With regards to an HTML page (unless you have a solution to the problem described?) is there any links that could guide me in what I want to achieve?

    Thanks, Shaun


    S.Harrison

    Tuesday, June 17, 2014 7:28 AM
  • Hi Shaun,

    The following links might help you develop the HTML page and necessary tasks:

    http://crmbook.powerobjects.com/extending-crm/javascript/html-web-resources/

    http://msdn.microsoft.com/en-in/library/gg327945.aspx

    The "Address" out-of-the-box CRM entity has a lot of limitations, and I have had quite some trouble using that effectively. But if you could tell us your exact requirement what you're trying to achieve in terms of business; we might be able to recommend some alternative methods.


    Admin QuikView Solution for CRM 2013

    Tuesday, June 17, 2014 7:53 AM
  • Hi,

    Definitely the hardest Entity I've had to work with

    Thanks for your help, I've decided to bite the bullet and go with a plugin to get the information I need

    Within the entity there is a field called AddressNumber

    Do you know if that field is unique to the account or just a general address count?

    Thanks, Shaun


    S.Harrison

    Tuesday, June 17, 2014 8:14 AM
  • Hi Shaun,

    The OOB AddressNumber cannot identify a unique Address among all Address records. Whenever you create an Account and provide the address, an "Address" entity record is created in the backend with AddressNumber as 1. Additionally, if you specify another address in the Account form, a second "Address" entity record is created in the backend with AddressNumber as 2 (If you see Account customization, you'll find address fields with address2_ prefix, these are second address fields). So, it means for every Account record, you will have two default "Address" records created. However, you can add more addresses by going to "More Address" and adding more.

    So this means for every Account record, you can have "Address" record with AccountNumber 1 and 2. This link will help you understand better.


    Admin QuikView Solution for CRM 2013


    Tuesday, June 17, 2014 9:14 AM
  • Hi,

    Thanks for your help, the "More Addresses" is what I'm working on, I'm using the account number to create a unique address code, I noticed CRM automatically increase the number by 1 for each address so that saves me the hassle

    Thanks again for your help

    Shaun


    S.Harrison

    Tuesday, June 17, 2014 9:18 AM
  • Yes Shaun, that's exactly what CRM does.

    Admin QuikView Solution for CRM 2013

    Tuesday, June 17, 2014 9:19 AM
  • Hi,

    Is it possible for a lookup to get the address Names in "More Addresses"?

    Thanks, Shaun


    S.Harrison

    Tuesday, June 17, 2014 1:11 PM
  • Hi Shaun,

    If you're asking to create a lookup of "More Addresses", nope that's not possible either in the "More Address" form or in any other entity form. If you're asking to add the Address Name from "More Address" in any other entity View, that's not possible as well.


    Admin QuikView Solution for CRM 2013


    Tuesday, June 17, 2014 1:17 PM
  • Hi,

    Thanks for the quick reply

    So there is no possible way of getting records that is stored in the Entity 'Address'?

    Thanks, Shaun


    S.Harrison

    Tuesday, June 17, 2014 1:32 PM
  • Hi Shaun,

    There is actually a way, using the REST OData endpoint. Similar to how you queried for Accounts using the OData, you can do the same for Addresses, the name in this case would be "CustomerAddressSet" and you'd need to select the Address Name and whatever fields you need. Once you have that information, you can create a custom HTML web resource and populate the data in a HTML dropdown. Then you can go ahead and embed that HTML web resource in any entity form you wish to.


    Admin QuikView Solution for CRM 2013

    Tuesday, June 17, 2014 3:30 PM