locked
MS CRM 2011 online - setting appointment's set regarding field automatically RRS feed

  • Question

  • I have a form where is a subgrid pointing to appointments. When I add appointment via the subgrid, I want to set the appointment's regarding field to populate automatically and pointing to originating entity.

    function EventOnLoad()
    {
        // is this a create form?
        if (Xrm.Page.ui.getFormType() == 1)
        {
            // The query string has the parent's id and object type code
            var queryStringObject = Xrm.Page.context.getQueryStringParameters();
    
            if (queryStringObject != null)
            {
                // let's go get the event's object type code.  
                var eventOTC = getEntityOTC('mpxrm_project'); 
    			alert("stage 1");
    
                // If the parent is an event, then we need to set the regarding object.
                // NOTE: If this appointment was created from the left navigation _CreateFromType will be null and the code won't execute (which we want)
                if (eventOTC == queryStringObject._CreateFromType)
                {
                    var eventName = "";
    				alert("stage 2");
    				var root = Xrm.Page.context.getClientUrl();
    				alert(root);
    
                      var sQuery = root + "/XRMServices/2011/organizationData.svc/mpxrm_projectSet?$select=mpxrm_name,mpxrm_project_id&$filter=mpxrm_project_id eq guid'"+ queryStringObject._CreateFromId +"'";
    
                alert("stage 3");
    				alert(sQuery);
    				var request = new XMLHttpRequest();
                    request.open("GET", sQuery, false); //sync
                    request.setRequestHeader("Accept", "application/json");
                    request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                    request.send();
    
                    var retrievedData = JSON.parse(request.responseText).d;
    				alert("stage 4");
    				alert(retrievedData);
                    if (retrievedData.results[0].mpxrm_project_id != null)
                    {
    				alert("stage 5");
                        eventName = retrievedData.results[0].mpxrm_project_id;
    
                        var olookup = new Object();
                        olookup.id = queryStringObject._CreateFromId;
                        olookup.entityType = 'mpxrm_project';
                        olookup.name = eventName;
                        var olookupValue = new Array();
                        olookupValue[0] = olookup;
    
                        Xrm.Page.getAttribute('regardingobjectid').setValue(olookupValue);
                    } 
                }
            }
        }
    }
    
    // This funtion gets the Object Type Code for any entity
    function getEntityOTC(entityName)
    {
        var lookupService = new RemoteCommand("LookupService", "RetrieveTypeCode");
        lookupService.SetParameter("entityName", entityName);
    
        var result = lookupService.Execute();
        if (result.Success && typeof result.ReturnValue == "number")
        {
            return result.ReturnValue;
        }
        else
        {
            return null;
        }
    }

    I managed to get this on alert("stage 4");. Then I managed to get this to alert(retrievedData); but with this i get answer undefined.

    After that I get error message:

    There was an error with this field's customizes event.
    Field:window
    Event:onload
    Error: unable to get property 'results' of undefined or null reference

    Any helps. This is working on another environment where only fieldnames and entity's name are different.

    Thursday, October 31, 2013 7:47 AM