locked
Get a value from an entity RRS feed

  • Question

  • Hello
    I'm very new with crm 2011. I would like to insert a value in an entity named 'opportunite' from another entity named 'SCPI'. Actually I would like to fill the field "new_tauxexp" of the first entity with the value in the field "resto_exp" from the second entity.
    I tried to look for an answer in this forum but - maybe due to my difficulties to understand english- I didn't find something clear !
    Thank you in advance

    Monday, June 24, 2013 3:27 PM

All replies

  • Have you created relationship between opportunity and your SCPI entity ?? if yes you can fetch data from your related entity based on GUID otherwise you need to use some other criteria to fetch data from your entity ?? you can use rest endpoint, it's very simple to implement, you can check latest CRM sdk for sample code.

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, June 24, 2013 3:37 PM
    Moderator
  • Thank you very much for your reply.
    Could you give me a little example of a basic application using the rest endpoint ?
    Monday, June 24, 2013 4:11 PM
  • Like Mahender mentioned you can download the SDK and check out the JavaScript/REST examples in the sample code - the SDK provides some libraries for working with the REST endpoint.

    Otherwise without a library you can make a XMLHTTPRequest directly:

    function Contact_OnChange() {
        var contact = new Array();
        contact = Xrm.Page.getAttribute("new_contact").getValue();
    
        if (contact == null) {
            return;
        }
    
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/ContactSet?$select=MobilePhone&$filter=ContactId eq guid'" + contact[0].id + "'";
    
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", oDataSelect, false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        retrieveReq.onreadystatechange = function () {
            GetContactData(this);
        };
        retrieveReq.send();
    }
    
    function GetContactData(retrieveReq) {
        if (retrieveReq.readyState == 4) {
            if (retrieveReq.status == 200) {
                var retrieved = JSON.parse(retrieveReq.responseText).d;
                Xrm.Page.getAttribute("new_mobilephone").setValue(retrieved.results[0].MobilePhone);
            }
        }
    }
    

    This example looks up a mobile phone number for a contact and places it in a form field.


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, June 24, 2013 4:25 PM
    Moderator
  • Another option that might work for you would be to use the mapping functionality. If you go to Settings, Customizations, Customize the System, then locate your SCPI entity. From there expand the entity and find the 1:N Relationship field. Find the one between your SCPI entity and your opportunity entity and open it. Then on the left hand side you can click on "Mapping" you can map data from your SCPI entity to your opportunity by selecting which fields you want.

    If you do it this way you will have to create the Opportunity from the SCPI entity. But it will map the data over for you and it is much easier than coding.

    -Hope this helps you.

    Tuesday, June 25, 2013 2:52 PM
  • Ok thanks.
    I tried to help me with the example on http://worldofdynamics.blogspot.fr/2011/06/microsoft-dynamics-crm-2011-retrieve.html

    I just changed the schema names. I would like to get the value from the field "new_retro". My code is :

    function getRateSCPI() {
    
        var EntityName, EntityId, RateSCPI, LookupFieldObject;
        var PrimaryContactLookupId, PrimaryContactLookupName, PrimaryContactLookupType;
        var resultXml;
    
        LookupFieldObject = Xrm.Page.data.entity.attributes.get('new_scpi'); 
    																  				 
        if (LookupFieldObject.getValue() != null) {
    
            EntityId = LookupFieldObject.getValue()[0].id;
            EntityName = LookupFieldObject.getValue()[0].entityType;
    
    
            resultXml = RetrieveEntityById(EntityName, EntityId, 'new_retro');
    
            
            if (resultXml != null && resultXml.selectSingleNode('//q1:new_retro') != null) { 
    
                RateSCPI = resultXml.selectSingleNode('//q1:new_retro').nodeTypedValue; 
    
                alert("The rate for this SCPI is :" + RateSCPI);
    
            }
    	}	
    }	
    	
    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();
    
        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>";
    
        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;
        }
    }
    I would appreciate very much if you are able to help me !!!



    Thursday, June 27, 2013 3:30 PM
  • I find there is an error in this part :

        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);

    And the link below is not valable anymore..

    http://schemas.microsoft.com/crm/2007/WebServices/Retrieve
    Thursday, June 27, 2013 4:12 PM
  • You should not use 2007 end points instead use 2011 end points, try to use rest samples or the code that
    JLattimer has provided above.


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Thursday, June 27, 2013 4:36 PM
    Moderator
  • Ok I'll use JLattimer code.

    But what have I to change exactly ? Name of the fields and ?

    Friday, June 28, 2013 9:27 AM
  • You need to change your entity fields (make sure to use schema name) and entity (Change ContactSet to your custom entity schema name)

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, June 28, 2013 9:35 AM
    Moderator
  • Now I have a pb when I load my form. I get an error message which comes from the json2 function. I've just created a web ressource named new_json2 and pasted the sample code 'json' from the sdk. Was it the right way to use the oData restpoint ?

    Monday, July 1, 2013 1:43 PM
  • Yes that is correct, you need to add that webresource in your entity.

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, July 2, 2013 5:35 AM
    Moderator
  • Thank you for your suggestion
    I still have the same error.. I put a screenshot just below (in french but let me know if you want precisions)

    Thanks for helping me..!

    Tuesday, July 2, 2013 8:54 AM
  • And here the web resources of my form

    Tuesday, July 2, 2013 8:56 AM
  • Up please, I can't find the solution.. What function name should I put to call the json2 web resource ?

    Monday, July 8, 2013 3:12 PM