Answered by:
CRM date function fn_UTCToLocalTime is giving NULL

Question
-
Hello,
I have come across the CRM built in date function "dbo.fn_UTCToLocalTime()" which gives the local time.
Actually when I login into the crm database server and ran the below query
Select dbo.fn_UTCToLocalTime(GetUTCDate())
it is giving the result as expected.
But, when I access the database from my machine using sql authentication then the same query is giving NULL value.
Can anyone suggest why this is happening?
Your assistance will be highly appreciable. Thanks in advance.
PerfectionistWednesday, August 10, 2011 3:17 PM
Answers
-
I am sure there is
Is this stored procedure going to be in a different database than the CRM database?
You could do a retrieve or retrievemultiple call in a sql CLR function or anywhere you can use .NET, or from the CRM server in Jscript for that matter. You do a retrieve or retrievemultiple on the utcconversiontimezonecode attribute of the particular systemuser entity by their id.
Here are some examples of retrieve and retrievemultiple
RETRIEVE request examples
http://mileyja.blogspot.com/2011/04/how-to-use-retrieve-messages-in-jscript.html (JSCRIPT C#)
http://mileyja.blogspot.com/2011/06/merge-entity-records-in-microsoft.html (C#)
http://mileyja.blogspot.com/2011/06/retrieve-entity-in-microsoft-dynamics.html (C#, Silverlight)
Retrievemultiple exampleshttp://mileyja.blogspot.com/2011/03/crm-2011-retrievemultiple-calls-in.html - (JSCRIPT)
http://mileyja.blogspot.com/2011/03/editing-embedded-web-resource-contents.html - (C#)
http://mileyja.blogspot.com/2011/04/storing-and-utilizing-x509-certificates.html (C#)
http://mileyja.blogspot.com/2011/05/how-to-retrieve-users-systemusers-for.html (JSCRIPT, C#)
http://mileyja.blogspot.com/2011/06/use-fetchxml-queries-in-jscript-and-net.html (JSCRIPT, C#, FetchXML)
http://mileyja.blogspot.com/2011/06/getting-row-count-or-entity-count-in.html (JSCRIPT, C#, FetchXML, RowCount)
http://mileyja.blogspot.com/2011/06/aggregation-count-max-min-etc-and.html (JSCRIPT, C#, FetchXML, Aggregation)
http://mileyja.blogspot.com/2011/06/using-retrievemultiple-from-silverlight.html (Silverlight, C#)
http://mileyja.blogspot.com/2011/07/using-jscript-to-access-soap-web.html (JSCRIPT, Synchronous)
Jamie Miley
Check out my about.me profile!
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Marked as answer by Donna EdwardsMVP Thursday, August 18, 2011 2:12 AM
Wednesday, August 10, 2011 3:51 PMModerator
All replies
-
I guess I would suggest you forget you ever saw that function. Besides select queries against the filtered views for reporting purposes, directly working with the database isn't supported and is dangerous to the safety and supportability of your implementation.
I am going to guess though that it might have something to do with access restrictions and things on the data based on CRM roles. You would be able to test this by using windows auth with a user that has access to data in CRM.
Jamie Miley
Check out my about.me profile!
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Wednesday, August 10, 2011 3:19 PM
Wednesday, August 10, 2011 3:19 PMModerator -
Thanks for your suggestion.
But, I am writing one user defined store proc for one custom functionality to display the list of activities where I need to generate the date field columns of activities. so, inorder to get the local time, I am using this function. Is there any way to generate the date in a similar fashion without using this function?
-Thanks
PerfectionistWednesday, August 10, 2011 3:24 PM -
I am sure there is
Is this stored procedure going to be in a different database than the CRM database?
You could do a retrieve or retrievemultiple call in a sql CLR function or anywhere you can use .NET, or from the CRM server in Jscript for that matter. You do a retrieve or retrievemultiple on the utcconversiontimezonecode attribute of the particular systemuser entity by their id.
Here are some examples of retrieve and retrievemultiple
RETRIEVE request examples
http://mileyja.blogspot.com/2011/04/how-to-use-retrieve-messages-in-jscript.html (JSCRIPT C#)
http://mileyja.blogspot.com/2011/06/merge-entity-records-in-microsoft.html (C#)
http://mileyja.blogspot.com/2011/06/retrieve-entity-in-microsoft-dynamics.html (C#, Silverlight)
Retrievemultiple exampleshttp://mileyja.blogspot.com/2011/03/crm-2011-retrievemultiple-calls-in.html - (JSCRIPT)
http://mileyja.blogspot.com/2011/03/editing-embedded-web-resource-contents.html - (C#)
http://mileyja.blogspot.com/2011/04/storing-and-utilizing-x509-certificates.html (C#)
http://mileyja.blogspot.com/2011/05/how-to-retrieve-users-systemusers-for.html (JSCRIPT, C#)
http://mileyja.blogspot.com/2011/06/use-fetchxml-queries-in-jscript-and-net.html (JSCRIPT, C#, FetchXML)
http://mileyja.blogspot.com/2011/06/getting-row-count-or-entity-count-in.html (JSCRIPT, C#, FetchXML, RowCount)
http://mileyja.blogspot.com/2011/06/aggregation-count-max-min-etc-and.html (JSCRIPT, C#, FetchXML, Aggregation)
http://mileyja.blogspot.com/2011/06/using-retrievemultiple-from-silverlight.html (Silverlight, C#)
http://mileyja.blogspot.com/2011/07/using-jscript-to-access-soap-web.html (JSCRIPT, Synchronous)
Jamie Miley
Check out my about.me profile!
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Marked as answer by Donna EdwardsMVP Thursday, August 18, 2011 2:12 AM
Wednesday, August 10, 2011 3:51 PMModerator -
Hi Jamie,
What is the Minimum permission required to a user in a database other than CRM database in order to execute the Stored Procedure and Functions.
Tuesday, October 4, 2011 1:58 PM -
Hi
the problem is that the function fn_UTCToLocalTime executes another function fn_FindUserGuid
which returns the guid of the current connected user.
After getting the GUID, the procedure gets the settings from the CRM table "systemuser"
since you are using an SQL user (not active directory) the "systemuser" table will return null (since the user is not a CRM user)
as a workaround I have modified the "fn_FindUserGuid" function in the CRM databse by adding
the below to the end of the function:
if @userGuid is null
begin
if SYSTEM_USER = 'SQLuser'
begin
select @userGuid = s.SystemUserId
from SystemUserBase s
where s.DomainName = 'Domain\Username'
end
endreplace SQLuser by the username you are using to connect to the database , in my case it was "sa"
and 'Domain\Username' by any CRM username 'MyCompany\CRMadmin'
it should work now :)
the function will look like
ALTER function [dbo].[fn_FindUserGuid] ()
returns uniqueidentifier
as
begindeclare @userGuid uniqueidentifier
--- test whether the query is runing by priviledged user with user role of CRMReaderRole
--- if it is dbo, we trust it as well.
--- There is an issue in SQL. If the user is a dbo, if it not member of any role
if (is_member('CRMReaderRole') | is_member('db_owner')) = 1
begin
select @userGuid = cast(context_info() as uniqueidentifier)
endif @userGuid is null
begin
select @userGuid = s.SystemUserId
from SystemUserBase s
where s.DomainName = SUSER_SNAME()
end
if @userGuid is null
begin
if SYSTEM_USER = 'sa'
begin
select @userGuid = s.SystemUserId
from SystemUserBase s
where s.DomainName = 'MyCompany\CRMadmin'
end
end
return @userGuid
end- Edited by ShadowWorker Thursday, January 5, 2012 4:58 PM
Thursday, January 5, 2012 4:57 PM