CRM 2015 How to read audit data in clear form? - dynamics-crm

How can I read from AuditBase using sdk 2015? How can I read the data in clear and understandable form?

Check out these SDK functions, Audit entity messages and methods
DeleteAuditDataRequestm Deletes all partitions containing audit data created before a given end date.
RetrieveAttributeChangeHistoryRequest, Retrieves all changes to a specific attribute.
RetrieveAuditDetailsRequest, Retrieves the full audit details of a particular audit record. The record to retrieve is specified in the AuditId property.
RetrieveRequest, Retrieves an audit record. You can also call the IOrganizationService.Retrieve method.
RetrieveMultipleRequest, Retrieves a collection of audit records. You can also call the IOrganizationService.RetrieveMultiple method.
RetrieveRecordChangeHistoryRequest, Retrieves all changes to a specific entity.

Related

Loading records into Dynamics 365 through ADF

I'm using the Dynamics connector in Azure Data Factory.
TLDR
Does this connector support loading child records which need a parent record key passed in? For example if I want to create a contact and attach it to a parent account, I upsert a record with a null contactid, a valid parentcustomerid GUID and set parentcustomeridtype to 1 (or 2) but I get an error.
Long Story
I'm successfully connecting to Dynamics 365 and extracting data (for example, the lead table) into a SQL Server table
To test that I can transfer data the other way, I am simply loading the data back from the lead table into the lead entity in Dynamics.
I'm getting this error:
Failure happened on 'Sink' side. ErrorCode=DynamicsMissingTargetForMultiTargetLookupField,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=,Source=,''Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=Cannot find the target column for multi-target lookup field: 'ownerid'.
As a test I removed ownerid from the list of source columns it loads OK.
This is obviously a foreign key value.
It raises two questions for me:
Specifically with regards to the error message: If I knew which lookup it needed to use, how can I specify which lookup table it should validate against? There's no settings in the ADF connector to allow me to do this.
This is obviously a foreign key value. If I only had the name (or business key) for this row, how can I easily lookup the foreign key value?
How is this normally done through other API's, i.e. the web API?
Is there an XRMToolbox addin that would help clarify?
I've also read some posts that imply that you can send pre-connected data in an XML document so perhaps that would help also.
EDIT 1
I realised that the lead.ownertypeid field in my source dataset is NULL (that's what was exported). It's also NULL if I browse it in various Xrmtoolbox tools. I tried hard coding it to systemuser (which is what it actually is in the owner table against the actual owner record) but I still get the same error.
I also notice there's a record with the same PK value in systemuser table
So the same record is in two tables, but how do I tell the dynamics connector which one to use? and why does it even care?
EDIT 2
I was getting a similar message for msauto_testdrive for customerid.
I excluded all records with customerid=null, and got the same error.
EDIT 2
This link appears to indicate that I need to set customeridtype to 1 (Account) or 2 (Contact). I did so, but still got the same error.
Also I believe I have the same issue as this guy.
Maybe the ADF connector suffers from the same problem.
At the time of writing, #Arun Vinoth was 100% correct. However shortly afterwards there was a documentation update (in response to a GitHub I raised) that explained how to do it.
I'll document how I did it here.
To populate a contact with against a parent account, you need the parent accounts GUID. Then you prepare a dataset like this:
SELECT
-- a NULL contactid means this is a new record
CAST(NULL as uniqueidentifier) as contactid,
-- the GUID of the parent account
CAST('A7070AE2-D7A6-EA11-A812-000D3A79983B' as uniqueidentifier) parentcustomerid,
-- customer id is an account
'account' [parentcustomerid#EntityReference],
'Joe' as firstname,
'Bloggs' lastname,
Now you can apply the normal automapping approach in ADF.
Now you can select from this dataset and load into contact. You can apply the usual automapping approach, this is: create datasets without schemas. Perform a copy activity without mapping columns
This is the ADF limitation with respect to CDS polymorphic lookups like Customer and Owner. Upvote this ADF idea
Workaround is to use two temporary source lookup fields (owner team and user in case of owner, account and contact in case of customer) and with parallel branch in a MS Flow to solve this issue. Read more, also you can download the Flow sample to use.
First, create two temporary lookup fields on the entity that you wish to import Customer lookup data into it, to both the Account and Contact entities respectively
Within your ADF pipeline flow, you will then need to map the GUID values for your Account and Contact fields to the respective lookup fields created above. The simplest way of doing this is to have two separate columns within your source dataset – one containing Account GUID’s to map and the other, Contact.
Then, finally, you can put together a Microsoft Flow that then performs the appropriate mapping from the temporary fields to the Customer lookup field. First, define the trigger point for when your affected Entity record is created (in this case, Contact) and add on some parallel branches to check for values in either of these two temporary lookup fields
Then, if either of these conditions is hit, set up an Update record task to perform a single field update, as indicated below if the ADF Account Lookup field has data within it

Is there a way to disable the audit for stageid field on a CRM entity that's enabled for auditing?

Got a custom entity which has auditing enabled. Every time an user changes the stage on the Business process flow (BPF) on that entity, it's generating an audit log record with the change in the stageid which is irrelevant for the business and i cannot use the modifiedon field since the stage change is changing the modifiedon field data as well, which makes the data irrelevant for the business.
One idea would be to create a field called Business Data Modified On. Then, create a workflow that runs on change of the fields you consider to be business data and have the workflow set the new field to the current time.
It does not appear that Auditing can be disabled on the managed field StageId.

Check if a record has any attachment - Dynamics CRM

I need to write a plugin where I have to check if a particular record has any file attached.
When I remove any file from a record it gets soft deleted.
I check Annotation table in SQL server, but is there any flag that denotes the entry is soft deleted or not?
Dynamics CRM 2011 does not have a soft-delete. When the record is deleted in CRM there is a DELETE executed on the SQL server, resulting in the record being removed.
In an annotation, which is the entity for Note, an attachment is stored as bas64 encoded text. If you delete an attachment on a Note it results in an UPDATE to the annotation entity setting the fields related to the attachment to null.
If you want to trigger a plugin when an attachment is deleted you need to look at the both the delete and update events on annotation. If you want to know if the annotations related to a particular entity has an attachment you need to query annotation entity where objectid matches the entity and filename is not null.
For emails and appointments attachments are stored in activitymimeattachment instead of annotation.

How can i retrieve dynamic CRM online audit summary with CRM SDK?

I want to get a list of entities which are deleted recently. from audit summary view, we can see which entity is deleted. But how can I do it with CRM SDK(C# code)? Is it possible to write a query expression(with specified operation, date filter...) to query audit summary?
It is possible to retrieve the data change history using RetrieveRecordChangeHistoryRequest Class
This is a sample code:
// Retrieve the audit history for the account and display it.
RetrieveRecordChangeHistoryRequest changeRequest = new RetrieveRecordChangeHistoryRequest();
changeRequest.Target = new EntityReference(Account.EntityLogicalName, _newAccountId);
RetrieveRecordChangeHistoryResponse changeResponse =
(RetrieveRecordChangeHistoryResponse)_service.Execute(changeRequest);
AuditDetailCollection details = changeResponse.AuditDetailCollection;
Sample: Audit entity data changes is an example on how you can enable and disable auditing on an entity and its attributes, retrieve the data change history of the audited entity, and delete the audit records.

MS CRM 4 - Custom entity with "regardingobjectid" functionality

I've made a custom entity that will work as an data modification audit (any entity modified will trigger creating an instance of this entity). So far I have the plugin working fine (tracking old and new versions of properties changed).
I'd like to also keep track of what entity this is related to. At first I added a N:1 from DataHistory to Task (eg.) and I can indeed link back to the original task (via a "new_tasksid" attribute I added to DataHistory).
The problem is every entity I want to log will need a separate attribute id (and an additional entry in the form!)
Looking at how phone, task, etc utilize a "regardingobjectid", this is what I should do. Unfortunately, when I try to add a "dataobjectid" and map it to eg Task and PhoneCall, it complains (on the second save), that the reference needs to be unique. How does the CRM get around this and can I emulate it?
You could create your generic "dataobjectid" field, but make it a text field and store the guid of the object there. You would lose the native grids for looking at the audit records, and you wouldn't be able to join these entities through advanced find, fetch or query expressions, but if that's not important, then you can whip up an ASPX page that displays the audit logs for that record in whatever format you choose and avoid making new relationships for every entity you want to audit.
CRM has a special lookup type that can lookup to many entity types. That functionality isn't available to us customizers, unfortunately. Your best bet is to add each relationship that could be regarding and hide the lookups that aren't in use for this particular entity.

Resources