locked
Auto Populating Option Sets and Lookup Field Based On A Lookup Record. RRS feed

  • Question

  • See This  For some background information. I am trying to do the same except im trying to auto populate a lookup field and optionset fields based on the details in a lookuprecord selected.

    So i am trying to bring accross a lookfield field and optionset field, from a lookup field when it is selected.



    function getVehicleDetails()
    {
        var lookUpObjectValue = Xrm.Page.getAttribute("c2_vehicle").getValue();
        if ((lookUpObjectValue != null))
        {
            var lookuptextvalue = lookUpObjectValue[0].name;
     
            var lookupid = lookUpObjectValue[0].id;
            //alert(lookupid);
    
     
        var serverUrl = Xrm.Page.context.getServerUrl();
     
        //The XRM OData end-point
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
     
     
        var odataSetName = "c2_vehicleSet";
     
        var odataSelect = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + lookupid + "')";
     
        //alert(odataSelect);
     
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: odataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {
     
                var result_contact= data.d;
    		   
                //alert(result_contact.AccountNumber);
                            //replace the fields with the fields on your entity
    						Xrm.Page.getAttribute("c2_vehiclemake").setValue(result_contact.c2_vehiclemake);
    						Xrm.Page.getAttribute("c2_enginetype").setValue(result_contact.c2_enginetype);
    
                          
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
        });
     
        }
     
       }

    ...

    c2_vehiclemake is a lookupfield

    and c2_enginetype is an optionset.

    But it is not working as it is different to a normal text field.

    I have tried this however, it doesent work.

                   var make = result_contact.c2_VehicleMake;
                
                if(make != null) {
                var finalMakeID = make[0].id;
                var finalMakeName = make[0].name;
                var finalMakeType = make[0].entityType;

                   var value = new Array();
                    value[0] = new Object();
                    value[0].id = finalMakeID;
                    value[0].name = finalMakeName;
                    value[0].entityType = finalMakeType;

            }
                            Xrm.Page.getAttribute("c2_vehiclemake").setValue(value);




    • Edited by KKHAN1991 Wednesday, July 31, 2013 2:06 PM
    Wednesday, July 31, 2013 1:26 PM

All replies

  • First you need to be sure that c2_vehiclemake and c2_enginetype are the schema names and not the logical name.

    After you can get the lookup and set in this way:

    var make = result_contact.c2_vehiclemake;
    var makeLookup = new Array();
    makeLookup[0] = new Object();
    makeLookup[0].id = make.Id;
    makeLookup[0].name = make.Name;
    makeLookup[0].entityType = make.LogicalName;
    Xrm.Page.getAttribute("c2_vehiclemake").setValue(makeLookup);

    for the optionset you need to use this code:

    var engineValue = result_contact.c2_enginetype.Value;
    Xrm.Page.getAttribute("c2_enginetype").setValue(engineValue);
    


    My blog: www.crmanswers.net

    Wednesday, July 31, 2013 1:53 PM