locked
MS CRM 2011 - Data from look up view RRS feed

  • Question

  • I have Weigh-in form.

    I want to add a new product to that form. This is done via 1:n relation and a look up. The product form gets a weigh-in number from weigh-in form. I want also that the product form gets a customer name which has been defined also on the weigh-in form. The weigh in number is just a text field, but the customer field is a look up.

    What I get is customer field's id number (or entity id, I am not sure), but not the actual text which is needed.

    Also there is a drop down menu on the weigh in form which text values I need to get on the same product form. I only get those drop down menu's actual number values, not the text values.

    Here is my modified code. With that code I only get the customer entity id or something. If I use same code to retrieve drop down menu values, I only get actual values, not the text.

    function GetInfo() {
    
        var EntityName, EntityId, LookupFieldObject, CUSTOMER;
        var PrimaryContactLookupId, PrimaryContactLookupName, PrimaryContactLookupType;
        var resultXml;
    
    
        LookupFieldObject = Xrm.Page.data.entity.attributes.get('LOOK UP FIELD NAME'); 
    
        // If lookup field has value then the code will only run
        if (LookupFieldObject.getValue() !== null) {
    
    
    
    
            //Fetch and place Entity Id (GUID) and Name (String) form lookup field into local variables
            EntityId = LookupFieldObject.getValue()[0].id;
            EntityName = LookupFieldObject.getValue()[0].entityType;
    
            // Paramter explaination for function call
            // RetrieveEntityById('account', AccountId, 'accountnumber,emailaddress1');
    
            // 1st paramter requires entity name (not display or schema name) just pass it in a string
            // i.e. for Account entity use 'account' for opportunity entity use 'opportunity'
    
            // 2nd paramter requires entity Id (GUID Type) field which we have retrieved and stored in
            // AccountId local variable
    
            // 3rd paramter requires attributes to be retrieved schema name, if thrid attributed is
            // required to be retrieved then use like: i.e. 'accountnumber,emailaddress1,telephon
    		
            resultXml = RetrieveEntityById(EntityName, EntityId, 'RELATED ENTITY'S FIELD NAME');
    
            // In retrieved XML document check if it has accountnumber attribute
            if (resultXml !== null && resultXml.selectSingleNode('//q1:RELATED ENTITY'S FIELD NAME') !== null) {
    
                // If XML document has account number attribute then assign to local variable AccountNumber
                CUSTOMER = resultXml.selectSingleNode('//q1:RELATED ENTITY'S FIELD NAME').nodeTypedValue;
    
               
    			Xrm.Page.data.entity.attributes.get('PRODUCT FORM CUSTOMER FIELD NAME').setValue(CUSTOMER);
    
                //If required then use the below code line to set value in field on form
                //Xrm.Page.data.entity.attributes.get('new_myaccountnumber').setValue(AccountNumber);
            }
    
    
    		     
            }      
    
        }
    
    
    // Do not make any changes to this function
    function RetrieveEntityById(prmEntityName, prmEntityId, prmEntityColumns) {
    
        var resultXml, errorCount, msg, xmlHttpRequest, arrayEntityColumns, xmlEntityColumns;
    
        arrayEntityColumns = prmEntityColumns.split(",");
    
        for (var i = 0; i < arrayEntityColumns.length; i++) {
            xmlEntityColumns += "<q1:Attribute>" + arrayEntityColumns[i] + "</q1:Attribute>";
        }
    
        var authenticationHeader = Xrm.Page.context.getAuthenticationHeader();
    
        //Prepare the SOAP message.
        var xml = "<?xml version='1.0' encoding='utf-8'?>" +
        "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +
        " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
        " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
        authenticationHeader +
        "<soap:Body>" +
        "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
        "<entityName>" + prmEntityName + "</entityName>" +
        "<id>" + prmEntityId + "</id>" +
        "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
        "<q1:Attributes>" +
        xmlEntityColumns +
       "</q1:Attributes>" +
        "</columnSet>" +
        "</Retrieve></soap:Body></soap:Envelope>";
    
        //call function to create Soap Request to ms crm webservice
        xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
        xmlHttpRequest.send(xml);
    
        resultXml = xmlHttpRequest.responseXML;
    
        var errorCount = resultXml.selectNodes('//error').length;
    
        if (errorCount !== 0) {
            var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
            alert("Error Message : " + msg);
        }
        else {
            return resultXml;
        }
    }
    
    

    Friday, June 28, 2013 11:42 AM