Monthly Archives: September 2012

Working with MS CRM 2011 data types

I have seen many question in MS CRM Development where developers asking for MS CRM 2011 data type related question, like how to set lookup field, how to set/retrieve optionset value. so I thought to write code to show how we can set different data type field in MS CRM 2011.

Below is the example to create account using late bound

Entity _Account = new Entity();
_Account.LogicalName = “account”;
//setting text field
_Account.Attributes.Add(“name”, “Myaccount123”);

//setting optionset field
_Account.Attributes.Add(“accountcategorycode”, new OptionSetValue(2));

//setting datetime field
_Account.Attributes.Add(“new_collectiondate”, new DateTime(2012, 3, 25));

//setting currency field
_Account.Attributes.Add(“creditlimit”, new Money(300));

//setting lookup
_Account.Attributes.Add(“parentaccountid”, new EntityReference(“account”, new Guid(“XXX-XXX……..”)));

//setting decimal
_Account.Attributes.Add(“new_executivecommission”, new Decimal(55.5));

//setting boolean
_Account.Attributes.Add(“new_isbilled”, true);

service.Create(_Account);

we should get new account created like below

In next post I will show how to retrieve different data types fields using server side code.

Enjoy !!!

Advertisement

2 Comments

Filed under MS CRM 2011

Fetch team members for selected Team

I got one requirement to get all team members based on team, so I thought to share code here so that it can help other crm developers. you can use below code

private EntityCollection GetTeammembers(IOrganizationService _iService,Guid TeamID)
{
EntityCollection _Teammembers = null;
Guid _UserId = Guid.Empty;
EntityCollection col = new EntityCollection();
QueryExpression _Query = new QueryExpression();
_Query.EntityName = “systemuser”;
_Query.ColumnSet = new ColumnSet(new string[] { “systemuserid”, “firstname” });
LinkEntity _LinkEntity = new LinkEntity();
_LinkEntity.LinkFromAttributeName = “systemuserid”;
_LinkEntity.LinkToAttributeName = “systemuserid”;
_LinkEntity.LinkFromEntityName = “systemuser”;
_LinkEntity.LinkToEntityName = “teammembership”;
_LinkEntity.JoinOperator = JoinOperator.Natural;
_LinkEntity.LinkCriteria = new FilterExpression();
_LinkEntity.LinkCriteria.AddCondition(“teamid”, ConditionOperator.Equal, TeamID);
_Query.LinkEntities.Add(_LinkEntity);
_Teammembers = _Service.RetrieveMultiple(_Query);
return _Teammembers;
}

Enjoy !!!

Leave a comment

Filed under MS CRM 2011

InstantiateTemplateRequest using Javascript – MS CRM 2011

If you want to create new email using template or want to place email template contents to email while replying, you need to use”InstantiateTemplateRequest” to initiate email template. It has three below parameters

TemplateId (ID of the email template that you want to use)

ObjectId (associated entity record id, (for example you have used data field from user entity then this will be the system user id)

ObjectType (data field’s associated entity name)

In my example, I have used system user entity’s data fields in my template, so here is the code to Initiate email template using Soap request.

function InstatiateEmailTemplate(TemplateID, FromUser) {
var Soapreq = “<s:Envelope xmlns:s=’http://schemas.xmlsoap.org/soap/envelope/’>&#8221; +
“<s:Body><Execute xmlns=’http://schemas.microsoft.com/xrm/2011/Contracts/Services&#8217; xmlns:i=’http://www.w3.org/2001/XMLSchema-instance’>&#8221; +
“<request i:type=’b:InstantiateTemplateRequest’ xmlns:a=’http://schemas.microsoft.com/xrm/2011/Contracts&#8217; xmlns:b=’http://schemas.microsoft.com/crm/2011/Contracts’>&#8221; +
“<a:Parameters xmlns:c=’http://schemas.datacontract.org/2004/07/System.Collections.Generic’>&#8221; +
“<a:KeyValuePairOfstringanyType>” +
“<c:key>TemplateId</c:key>” +
“<c:value i:type=’d:guid’ xmlns:d=’http://schemas.microsoft.com/2003/10/Serialization/’>&#8221; + TemplateID + “</c:value>” +
“</a:KeyValuePairOfstringanyType>” +
“<a:KeyValuePairOfstringanyType>” +
” <c:key>ObjectType</c:key>” +
”          <c:value i:type=’d:string’ xmlns:d=’http://www.w3.org/2001/XMLSchema’>systemuser</c:value>&#8221; +
”       </a:KeyValuePairOfstringanyType>” +
”      <a:KeyValuePairOfstringanyType>” +
”       <c:key>ObjectId</c:key>” +
”      <c:value i:type=’d:guid’ xmlns:d=’http://schemas.microsoft.com/2003/10/Serialization/’>&#8221; + FromUser + “</c:value>” +
”   </a:KeyValuePairOfstringanyType>” +
“</a:Parameters>” +
“<a:RequestId i:nil=’true’ />” +
“<a:RequestName>InstantiateTemplate</a:RequestName>” +
“</request>” +
“</Execute>” +
“</s:Body>” +
“</s:Envelope>”;
}

Enjoy !!

Note: if you are copying this code, please make sure to change quotes.

4 Comments

Filed under MS CRM 2011