718 Washington Ave. N. Suite #101
Minneapolis, MN 55401
View map and all Locations

Send us a message

Microsoft Dynamics CRM requires specific criteria when it comes to importing data to the Contract entity. There are a few restrictions and workflows that are run by CRM to ensure a successful import, and in today’s blog, we’ll go over them. Let’s get started!


The table below illustrates State (statecode) and Status Reason (statuscode) values applicable to the Contract entity:

How to Populate Contract Entities with Legacy Data

Most of the codes are not permitted to be set directly. Additionally, once the record state is changed from the “Draft” status reason it becomes locked and is a read-only record.

Other important fields include “expireson” and “cancelon”. Once the Current Date becomes greater than the Expiration Date, CRM automatically flips the status to “Expired” and it becomes a read only record with “cancelon” required when the status is Canceled (4). Keeping these parameters in mind, loading legacy data is possible in the example below:

foreach (var contract in contractResults.Entities)
{Guid newContractId = Guid.Empty;
int stateCodeValue = -1;
var targetContractRef = new EntityReference(“contract”);

var customer = ((EntityReference)(contract[“customerid”]));

var billingCustomer = ((EntityReference)(contract[“billingcustomerid”]));

var contractExpiresOn = (DateTime)contract[“expireson”];

var contractStartsOn = (DateTime)contract[“activeon”];

bool deactivateCustomerFl = true;

bool deactivateBillingCustomerFl = true;

deactivateCustomerFl = ActivateCustomer(helper, customer);
deactivateBillingCustomerFl = ActivateCustomer(helper, billingCustomer);
var targetContractEntity = new Entity(targetEntityName);

var targetStatusEntity = new Entity(targetEntityName);

// Populate targetContractEntity with date from a source record

CopyFields(ref targetContractEntity, contract);
// Set state and status to draft

targetContractEntity[“statecode”] = new OptionSetValue(0);
targetContractEntity[“statuscode”] = new OptionSetValue(1);
targetContractEntity[“billingstarton”] = contractStartsOn;
targetContractEntity[“billingendon”] = contractExpiresOn;
// Set Expire Date to the Future Date

targetContractEntity[“expireson”] = DateTime.Parse(“01/01/2099”);
// Remove cancelon if it exists

if (targetContractEntity.Contains(“cancelon”)) targetContractEntity.Attributes.Remove(“cancelon”);

// Create a contract record

var createContractReq = new CreateRequest { Target = targetContractEntity };

contractResponse = (OrganizationResponse)helper.TargetOgrService.Execute(createContractReq);
newContractId = (Guid)contractResponse.Results.FirstOrDefault().Value;
// Create realted records


// Insert other related records……


// Make sure that contract detail “activeon”, “expireson” are within the range and fit into the contract

// template

// Set contract to invoiced, to lock it up

var targetContractUpdateEntity = new Entity(“contract”);

targetContractUpdateEntity[“contractid”] = newContractId;
OrganizationResponse contractUpdateResponse;
var updateRequest = new UpdateRequest();

updateRequest = new UpdateRequest { Target = targetContractUpdateEntity };
contractUpdateResponse = (OrganizationResponse)helper.TargetOgrService.Execute(updateRequest);
// Set actual expires on unless the contract is Canceled

// if the field is updated and the value is in the past contract will be

// set to expired and it won’t be possibe to calcel it

if (((OptionSetValue)contract[“statecode”]).Value != 4)

targetContractUpdateEntity[“expireson”] = contractExpiresOn;
targetContractUpdateEntity[“statecode”] = new OptionSetValue(1);
targetContractUpdateEntity[“statuscode”] = new OptionSetValue(2);
// Contract will be set to “Invoiced” state.

// if “Expireson” is in the past CRM will expire the contract

// else if “Activeon” is in the future it will remain invoiced

// else it will be set to “Active”

updateRequest = new UpdateRequest { Target = targetContractUpdateEntity };
contractUpdateResponse = (OrganizationResponse)helper.TargetOgrService.Execute(updateRequest);
// Next handle canceled contracts

if (((OptionSetValue)contract[“statecode”]).Value == 4) // Canceled

CancelContractRequest cancelReq = new CancelContractRequest();
cancelReq.ContractId = contractId;
cancelReq.CancelDate = (DateTime)contract[“cancelon”];
cancelReq.Status = new OptionSetValue(-1);
CancelContractResponse resp = (CancelContractResponse)helper.TargetOgrService.Execute(cancelReq);
//Deactivate customer if it was inactive prior to load

if (deactivateCustomerFl) DeactivateCustomer(helper, customer);

if (deactivateBillingCustomerFl) DeactivateCustomer(helper, billingCustomer);


Note: The same logic and sequence of steps can be used with either the Scribe or KingswaySoft adapter. That’s all for the blog today! You can get tips and tricks for working with Dynamics CRM delivered directly to your inbox by subscribing to our blog!

Happy CRM’ing!

Avatar for Joe D365

Joe D365

Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.