Category Archives: MS CRM 4.0

An error occurred while parsing EntityName

While integrating CRM orders to our custom financial system webservice I got error “An error occurred while parsing EntityName” in my plugin. Our custom financial service accepts xml string as input, so while sending data in xml form, sometime account name contains special character like ‘&’ which is a illigal character in I wrote below function to replace illigal characters in xml request

private string FormatText(string XmlRequest)
string _ModifiedXML = XmlRequest.Replace(“&”, “&”);
_ModifiedXML = _ModifiedXML.Replace(“”, “>”);
_ModifiedXML = _ModifiedXML.Replace(“‘”, “'”);
_ModifiedXML = _ModifiedXML.Replace(“””, “"”);

return _XmlRequest;

Hope it will help somebody !!!

Leave a comment

Filed under MS CRM 4.0

Get Max value using Javascript

 If you want to fetch Max value from CRM entity record using Javascript, you can use below code, remember to change entity and field name accordingly

var authenticationHeader = GenerateAuthenticationHeader();

// Prepare the SOAP message.

var xml = “<?xml version=’1.0′ encoding=’utf-8’?>”+

“<soap:Envelope xmlns:soap=’'”+

” xmlns:xsi=’'”+

” xmlns:xsd=’’>”+



“<RetrieveMultiple xmlns=’’>”+

“<query xmlns:q1=’'”+

” xsi:type=’q1:QueryExpression’>”+

“<q1:EntityName>EntityName</q1:EntityName>”+ //change entity name

“<q1:ColumnSet xsi:type=’q1:ColumnSet’>”+


“<q1:Attribute>FieldName</q1:Attribute>”+ //change field name



“<q1:Orders>” +

“<q1:Order>” +

“<q1:AttributeName>FieldName</q1:AttributeName>” + //change field name

“<q1:OrderType>Descending</q1:OrderType>” +

“</q1:Order>” +

“</q1:Orders>” +





// Prepare the xmlHttpObject and send the request.

var xHReq = new ActiveXObject(“Msxml2.XMLHTTP”);

xHReq.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);


xHReq.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8″);

xHReq.setRequestHeader(“Content-Length”, xml.length);


// Capture the result.

var resultXml = xHReq.responseXML;

// Check for errors.

