locked
Enable Auditing using Jscript RRS feed

  • Question

  • Hi All,

    Is it possible to enable the auditing of an attribute using JScript? If yes, can you please provide some code snippet for it.

    Tuesday, November 15, 2011 8:06 AM

Answers

  • Hi Pradips,

    It is possible, please find the reference link which given below.

    http://mileyja.blogspot.com/2011/08/retrieve-attribute-history-and-audit.html

     


    Thanks & Regards, MS CRM Consultant, V.Surya.
    Tuesday, November 15, 2011 10:04 AM
    Answerer
  • First of all you need an entity to hold the audit records. For crm 4 you need to add this function on load of form. Please change attributes names according to your envoirnment:-

    //ONLOAD
    //This method will take two parameters:
    //1. The field name
    //2. The field data
    UpdateAudit = function(fieldName, fieldData)
    {
    //get the values from the form
    var trn = crmForm.all.new_trn.DataValue;
    var now = crmForm.all.birthdate.DataValue;
    var M = now.getMonth()+1;
    if ((M>1) && (M<9))
    {
    M="0"+M;
    }
    //alert(M);
    
    var d = now.getDate();
    var y = now.getFullYear();
    var H = now.getHours();
    var m = now.getMinutes();
    //var DOB = d+"/"+M+"/"+y + " " +H+":"+m;
    var DOB = y+"/"+M+"/"+d;
    //alert(DOB);
    var mby = crmForm.all.modifiedby.DataValue[0].id;
    var cid= crmForm.ObjectId;
    //create a new record in the Contact Audit Entity
    var authenticationHeader = GenerateAuthenticationHeader();
    //alert(1);
    // Prepare the SOAP message.
    var xml = "<?xml version='1.0' encoding='utf-8'?>" + 
    "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+
    " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
    " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ 
    authenticationHeader+
    "<soap:Body>"+ 
    "<Create xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ 
    "<entity xsi:type='new_contactaudit'>"+ 
    "<new_trn>"+trn+"</new_trn>"+ 
    "<new_teacherid>"+cid+"</new_teacherid>"+ 
    "<new_newvalue>"+fieldData+"</new_newvalue>"+ 
    "<new_attributename>"+fieldName+"</new_attributename>"+ 
    "<new_dateofbirth>"+DOB+"</new_dateofbirth>"+ 
    "<modifiedby>"+mby+"</modifiedby>"+
    "</entity>"+ 
    "</Create>"+ 
    "</soap:Body>"+ 
    "</soap:Envelope>";
    // Prepare the xmlHttpObject and send the request.
    
    //alert(xml);
    
    var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
    xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Create");
    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xHReq.setRequestHeader("Content-Length", xml.length);
    xHReq.send(xml);
    // Capture the result
    var resultXml = xHReq.responseXML;
    
    //alert(resultXml);
    
    
    // Check for errors.
    var errorCount = resultXml.selectNodes('//error').length;
    if (errorCount != 0)
    {
     var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
     alert(msg);
    }
    
    This function will create a record in audit entity. Now put the following code on change of the field you want to audit. You can call this function on change of multiple fields if you want to audit more fields:-
    
    <pre lang="x-js">ONCHANGE OF LAST NAME
    if (crmForm.FormType == 2)
    {
    UpdateAudit("Contact's Last Name", crmForm.all.lastname.DataValue);
    }
    

     


    Regards Faisal
    Tuesday, November 15, 2011 10:05 AM
  • Have a look at this section in the SDK for messages for enabling auditing for attributes.
    Tuesday, November 15, 2011 10:56 AM

All replies

  • Hi Pradips,

    It is possible, please find the reference link which given below.

    http://mileyja.blogspot.com/2011/08/retrieve-attribute-history-and-audit.html

     


    Thanks & Regards, MS CRM Consultant, V.Surya.
    Tuesday, November 15, 2011 10:04 AM
    Answerer
  • First of all you need an entity to hold the audit records. For crm 4 you need to add this function on load of form. Please change attributes names according to your envoirnment:-

    //ONLOAD
    //This method will take two parameters:
    //1. The field name
    //2. The field data
    UpdateAudit = function(fieldName, fieldData)
    {
    //get the values from the form
    var trn = crmForm.all.new_trn.DataValue;
    var now = crmForm.all.birthdate.DataValue;
    var M = now.getMonth()+1;
    if ((M>1) && (M<9))
    {
    M="0"+M;
    }
    //alert(M);
    
    var d = now.getDate();
    var y = now.getFullYear();
    var H = now.getHours();
    var m = now.getMinutes();
    //var DOB = d+"/"+M+"/"+y + " " +H+":"+m;
    var DOB = y+"/"+M+"/"+d;
    //alert(DOB);
    var mby = crmForm.all.modifiedby.DataValue[0].id;
    var cid= crmForm.ObjectId;
    //create a new record in the Contact Audit Entity
    var authenticationHeader = GenerateAuthenticationHeader();
    //alert(1);
    // Prepare the SOAP message.
    var xml = "<?xml version='1.0' encoding='utf-8'?>" + 
    "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+
    " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
    " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ 
    authenticationHeader+
    "<soap:Body>"+ 
    "<Create xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ 
    "<entity xsi:type='new_contactaudit'>"+ 
    "<new_trn>"+trn+"</new_trn>"+ 
    "<new_teacherid>"+cid+"</new_teacherid>"+ 
    "<new_newvalue>"+fieldData+"</new_newvalue>"+ 
    "<new_attributename>"+fieldName+"</new_attributename>"+ 
    "<new_dateofbirth>"+DOB+"</new_dateofbirth>"+ 
    "<modifiedby>"+mby+"</modifiedby>"+
    "</entity>"+ 
    "</Create>"+ 
    "</soap:Body>"+ 
    "</soap:Envelope>";
    // Prepare the xmlHttpObject and send the request.
    
    //alert(xml);
    
    var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
    xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Create");
    xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xHReq.setRequestHeader("Content-Length", xml.length);
    xHReq.send(xml);
    // Capture the result
    var resultXml = xHReq.responseXML;
    
    //alert(resultXml);
    
    
    // Check for errors.
    var errorCount = resultXml.selectNodes('//error').length;
    if (errorCount != 0)
    {
     var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
     alert(msg);
    }
    
    This function will create a record in audit entity. Now put the following code on change of the field you want to audit. You can call this function on change of multiple fields if you want to audit more fields:-
    
    <pre lang="x-js">ONCHANGE OF LAST NAME
    if (crmForm.FormType == 2)
    {
    UpdateAudit("Contact's Last Name", crmForm.all.lastname.DataValue);
    }
    

     


    Regards Faisal
    Tuesday, November 15, 2011 10:05 AM
  • Have a look at this section in the SDK for messages for enabling auditing for attributes.
    Tuesday, November 15, 2011 10:56 AM
  • Hi All,

    Thanks for your reply. One thing I have missed is that we are using CRM2011. I know we can achieve it using the SDKs but my requirement is to have this functionality using JScript where we can enable the auditing property of an attribute depending upon a condition.

    Please advise.

    Wednesday, November 16, 2011 8:53 AM