Wednesday, June 8, 2016

Error: 400: Bad Request in MS CRM

Error: 400: Bad Request: Error processing request stream. The property name 'field_Name' specified for type 

'Microsoft.Crm.Sdk.Data.Services.field_Name' is not valid.

I had forgotten that I needed to specify the Schema name of the attribute and this is Case Sensitive. Once I had the case correct everything worked 

well again.

Error processing request stream. The property name 'new_Email' specified for type 'Microsoft.Crm.Sdk.Data.Services.Incident' is not valid.
check the field name in this. Check the Letters CASE

Validate Business Process Flow using Javascript

function AddStageHandlers() {
    Xrm.Page.data.process.addOnStageChange(StageChangedHandler);
}

function StageChangedHandler() {
    var activeStage = Xrm.Page.data.process.getActiveStage();
    var stageid = Xrm.Page.getAttribute("stageid").getValue();
    var peocessid = Xrm.Page.getAttribute("processid").getValue();
    var stageName = activeStage.getName();
    if (stageName == "BPF Stage Name") {
        if (Xrm.Page.getAttribute("optionsetattributename").getText() == "Yes") {
            if (Xrm.Page.getAttribute("attributename").getValue() == null) {
                //Previous stage ID
                Xrm.Page.data.process.movePrevious(function () { Xrm.Page.data.process.setActiveStage('stageid'); alert("All fields are Mandatory"); });
            }
            else {
                //Next stage ID
                Xrm.Page.data.process.moveNext(function () { Xrm.Page.data.process.setActiveStage('stageid'); alert("All fields are Mandatory"); });
            }
        }

        else {
            //Next stage ID
            Xrm.Page.data.process.moveNext(function () { Xrm.Page.data.process.setActiveStage('stageid'); alert("All fields are Mandatory"); });
        }
    };
}

How to set Optionset Valueby Text

function setOptionSetValueByText() {
    var optionText = Xrm.Page.getAttribute("statuscode").getText();
    var options = Xrm.Page.getAttribute("new_status").getOptions();
    for (i = 0; i < options.length; i++) {
        if (options[i].text == optionText) {
            Xrm.Page.getAttribute("new_status").setValue(options[i].value);
        }
    }
}

Fetch Child records in MS CRM

function fetchChildRecord() {
    var GUIDvalue = Xrm.Page.data.entity.getId();
    if (GUIDvalue != null) {
        //alert(GUIDvalue);
        var childRec = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                            "<entity name='contact'>" +
                            " <attribute name='fullname' />" +
                            " <attribute name='parentcustomerid' />" +
                            " <attribute name='telephone1' />" +
                            " <order attribute='fullname' descending='false' />" +
                            " <filter type='and'>" +
                            " <condition attribute='parentcustomerid' operator='eq'  value='" + GUIDvalue + "' />" +
                            " </filter>" +
                            "</entity>" +
                            "</fetch> ";

        var childRecords = XrmServiceToolkit.Soap.Fetch(childRec);
        if (childRecords.length > 0) {
            if (childRecords[0].attributes.status != undefined) {
                var StatusValue = childRecords[0].attributes.fullname.value;
                alert(childRecords[0].attributes.fullname.value);
                //return StatusValue;
            }
        }
    }
}

//Have to add the XrmServiceToolkit.js file 
//links to download the file xrmservicetoolkit/xrmservicetoolkit 

Thursday, May 26, 2016

How to set the From and To Default values in Email in MS CRM

function RetrieveTOfromCASE() {
    var regardingObject = Xrm.Page.getAttribute("regardingobjectid");
    if (regardingObject.getValue() != null) {
        //SDK.REST.retrieveRecord(guid,entityname,select query,null,successcallback,errorcallback);
        SDK.REST.retrieveRecord(regardingObject.getValue()[0].id, "Incident", "Title,ContactId", null, successRetrieveEmail, errorHandler);
        
    }
}

function DefaultFROMandTO(GUID, NAME, LOGICALNAME) {
    //Set From Value
    if (Xrm.Page.ui.getFormType() == 1) {
        var lookupValue = new Array();
        lookupValue[0] = new Object();
        lookupValue[0].id = "GUID";
        lookupValue[0].name = "NAME(Contact/User/Queue)";
        lookupValue[0].entityType = "ENTITYNAME(Contact/User/Queue)";
        Xrm.Page.getAttribute('from').setValue(lookupValue);

        //Set To Value
        var lookupValue = new Array();
        lookupValue[0] = new Object();
        lookupValue[0].id = GUID;
        lookupValue[0].name = NAME;
        lookupValue[0].entityType = LOGICALNAME;
        Xrm.Page.getAttribute("to").setValue(lookupValue);
    }
}

var successRetrieveEmail = function (results) {
    DefaultTO(results.ContactId.Id, results.ContactId.Name, results.ContactId.LogicalName);
}

function errorHandler(error) {
}

//You need to add CRMSDK for this to work

Get Parent value in Child Record in MS CRM

function GetParentValue() {
//Fetch the case id.
//calling this function on Email form
    var regardingObject = Xrm.Page.getAttribute("regardingobjectid");
    if (regardingObject.getValue() != null) {
        //OData URI to get address information from parent account record
        var oDataURI = Xrm.Page.context.getClientUrl()
            + "/XRMServices/2011/OrganizationData.svc/"
            + "IncidentSet(guid'" + regardingObject.getValue()[0].id + "')"
            + "?$select=Title,ContactId";

        //Asynchronous XMLHttpRequest to retrieve account record
        var req = new XMLHttpRequest();
        req.open("GET", encodeURI(oDataURI), true);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function () {
            debugger;
            if (this.readyState == 4 /* complete */) {
                req.onreadystatechange = null; //avoids memory leaks
                if (this.status == 200) {
                    //parse the response string as a JSON object into the successCallback method.
                    //successCallback(JSON.parse(this.responseText).d);
                    //Directly show the alert and assign accordingly to other values
                    alert(JSON.parse(JSON.parse(this.responseText).d.Title));
                }
                else {
                    errorCallback(CaseID);
                }
            }
        };
        req.send();
    }
}

//Call the GetParentValue method on OnLoad. 

Tuesday, October 27, 2015

HTTP 404 in MS CRM

1.    Check if the database is running up
2.    If the database is set to SingleUser by any chance then follow the below steps
    •    Stop all the Microsoft Dynamics CRM Asynchronous Processing Service in the application server
    •    and then go to SQL Server and open the respective database--> check if DB Properties are accessible --> change from properties
3.    If the DB Properties are not accessible just use the below command

    Use DatabaseName
    GO
    Select * from master.sys.sysprocesses Where spid > 50 And dbid=DB_ID (‘DatabaseName’)


    Use TestServer_MSCRM
        kill 69 --spid from the above Query
    Use master;
    GO
    ALTER DATABASE DatabaseName
    SET MULTI_USER;