Monthly Archives: February 2013

Restore Backup Database Failed Because Database Is in Use

How to restore backup database while the existing database is in use

Sometimes, backing up and restoring database is not so straight forward. I used to get the issue of the current database is in use and cannot restore the backup database. But my life is saved when I found the following scripts. 🙂

USE MASTER
GO

ALTER DATABASE MyDatabase
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
Go

RESTORE DATABASE MyDatabase
FROM DISK = N'[file address]'
Go

ALTER DATABASE MyDatabase
SET MULTI_USER with ROLLBACK IMMEDIATE
Go

 

Leave a comment

Filed under Failed to Restore Backup Database, Microsoft SQL Server

UTC time and Local time in CRM 2011

How to convert UTC time into local time in CRM 2011

Sometimes, we need to Convert UTC time into local time for various reasons. If you are in that condition, here is the some lines of code for you to do it.

private static string RetrieveLocalTimeFromUTCTime(DateTime utcTime, string crmUri)
{
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
Uri organizationUri = new Uri(crmUri);

using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(organizationUri, null, credentials, null))
{
IOrganizationService _serviceProxy = (IOrganizationService)serviceProxy;

var _timeZoneCode = _serviceProxy.RetrieveMultiple(
new QueryExpression(“usersettings”)
{
ColumnSet = new ColumnSet(“timezonecode”),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression(“systemuserid”, ConditionOperator.EqualUserId)
}
}
}).Entities[0].Attributes[“timezonecode”];

var request = new LocalTimeFromUtcTimeRequest();
request.TimeZoneCode = (int)_timeZoneCode;
request.UtcTime = utcTime.ToUniversalTime();

var response = (LocalTimeFromUtcTimeResponse)_serviceProxy.Execute(request);

return response.LocalTime.ToString();
}
}

Leave a comment

Filed under Convert UTC time into Local time, MS Dynamic CRM 2011

CRM 2011 SOP Integration Mode

How to turn on SOP Integration Mode in CRM 2011

If you want to integrate your CRM 2011 Organization with a separate ERP or Finance system using a product like Scribe or SmartConnect, Dynamics CRM 2011 SOP Integration Mode will make it much easier for you to do. It is not known much and hardly documented setting.

The “IsSOPIntegrationEnabled” setting makes the following changes:

  • Replaces the ‘Create Invoice’ button on the Sales Order ribbon with a ‘Submit Order’ button
  • Enables a series of ‘Submitted Status’ fields on the Sales Order entity and Sales Order properties window
  • Enables a ‘Submitted’ statecode value for the Sales Order entity
  • Allows you to specify an ‘Integration User’ that can then make changes to Sales Order and Invoice records through the SDK that would normally be read-only to all other users.

With integration mode enabled, this process flow is possible:

  1. CRM User submits a Sales Order (making the record read-only to normal users)
  2. Integration software picks up the CRM sales order and creates a record in your ERP system
  3. The CRM ‘Integration User’ account can write back to the CRM Sales Order to indicate that it has been successfully created in your ERP system.  This would need to happen through the SDK running in the context of the user account that has been marked as the integration user.
  4. The Finance Department can review the sales order, apply taxes and currency changes etc. to the record, then post it in the ERP system.
  5. The Integration picks up posted Invoices from your ERP system and creates a copy in CRM for reference by CRM users.
  6. As payments are applied to the invoice in your ERP system, another integration updates the payment status of the copy Invoice in CRM.

Turning this mode on takes a few steps, the “IsSOPIntegrationEnabled” value is held in the OrganizationBase table of your CRM Organization database.  Updates direct to SQL are unsupported, but Sonoma Partners have recently released this tool  that allows you to make changes to the OrganizationBase table in a supported way. But if you want to do it by yourself with C#, the following lines of codes will help you to do so.

public static void ActivateIntegrationMode(string CRM_USERNAME, string CRM_PASSWORD, string CRM_DOMAIN, string CRM_ORG_URL, bool turnOn)
{
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new NetworkCredential(CRM_USERNAME, CRM_PASSWORD, CRM_DOMAIN);
Uri organizationUri = new Uri(CRM_ORG_URL);

using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(organizationUri, null, credentials, null))
{
IOrganizationService _serviceProxy = (IOrganizationService)serviceProxy;

Guid orgId = ((WhoAmIResponse)_serviceProxy.Execute(new WhoAmIRequest())).OrganizationId;
Guid userId = ((WhoAmIResponse)_serviceProxy.Execute(new WhoAmIRequest())).UserId;

//– Update Org to SOP Integration Mode
if (turnOn)
{
Entity org = new Entity(“organization”);
org[“issopintegrationenabled”] = true;
org[“organizationid”] = orgId;
_serviceProxy.Update(org);

//– Update User to Integration Mode
Entity user = new Entity(“systemuser”);
user[“systemuserid”] = userId;
user[“isintegrationuser”] = true;
_serviceProxy.Update(user);
}
else
{
Entity org = new Entity(“organization”);
org[“issopintegrationenabled”] = false;
org[“organizationid”] = orgId;
_serviceProxy.Update(org);

//– Update User to Integration Mode
Entity user = new Entity(“systemuser”);
user[“systemuserid”] = userId;
user[“isintegrationuser”] = false;
_serviceProxy.Update(user);
}
}
}

[Reference: http://crmuk.wordpress.com/2012/11/21/crm-2011-sop-integration-mode/]

Leave a comment

Filed under MS Dynamic CRM 2011, SOP Integration Mode