locked
Dynamcis Auto-populate multiple fields from a single lookup field RRS feed

  • Question

  • Hello,

    I am trying to create the Jquery code for pulling multiple fields from an Account entity into a Case entity based on a single lookup value, but I'm having some difficulties. I have been researching like mad and have found many wonderful examples, but they are all for single lookup field to a single auto-populate field, but I'm not sure how to pull in the additional data.

    From the Case entity I will be using a lookup field for Account entity Company Name. Based on that field in the Account I will pull in 10 fields into the Case entity. Do I need to write a function to each of these or can a single lookup field pull all of the information for these 10 fields?

    Thank you for helping me get this answer. I understand the basics for this, but not how to make it larger.

    Cass

    Monday, October 28, 2013 11:34 PM

All replies

  • Hello,

    Please check the following post

    http://vikramxrm.blogspot.com/2012/04/auto-populate-form-fields-with-selected.html


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    Tuesday, October 29, 2013 2:21 AM
  • CassSerenity use can do this functionality by using OData you retrieve addition data and populate it

    check the sample i pasted here

    http://deepakexploring.wordpress.com/2011/10/20/odata-and-jscript-in-crm-2011/

    http://rajeevpentyala.wordpress.com/2012/02/12/retrieve-record-using-odata-and-jquery-in-crm-2011/


    ms crm

    Tuesday, October 29, 2013 9:28 AM
  • Hi

    OnChange of lookup field write following code 

    I think it will help you

    function onchange() {
    
        var value = Xrm.Page.data.entity.attributes.get("customerid").getValue();
    
        if (value != null) {
    
    
            var custText = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].name;
            var Id = Xrm.Page.data.entity.attributes.get("customerid").getValue()[0].id;
           
                 
                if (Id != null) {
                    var accounts = GetAccounts(Id);
                    if (accounts != null && accounts[0].results.length > 0) {
                        for (var count = 0; count < accounts[0].results.length; count++) {
    
                            
                            Xrm.Page.getAttribute("Fieldname1").setValue(accounts[0].results[count].Name);
                            Xrm.Page.getAttribute("Fieldname2").setValue(accounts[0].results[count].yourotherfield);
                        }
                    }
                }
            }
    
    
        }
    
    
    
    
    function GetAccounts(Id) {
        var serverUrl = Xrm.Page.context.getServerUrl();
        var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/AccountSet?$select=AccountId,Name,yourotherfield&$filter=AccountId eq guid'" + Id + "'";
        var jSonArray = new Array();
    
        jQuery.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataUri,
            async: false,
            beforeSend: function (XMLHttpRequest) {
                //Specifying this header ensures that the results will be returned as JSON.            
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                if (data && data.d != null) {
                    jSonArray.push(data.d);
                }
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert("Error :  has occured during retrieval of the contacts");
            }
        });
    
        return jSonArray;
    }
    

    Tuesday, October 29, 2013 10:24 AM
  • I have taken you examples and applied them to my multiple fields, unfortunately they are not populating after I do the lookup on Company Name.

    Currently I have added this code to the library and selected it to happen onChange for the Company Name lookup field. 

    Can you please let me know what I am doing wrong so that it won't populate? Thank you very much!!

    function GetRecord(_Filter)
    	{
        		try 
    			{
           		 	var _Result = null;
            		var _EndPoint = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";
            		var _XMLHttpRequest = new XMLHttpRequest();
    
            		_XMLHttpRequest.open("GET", _EndPoint + "/" + _Filter, false);
           			_XMLHttpRequest.setRequestHeader("Accept", "application/json");
           			_XMLHttpRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            
    			var _Result = _XMLHttpRequest.send();
            
    			if (_XMLHttpRequest.readyState == 4 && _XMLHttpRequest.status == 200) 
    				{
                			_Result = this.parent.JSON.parse(_XMLHttpRequest.responseText).d;
            			}
    
            		return _Result;
       		 	}
    
        	        catch (Err) 
    			{
            		alert(Err);
            		return;
        		        }
    	}
    
    //set Trouble Ticket fields based on the account selected
    function SetFieldsValues() 
    	{
    	   try
    	   	 {
        			var _RecordId = null;
        			var _Columns = "new_ofLicenses,new_ServiceLevel,new_ServiceExperation,new_CompanyTimeZone,new_SupportService,new_ProductVersion,new_TerminalServiceEnvironment,new_InImplementation";
        			var _Entity = "Account";
    
        			if (Xrm.Page.getAttribute("new_companyname").getValue() != null) 
    				{
           					 _RecordId = Xrm.Page.getAttribute("new_companyname").getValue()[0].id;
           					 var _Filter = "/" + _Entity + "Set?$select=" + _Columns + "&$filter=" + _Entity + "Id eq (guid'" + _RecordId + "')";
            				 var _Result = GetRecord(_Filter);
           
    					 if (_Result != null && _Result.results.length == 1) 
    						{
               					 //set Number of Licenses
               					 if (_Result.results[0].new_ofLicenses != null) 
    							{
                    						Xrm.Page.getAttribute('new_ofLicenses').setValue(_Result.results[0].new_ofLicenses);
               						 }
               					 //set Service Level
               					 if (_Result.results[0].new_ServiceLevel != null) 
    							{
                   							Xrm.Page.getAttribute('new_ServiceLevel').setValue(_Result.results[0].new_ServiceLevel);
              	 					}
               					 //set Service Experation
                					 if (_Result.results[0].new_ServiceExperation != null) 
    							{
                    						Xrm.Page.getAttribute('new_ServiceExperation').setValue(_Result.results[0].new_ServiceExperation);
           	   	 					}
              	 				 //set Company Time Zone
                					 if (_Result.results[0].new_CompanyTimeZone != null) 
    							{
                    						Xrm.Page.getAttribute('new_CompanyTimeZone').setValue(_Result.results[0].new_CompanyTimeZone);
               						}
         	  	    				 //set Support Service
            	    				 if (_Result.results[0].new_SupportService != null) 
    							{
                    						Xrm.Page.getAttribute('new_SupportService').setValue(_Result.results[0].new_SupportService);
               						}
         		      				 //set Product Version
         		       				 if (_Result.results[0].new_ProductVersion != null) 
    							{
                    						Xrm.Page.getAttribute('new_ProductVersion').setValue(_Result.results[0].new_ProductVersion);
               						}
          		     				 //set Terminal Service Environment
                					 if (_Result.results[0].new_TerminalServiceEnvironment != null) 
    							{
                    						Xrm.Page.getAttribute('new_TerminalServiceEnvironment').setValue(_Result.results[0].new_TerminalServiceEnvironment);
               						}
               					 //set In Implementation
                					 if (_Result.results[0].new_InImplementation != null) 
    							{
                    						Xrm.Page.getAttribute('new_InImplementation').setValue(_Result.results[0].new_InImplementation);
               						}
           						}
    				}
    	  	    }
    	       	
    	      catch (Err) 
    	       	    {
            			alert(Err);
            			return;
        		    }
    
    	}

    Thursday, November 7, 2013 12:48 AM
  • any error message you are getting ? 

    have you tried to debug your code ?

    what event handler have you attached SetFieldsValues function ?


    Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !
    Vikram !

    Thursday, November 7, 2013 2:03 AM