DateTime fields in CRM 2015

Hi,

We recently had a requirement wherein we were working with DateTime fields in a Plugin. It was related to Time Zone.

These articles were of extreme help.

http://develop1.net/public/post/Dynamics-CRM-DateTimes-the-last-word.aspx

http://inogic.com/blog/2015/06/handling-datetime-fields-in-microsoft-dynamics-crm-update-1/

http://ttidbit.blogspot.com/2013/01/crm-timezone-codes.html

Sample Code to use LocalTimeFromUtcTimeRequest


var currentUserSettings = organizationProxy.RetrieveMultiple(
new QueryExpression("usersettings")
{
ColumnSet = new ColumnSet("timezonecode"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("systemuserid", ConditionOperator.EqualUserId)
}
}
}).Entities[0];

var timeZoneCode = currentUserSettings.Attributes["timezonecode"].ToString();

DateTime time1 = DateTime.UtcNow;

LocalTimeFromUtcTimeRequest convert = new LocalTimeFromUtcTimeRequest();
convert.UtcTime = time1.ToUniversalTime();
convert.TimeZoneCode = Convert.ToInt32(timeZoneCode);
LocalTimeFromUtcTimeResponse response = (LocalTimeFromUtcTimeResponse)organizationProxy.Execute(convert);
MessageBox.Show(response.LocalTime.ToString());

 

The interesting part was that our plugin was running under the context of System account, so to get the logged in user from UI, we added an additional field and populated the value of time zone in it on on load using current logged in user’s information, and used the same field in plugin.

Hope it helps..

Advertisements

Plugin on Retrieve Multiple in CRM 2015.

Recently we had a requirement to filter sub grid on one of the forms. We thought of using Plugin on Retrieve Multiple for this.

Here we were showing Opportunity Sub Grid on Account form.

So we created a view for Opportunity that will be used as Sub Grid. Used it as Related Record Sub Grid so that we can have the GUID of the Account record being passed to the plugin.

The problem we faced à Sorting and Paging stopped working for the Sub Grid.

http://crmtipoftheday.com/2015/10/05/limitations-of-retrieve-plugins/v


void IPlugin.Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));

// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Query") && context.InputParameters["Query"] is QueryExpression && context.MessageName == "RetrieveMultiple")
{
QueryExpression qe = (QueryExpression)context.InputParameters["Query"];
if (qe.EntityName == "opportunity")
{
string currentCustomerId = string.Empty;
ConditionExpression[] filters = qe.Criteria.Conditions.ToArray();
foreach (var filter in filters)
{
currentCustomerId = filter.Values[0].ToString();
}

FetchXmlToQueryExpressionRequest req = new FetchXmlToQueryExpressionRequest();
req.FetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='opportunity'>" +
" <attribute name='name' />" +
"<attribute name='customerid' />" +
"<attribute name='estimatedvalue' />" +
"<attribute name='statuscode' />" +
"<attribute name='opportunityid' />" +
"<order attribute='name' descending='false' />" +
"<link-entity name='account' from='accountid' to='parentaccountid' alias='ad'>" +
" <filter type='and'>" +
" <filter type='or'>" +
" <condition attribute='accountid' operator='eq' uiname='aaass' uitype='account' value=' " + currentCustomerId + "' />" +
" <condition attribute='accountid' operator='under' uiname='aaass' uitype='account' value='" + currentCustomerId + "' />" +
"</filter>" +
"</filter>" +
"</link-entity>" +
"</entity>" +
"</fetch>";
FetchXmlToQueryExpressionResponse resp = (FetchXmlToQueryExpressionResponse)service.Execute(req);
context.InputParameters["Query"] = resp.Query;

}
}

Hope it helps..

Finding shared records in CRM

To find all the records(say lead)  shared with any particular user we can make use of following query

It will return us all records(lead) shared with the user directly( i.e. through Action–>Sharing and user)

select  fl.subject

from
PrincipalObjectAccess poa , FilteredLead fl, FilteredSystemUser fsu
where
poa.ObjectTypeCode = 4
and poa.ObjectId = fl.leadid
and poa.PrincipalId = fsu.systemuserid
and fsu.domainname=SYSTEM_USER

// fsu.fullname=’name of user’

And to get the records shared with the user indirectly (i.e. through Action–>Sharing and Team (user belongs to that team)

select  fl.subject
from 
FilteredLead fl,  PrincipalObjectAccess poa,  FilteredTeam ft
where
poa.ObjectTypeCode = 4 

and poa.ObjectId =fl.leadid
and poa.PrincipalId = ft.teamid and 

fl.owneridname not in (select fullname from filteredsystemuser
where domainname=SYSTEM_USER)
and
ft.teamid  in (select ft.teamid from filteredteammembership fm
, filteredsystemuser fsu, filteredteam ft
where fm.systemuserid=fsu.systemuserid and
ft.teamid=fm.teamid
and fsu.domainname=SYSTEM_USER
)

Bye

Finding Lead records that have an associated activity with them

Here in the query one more condition is added which is when the Rating is Hot. 

select leadqualitycodename,* from filteredlead As fl inner join dbo.FilteredActivityParty as fap
ON fl.leadid=fap.partyid
and fl.leadid=‘yourleadid’ and leadqualitycodename=‘Hot’ 

Finding all the opportunities shared with the user in CRM

To find the opportunity record shared with a particular user in Microsoft Dynamics Crm

select o.name as OpportunityName,p.AccessRightsMask from opportunityBase o
left join PrincipalObjectAccess
p on p.objectid=o.opportunityid left join systemuser u
on p.principalid=u.systemuserid where u.fullname=’nishant r’

or u.domainname=SYSTEM_USER

AccessRightsMask values and what they mean

AppendAccess 4 – Specifies the right to append the specified object to another object.
AppendToAccess 8 – Specifies the right to append another object to the specified object.
AssignAccess 0x80 -Specifies the right to assign the specified object to another security principal.
CreateAccess 0x10 -Specifies the right to create an instance of the object type.
DeleteAccess 0x20 -Specifies the right to delete the specified object.
ReadAccess 1 -Specifies the right to read the specified type of object.
ShareAccess 0x40 -Specifies the right to share the specified object.
WriteAccess 2 – Specifies the right to update (write to) the specified object.

3– Read + Write

65539– Read + Write + Delete

851991– All the rights

262145– Share+Read etc…

फिर मिलते हें
Bye