var errorCount = resultXml.selectNodes(‘//error’).length;

if (errorCount != 0)


var msg = resultXml.selectSingleNode(‘//description’).nodeTypedValue;





var results = resultXml.getElementsByTagName(‘BusinessEntity’);

var msg = “”;

if (results.length == 0)









var MaxValue = results[0].selectSingleNode(‘./q1:FieldName’).nodeTypedValue;   //change name of the field to get Max value




Leave a comment

Filed under JS Script, MS CRM & Javascript

Using CompoundUpdateRequest in Dynamics CRM 4.0

Sometime when we are integrating ms crm with another application we need to send CRM data to another application and also need to keep track if data is already sent or not. For example if you are sending sales order and sales order line items and you need to update sales order and line item after sending, you can use  compoundupdaterequest message to update sales order and order line items in one go.


private BusinessEntityCollection ReadAllOrderLineItems(Guid _ID, ICrmService _iService)


            RetrieveMultipleResponse retrieved = null;



                ConditionExpression _Condition = new ConditionExpression();

                _Condition.Operator = ConditionOperator.Equal;

                _Condition.AttributeName = “salesorderid”;

                _Condition.Values = new object[] { _ID };

                 FilterExpression _Filter = new FilterExpression();

                _Filter.FilterOperator = LogicalOperator.And;

                _Filter.Conditions.AddRange(new ConditionExpression[] { _Condition });

                 QueryExpression query = new QueryExpression();

                query.ColumnSet = new AllColumns();

                query.Criteria = _Filter;

                query.EntityName = “salesorderdetail”;

                RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();

                retrieve.Query = query;

                retrieve.ReturnDynamicEntities = true;

                retrieved = (RetrieveMultipleResponse)_iService.Execute(retrieve);


            catch (SoapException Ex)


                throw Ex;


            catch (Exception Excep)


                throw Excep;


            return retrieved.BusinessEntityCollection;


//Get all orderline items for particular salesorderid
BusinessEntityCollection _OrderLineItems = ReadAllOrderLineItems(_SalesorderID,_iCrmService); //I have written this function to fetch all order line items based on salesorder id.
 //Create Dynamic entity array to store salesorderdetails
 DynamicEntity[] _SalesorderDetailArr=new DynamicEntity[_OrderLineItems.BusinessEntities.Count];
 for(int i=0; i<_OrderLineItems.BusinessEntities.Count; i++)
               //get salesorder key
                DynamicEntity _ExistingSalesorderItem = (DynamicEntity)_OrderLineItems.BusinessEntities[i];
 // create properties for sales order detail, in my example I have created a bit field in sales order and sales order detail

                DynamicEntity _Salesorderitem=new DynamicEntity();
                _Salesorderitem.Name = “salesorderdetail”;
                 //Add property to set created flag
                 CrmBooleanProperty _Created = new CrmBooleanProperty();
                _Created.Name = “new_created”;
                _Created.Value = new CrmBoolean();
                _Created.Value.Value = true;
                 //set key property
                KeyProperty _SalesorderDetailKey = new KeyProperty();
                _SalesorderDetailKey.Name = “salesorderdetailid”;
                _SalesorderDetailKey.Value = new Key();
                _SalesorderDetailKey.Value.Value = ((Key)_ExistingSalesorderItem.Properties[“salesorderdetailid”]).Value;
                _SalesorderDetailArr[i] = _Salesorderitem;

            DynamicEntity _Salesorder = new DynamicEntity();
            _Salesorder.Name = “salesorder”;
            _Salesorder.Properties.Add(new KeyProperty(“salesorderid”, new Key(_ID)));
            CrmBooleanProperty _Created = new CrmBooleanProperty();
            _Created.Name = “new_created”;
            _Created.Value = new CrmBoolean();
            _Created.Value.Value = true;
            TargetCompoundDynamic _TargetUpdate = new TargetCompoundDynamic();
            _TargetUpdate.ChildEntities = _SalesorderDetailArr;
            _TargetUpdate.Entity = _Salesorder;
            CompoundUpdateRequest _Request = new CompoundUpdateRequest();
            _Request.Target = _TargetUpdate;
            CompoundUpdateResponse _Response = (CompoundUpdateResponse)_iService.Execute(_Request);


Filed under MS CRM 4.0, Workflow & Plugin Development

Customization Import from CRM online to Onpremise

Today I tried to import my customization.xml from CRM online to onpremise and I got error

 “Either the file could not be uploaded, or this is not a valid Customization file.”

I was surprised. I tried to import the same file to my CRM online environment again and it imported successfully. So my first thought was to enable trace and check for log files, I got one log file. I check that log file using CRM 4.0 Trace Log Viewer and I found an exception message

“The element ‘savedquery’ has invalid child element ‘visualizations’. List of possible elements expected:’Description,columnsetxml’.”

So “Visualization” was the main culprit here. In CRM online visualization represents to Charts, so a visualization tags is added under savedquery node. So I modified my customization xml file in visual studio and deleted all visualization tags and then imported my customization file and it worked like a charm.


Leave a comment

Filed under MS CRM 4.0

Close Activity based on Due Date using workflow


I got one requirement from my client where we need to complete Email activity based on it’s Due date. so I have to check if current date is equal to email’s due date then close email activity. So I created a workflow to implement this. Here are the steps to create workflow

1.       Create new workflow select Entity as Email, Give some name to workflow like “Close Email”, Select new Blank workflow.

2.       Select Run when record is created.

3.       Select “Wait Condition”


Timeout until Email:Due Date

5.       Select Row under and add new step “Change record status” and set email status as completed.

6.       It should look like as below.



7.       Publish your workflow and create Email record and set due date. workflow will close email activity based on it’s due date.


Cheer !!!

Leave a comment

Filed under MS CRM 4.0, Workflow & Plugin Development

Create Opportunity Relationship record

Sometime we need to create “Opportunity Relationship” record through code. We can use below code to create Opportunity relationship record. Basically we need to set three values while creating opportunity relationship record customerid, opportunityid and opportunityroleid.

//I have used Dynamic entity to create opportunity relationship record

DynamicEntity  _OpportunityRelationship = new DynamicEntity();

//Set entity name as “customeropportunityrole”         

 _OpportunityRelationship.Name = “customeropportunityrole”;

//Create property to set customer 

CustomerProperty _OpportunityCustomer=new CustomerProperty();

  _ OpportunityCustomer.Name=”customerid”;

 _ OpportunityCustomer.Value=new Customer();

 _ OpportunityCustomer.Value.type = “account”; //Set customer type (I am using account here)

 _ OpportunityCustomer.Value.Value=CustomerGUID;  //replace CustomerGUID with customer GUID


//set Opportunity ID 

LookupProperty _OpportunityID=new LookupProperty();

 _ OpportunityID.Name=”opportunityid”;

 _ OpportunityID.Value=new Lookup();

 _ OpportunityID.Value.Value=OpportunityID; //replace OpportunityID with opportunity GUID


 //Set Opportunity Role

//you can create Relationship Roles from Setting->Business Management->Relationship Roles        

 LookupProperty _OpportunityRole = new LookupProperty();

 _OpportunityRole.Name = “opportunityroleid”;

 _OpportunityRole.Value = new Lookup();

   _OpportunityRole.Value.Value =GUIDofRelationshipRole; //Replace GUIDofRelationshipRole with Relationship role Guid.


  TargetCreateDynamic targetCreate = new TargetCreateDynamic();

  targetCreate.Entity = _OpportunityRelationship;

// Create the request object.

 CreateRequest create = new CreateRequest();

  // Set the properties of the request object.

  create.Target = targetCreate;

 // Execute the request.

 CreateResponse created = (CreateResponse)_Service.Execute(create);

 Hope it will help somebody.

Cheer :)

Leave a comment

Filed under MS CRM 4.0, Workflow & Plugin Development

Microsoft Dynamics CRM MVP

Just want to share with you all that I won Dynamics CRM MVP award. It is great honor to me and I would like to thank Microsoft and Dynamics CRM community for this recognition. Special thanks to Jim to submit my nomination for this great honor.

I would like to thank all my friends,my family members,my colleagues for their cooperation and help to achieve this award.


Filed under MS CRM 4.0