CRM CheatSheet

CRM

JAVASCRIPT

SET LOOKUP FIELD

function SetLookup(executionContext, id, name, entityname, fieldname) {
  var formContext =
    executionContext.getFormContext != null
      ? executionContext.getFormContext()
      : executionContext;
  var lookupValue = [];
  lookupValue[0] = {};
  lookupValue[0].id = "00000000-0000-0000-0000-000000000000"; // GUID of the lookup id
  lookupValue[0].name = "Name of the lookup"; // Name of the lookup
  lookupValue[0].entityType = "entityname"; //Entity Type of the lookup entity
  formContext.getAttribute("fieldname").setValue(lookupValue); // You need to replace the lookup field Name..
}

XRM.WEBAPI

CREATE

var entityLogicalName = "account";
var data = {
  name: "Sample Account",
  creditonhold: false,
  address1_latitude: 47.639583,
  description: "This is the description of the sample account",
  revenue: 5000000,
  accountcategorycode: 1
};
Xrm.WebApi.createRecord(entityLogicalName, data).then(
  successCallback,
  errorCallback
);

UPDATE

var entityLogicalName = "account";
var data = {
  name: "Sample Account",
  creditonhold: false,
  address1_latitude: 47.639583,
  description: "This is the description of the sample account",
  revenue: 5000000,
  accountcategorycode: 1
};
Xrm.WebApi.updateRecord(entityLogicalName, data).then(
  successCallback,
  errorCallback
);

DELETE

var entityLogicalName = "account";
var id = "00000000-000000-000000-0000000" || Xrm.WebApi.retrieve;
Xrm.WebApi.deleteRecord(entityLogicalName, id).then(
  successCallback,
  errorCallback
);

RETRIEVE

var entityLogicalName = "account";
var id = "00000000-000000-000000-0000000";
var options = "?$select=name,revenue";
Xrm.WebApi.retrieveRecord(entityLogicalName, id, options).then(
  successCallback,
  errorCallback
);

RETRIEVE MULTIPLE

var entityLogicalName = "account";
var options = "?$select=name,revenue&$filter=name eq 'Account'";
Xrm.WebApi.retrieveMultipleRecords(
  entityLogicalName,
  options,
  maxPageSize
).then(successCallback, errorCallback);

RETRIEVE MULTIPLE WITH FETCH

var entityLogicalName = "account";
var options = "?fetchXml=" + fetchxml;
Xrm.WebApi.retrieveMultipleRecords(
  entityLogicalName,
  options,
  maxPageSize
).then(successCallback, errorCallback);

.NET

WORKFLOW ACTIVITY

SET ARGUMENTS

    [Input("NAME IN THE CRM WORKFLOW ACTIVITY")]
    [ReferenceTarget("entityname")]
    public InArgument<EntityReference> nameforc# { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
        ...
    }

GET CONTEXTS

    var tracingService = context.GetExtension<ITracingService>();
    var workflowContext = context.GetExtension<IWorkflowContext>();
    var serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
    var service = serviceFactory.CreateOrganizationService(workflowContext.UserId);

GET REFERENCE SET IN THE ARGUMENTS

    EntityReference nameforc#Ref = nameforc#.Get(context);

PLUGIN

GET CONTEXTS

ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(null);

GET TARGET

Entity entity = (Entity)context.InputParameters["Target"];

Join the newsletter