Tuesday, September 8, 2020

LinPadQuery to connect to an org with username, password

 <Query Kind="Program">

  <NuGetReference>Microsoft.CrmSdk.CoreAssemblies</NuGetReference>
  <NuGetReference>Microsoft.CrmSdk.Deployment</NuGetReference>
  <NuGetReference>Microsoft.CrmSdk.Workflow</NuGetReference>
  <NuGetReference>Microsoft.CrmSdk.XrmTooling.CoreAssembly</NuGetReference>
  <NuGetReference>Microsoft.CrmSdk.XrmTooling.PackageDeployment</NuGetReference>
  <Namespace>Microsoft.Xrm.Sdk</Namespace>
  <Namespace>Microsoft.Xrm.Sdk.Messages</Namespace>
  <Namespace>Microsoft.Xrm.Sdk.Query</Namespace>
</Query>
 
string orgUrl = "https://.crm.dynamics.com/";
string authType = "Office365"; // AD, IFD, OAuth, Office365
string user = ".com";
string password = "#########";
string domain = ""; //not required
Microsoft.Xrm.Sdk.IOrganizationService _orgService;
Microsoft.Xrm.Sdk.ITracingService _tracer;
 
void Main()
{
    _orgService = CreateOrgService();
    var account = _orgService.RetrieveMultiple(new Microsoft.Xrm.Sdk.Query.FetchExpression($@"<fetch>
          <entity name='account'>
            <attribute name='name' />
            <attribute name='accountid' />
          </entity>
        </fetch>"));
 
    account[0].Id.Dump();
}
 
// Define other methods and classes here
Microsoft.Xrm.Sdk.IOrganizationService CreateOrgService()
{
    var connectionString = $"Url={orgUrl};AuthType={authType};RequireNewInstance=true";
    if (!string.IsNullOrEmpty(user))
    {
        connectionString += $";UserName={user}";
    }
    if (!string.IsNullOrEmpty(password))
    {
        connectionString += $";Password={password}";
    }
    if (!string.IsNullOrEmpty(domain))
    {
        connectionString += $";Domain={domain}";
    }
    connectionString.Dump();
    return new Microsoft.Xrm.Tooling.Connector.CrmServiceClient(connectionString);
}

Mock XmlHttpRequest /odata request's response in Jasmine Unit test, without using any other module

 WE first need to replace the XmlHttpRequest With a Custom one that we can use for responding to the calls:

Declare a variable in describe, to make it available in all the it nodes

let mockXHR 

In beforeEach add the below code. This replaces the XrmHttpRequestObject definition when triggered, keeping the original definition backup.

mockXHR = {
open: ()=>{console.log('open called from mock')},
send: ()=>{},
setRequestHeader:()=>{},
onreadystatechange:()=>{},
readyState:4,
status:200,
response:JSON.stringify(
              { cci_accountstatus:803230000 }
            ) 
          }; 
window["mockXHR"]=mockXHR;
constoldXMLHttpRequest = window.XMLHttpRequest;
window.XMLHttpRequest =function(){returnmockXHR;};
In it, Call the onReadyStateChange after calling the intended function to be tested.
exampleFunctionCallWhichMakesXHR();
 mockXHR.onreadystatechange();

In afterEach add below, so that the original definition for the XHR is restored after all the tests are executed.
afterEach(() => {     window.XMLHttpRequest = oldXMLHttpRequest;   });

 

Mock Xrm.WebApi.retrieveRecord(...).then in Jasmine Unit test without using any other node module

In the describe/beforeEach/it add below :

For Successful call:

 Xrm: {

  WebApi: {

        retrieveRecord: () => {
            //Add the result Object Properties to this object below
            var retrieveRecordResult = {
            };
            return Promise.resolve(retrieveRecordResult);
        }
        retrieveMultipleRecords: () => {
            //Add the result array Properties to this object below
            var retrieveMultipleRecordsResults = {
                entities: [{ fieldName: fieldValue, anotherFiedName: anotherFieldValue }]
            };
            return Promise.resolve(retrieveMultipleRecordsResults);
        }
    }
}
  window['Xrm']=Xrm;

For Error in the call:

Xrm: {
    WebApi: {
        retrieveRecord: () => {
            //Add the result Object Properties to this object below
            var retrieveRecordError = {
                message: 'Error in retrieve Multiple'
            };
            return Promise.reject(retrieveRecordError);
        }
        retrieveMultipleRecords: () => {
            //Add the result array Properties to this object below
            var retrieveMultipleRecordsError = {
                message: 'Error in retrieve Multiple'
            };
            return Promise.reject(retrieveMultipleRecordsError);
        }
    }
}
  window['Xrm']=Xrm;