0

I want a trigger on the metadata table of CRM that fire when people delete fields.

This is working fine, but I don't know how to get the user who sent the request to delete the field.

There's any way to know this?

This is the trigger:

CREATE TRIGGER Trg_InsertMetadataAttributeDELETE
   ON  metadataschema.Attribute
   AFTER DELETE
AS 
BEGIN
    SET NOCOUNT ON;

    INSERT INTO MIGRACIONDATOS.TablaLogAtributos (Fecha, Entidad, Atributo)
    SELECT Getdate(), E.Name, A.Name
       FROM Deleted A
            INNER JOIN metadataschema.Entity E 
                ON A.EntityID = E.EntityID

END
GO

The CRM organization is not upgraded to UR12 and it cannot be upgraded

UPDATE: It is not posible if the roll up if inferior to 12.

1
  • Wouldn't it be the CRMAPP user you see here ?
    – Alex
    Dec 23, 2014 at 15:52

2 Answers 2

2

Manipulating the CRM database in any way other than tweaking indexes is unsupported by Microsoft. Assuming you have UR12 installed (if not, I highly suggest you do it ASAP) the Microsoft.Xrm.Metadata.Query provides a supported tool.

Retrieve and Detect Changes to Metadata

The classes in the Microsoft.Xrm.Sdk.Metadata.Query namespace and the RetrieveMetadataChangesRequest and RetrieveMetadataChangesResponse classes let you build efficient metadata queries and capture changes to metadata as they occur over time.

[Code samples are provided in the linked MSDN section]

3
  • Sorry, I don't have UR12 and we can't upgrade yet. UR12 provides powerfull tools but it's not an option at this moment.
    – Sxntk
    Dec 23, 2014 at 16:10
  • Edited the question to include that vital information. I'll leave the answer here in case someone with UR12 comes by with a similar need.
    – Alex
    Dec 23, 2014 at 16:15
  • This is the correct answer and it is not posible if the roll up is inferior to 12. Thank you.
    – Sxntk
    Jan 26, 2015 at 18:23
1
See if these help you :  All executable in triggers.

-- get the user name
SELECT SUSER_SNAME();

-- get the event data. Which you can parse and retrieve the event command, scheme, objects....
DECLARE @EventData XML;
    SET @EventData  = EVENTDATA();
-- Then parse the xml. For example :
SELECT @EventData.value('(/EVENT_INSTANCE/EventType)[1]',   'NVARCHAR(100)'), 
SELECT @EventData.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'NVARCHAR(MAX)'),

-- Get connection specific data 
SELECT *
FROM sys.dm_exec_connections
    WHERE session_id = @@SPID;

-- Get session specific data on whomever executed the process that fired the trigger
SELECT *
FROM sys.dm_exec_sessions
    WHERE session_id = @@SPID;

see link for the usage of EVENTDATA() : http://msdn.microsoft.com/en-us/library/ms173781.aspx

3
  • Hello, the EventData() return null, I think is becasue it is a DML proccess and not a DDL.
    – Sxntk
    Jan 8, 2015 at 19:54
  • Selecting the SUSER_NAME(), or from sys.dm_exec_sessions will work. Jan 13, 2015 at 12:36
  • I think you can't know this on a trigger, at least the information I need, thank you anyway
    – Sxntk
    Jan 26, 2015 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.