Wednesday, July 08, 2015

Deployment Manager via SQL in CRM 2011

Although this is an unsupported way, we still need to get this sometimes... a developer hack :)


DECLARE @DomainName VARCHAR(max)

SET @DomainName = 'domain\username'--e.x. 'sandnes\ganesh'
DECLARE @Id AS UNIQUEIDENTIFIER
DECLARE @UserId AS UNIQUEIDENTIFIER
DECLARE @RoleId AS UNIQUEIDENTIFIER

SELECT @RoleId = id
FROM   mscrm_config..securityrole
WHERE  NAME = 'Administrator'

SELECT @UserId = id
FROM   mscrm_config..systemuser
WHERE  NAME = @DomainName

IF ( @UserId IS NULL )
  BEGIN
      SET @UserId = Newid()

      INSERT INTO mscrm_config..systemuser
                  (id,
                   NAME,
                   defaultorganizationid)
      VALUES     (@UserId,
                  @DomainName,
                  '00000000-0000-0000-0000-000000000000')
  END

SELECT @Id = id
FROM   mscrm_config..systemuserroles
WHERE  systemuserid = @UserId

IF ( @Id IS NULL )
  BEGIN
      SET @Id = Newid()

      INSERT INTO mscrm_config..systemuserroles
                  (id,
                   securityroleid,
                   systemuserid)
      VALUES     (@Id,
                  @RoleId,
                  @UserId)
  END 

Monday, June 29, 2015

Merge records with automatic selection of all fields with data

When merging the records in GUI, you have the option of selecting the option which updates the master record with the values from the child record when the value is missing in the master record.

The below code does the same. If does a retrieve on the master and child record and then compares the columns from the child record with the master record and update the "UpdateContent" property of the merge request.

For merging custom entities, the first part of the code can be used and If we have a custom masterid created, the lookup value can be updated with the master record id and the child record can be inactivated/deleted.



Code:
public static bool MergeRecord(IOrganizationService _service, string entName, string srcId, string targetId)
        {
            bool result = false;
            MergeRequest mreq = new MergeRequest();
            Guid targetRecordID = new Guid(targetId);
            Guid tobemergedRecordID = new Guid(srcId);
            Entity srcEntity = _service.Retrieve(entName, tobemergedRecordID, new ColumnSet(true));
            Entity tgtEntity = _service.Retrieve(entName, tobemergedRecordID, new ColumnSet(true));
            Entity updContent = new Entity(entName);
            foreach (KeyValuePair<String, Object> attr in srcEntity.Attributes)
            {
                if (!tgtEntity.Attributes.Contains(attr.Key))
                { 
                    updContent.Attributes.Add(attr);
                }
            }

            mreq.Target = new EntityReference(entName, targetRecordID);  // Target Account ID , where other account will be mergered
            mreq.SubordinateId = tobemergedRecordID;
            mreq.PerformParentingChecks = false;
            mreq.UpdateContent = updContent;
            try
            {
                MergeResponse mresp = (MergeResponse)_service.Execute(mreq);
                result = true;
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                LogMessage(message, true);
                result = false;
            }

            return result;
        }





Thursday, March 05, 2015

Import a solution from CRM 2011 to CRM 2015

If you keen on trying CRM 2015 and not willing to choose the upgrade path of CRM 2011-> CRM 2013 -> CRM 2015.... then you are not alone...

When you try to import the solution you will get an error saying the solution cannot be imported from CRM 2011...

Since the solution file is a mere XML, as a developer, I wanted to play around it..

This is what I did. I changes the customization.xml and updated the xml header like below.

<ImportExportXml version="7.0.0000.4013" SolutionPackageVersion="7.0" languagecode="1044" generatedBy="CrmLive" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'

And the import went fine. Now it's for me to just start modifying the UI and fixing the javascript part :)


Have fun with crm 2015