Search code examples
sqltriggersdynamics-crm-2011dynamics-crmcrm

How to know the user context on a trigger


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.


Solution

  • 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]