Answered by:
Retrieve Message Problem in plugin

Question
-
Hi guys,
I have used Retrieve message in my audit log plugin to log Entity Name, UserFullName, EntityPrimary Attribute Name & Its Value.
Its working fine bt the problem is that it logs the record three times.
I'm not getting why is it so.
If context.MessageName.ToLower = "retrieve" AndAlso context.InputParameters.Contains("Target") Then objMoniker = New Moniker objMoniker = CType(context.InputParameters("Target"), Moniker) strEntityLogicalName = objMoniker.Name If strEntityLogicalName <> "new_auditlog" AndAlso (Not strEntityLogicalName.StartsWith("sw_")) AndAlso strEntityLogicalName <> "savedquery" Then objSystemUser = GetUserGlogon(context.UserId, objCrmService) If objSystemUser IsNot Nothing AndAlso objSystemUser.domainname <> String.Empty Then 'Get MetaDataService for Entity ObjRetrieveResponse = GetMetaDataOfEntity(context, strEntityLogicalName) objAuditLog = New CrmSdk.new_auditlog strUpdatedAttributesDetails = New StringBuilder strEntityDisplayName = ObjRetrieveResponse.EntityMetadata.DisplayName.UserLocLabel.Label strPrimaryAttribute = ObjRetrieveResponse.EntityMetadata.PrimaryField 'Retrieve the value of Primary Attribute objAuditLog.new_primaryattribute = GetPrimaryAttributeValue(objMoniker.id, objMoniker.name, strPrimaryAttribute, objCrmService) 'Fill First Two Rows strAttributeDetails = "Record: " & objAuditLog.new_primaryattribute.ToString _ & ", Entity: " & strEntityDisplayName & " is Viewed by : " & objSystemUser.fullname strUpdatedAttributesDetails.Append(strAttributeDetails).AppendLine() 'Fill the AuditLog Entity details objAuditLog.new_attributesupdated = strUpdatedAttributesDetails.ToString 'objAuditLog.new_requestedentityid = objMoniker.Id.ToString objAuditLog.new_userglogon = objSystemUser.domainname objAuditLog.new_userid = New CrmSdk.Lookup objAuditLog.new_userid.Value = context.UserId objAuditLog.new_userid.type = CrmSdk.EntityName.systemuser.ToString objAuditLog.new_eventname = "Read " & strEntityDisplayName objAuditLog.new_timestamp = New CrmSdk.CrmDateTime objAuditLog.new_timestamp.Value = context.CorrelationUpdatedTime.Value.ToString 'Create audit log CreateAuditLog(objAuditLog, objCrmService) End If End If End If
Dare to promiseFriday, August 7, 2009 1:09 PM
Answers
-
check if all three times context values in debug mode..if all context values ( like user, primary entity , message name) comes as same then it means then retrive method is getting fired three times ..
is it possible to retrive last audit log entry ...and then compare the value with current log..if both are same...it means this is same entry do not create new log......this way you can eliminate duplicate entries...- Proposed as answer by Mayank Pujara Tuesday, August 11, 2009 5:53 PM
- Marked as answer by DavidJennawayMVP, Moderator Friday, August 21, 2009 8:43 AM
Tuesday, August 11, 2009 8:28 AM
All replies
-
Hi.
Could you paste CreateAuditLog method here?
Truth is opened the prepared mind My blog - http://a33ik.blogspot.comFriday, August 7, 2009 3:59 PMModerator -
You might want to add some additional loggin logic to track different places in your code. Also, double check yyour plugin registration to ensure that you haven't somehow ended up with mutliple registrations (though that shouldn't have happened).
Matt, MVP - Dynamics CRMFriday, August 7, 2009 11:45 PMModerator -
register dynamic plug in as disk type and have pdb file in server/assembly/bin and then attached the w3p process in visual studio.. put the break point on first line..and keep on debuggin ..this will help you crack the problem.....
Saturday, August 8, 2009 12:53 PM -
Hi Matt,
Sorry, I couldnt get what exactly the login Login means :-(
I have debug the code by attaching it asyncronous service.. there also its coming three times. I'm also using stunnware in my pages.
Its working fine for update , Create and Delete, BUt for retrieve it going thrice.
Also for few entities the primary attribute come null sometime. I dont why this suspicous bevahiour is.
This is my Creating audit log code.
Public Sub CreateAuditLog(ByVal AuditLog As CrmSdk.new_auditlog, ByVal objCrmService As CrmSdk.CrmService) Dim objCreateRequest As CrmSdk.CreateRequest Dim objCreateResponse As CrmSdk.CreateResponse Dim objTargetCreate As CrmSdk.TargetCreateNew_auditlog 'Initiate The Objects objTargetCreate = New CrmSdk.TargetCreateNew_auditlog objCreateRequest = New CrmSdk.CreateRequest objCreateResponse = New CrmSdk.CreateResponse 'Create audit log objTargetCreate.New_auditlog = AuditLog objCreateRequest.Target = objTargetCreate objCreateResponse = CType(objCrmService.Execute(objCreateRequest), CrmSdk.CreateResponse) End Sub
Dare to promiseMonday, August 10, 2009 5:20 AM -
check if all three times context values in debug mode..if all context values ( like user, primary entity , message name) comes as same then it means then retrive method is getting fired three times ..
is it possible to retrive last audit log entry ...and then compare the value with current log..if both are same...it means this is same entry do not create new log......this way you can eliminate duplicate entries...- Proposed as answer by Mayank Pujara Tuesday, August 11, 2009 5:53 PM
- Marked as answer by DavidJennawayMVP, Moderator Friday, August 21, 2009 8:43 AM
Tuesday, August 11, 2009 8:28 AM