Tuesday, March 5, 2013


CRM 2011 Plugins: Welcome to transactions !

One of the thing about CRM 4.0 plugins is the inability to automatically rollback actions when an exception occurs during a business process.

To illustrate this, imagine a plugin which creates a contract, then a line of contract. If the code throws an exception during the creation of the contract line, you need to take care yourself of deleting the contract that have been created: of course, you need the contract AND the contract line, not just the contract.

In CRM 2011, you can now register your plugin in new stage (see below). That means that you can register your plugin in a transaction (if using stage 20 and 40) and whenever you plugin throws an exception, every action done during the transaction will be rollback. In our previous example, that means tha the contract will be deleted automatically by the application.

Event
Stage Name
Stage Number
Description
Pre-Event
Pre-validation
10
Stage in the pipeline for plug-ins that are to execute before the main system operation. Plug-ins registered in this stage may execute outside the database transaction.
Pre-Event
Pre-operation
20
Stage in the pipeline for plug-ins that are to execute before the main system operation. Plug-ins registered in this stage are executed within the database transaction.
Post-Event
Post-operation
40
Stage in the pipeline for plug-ins which are to execute after the main operation. Plug-ins registered in this stage are executed within the database transaction.
Post-Event
Post-operation
50
Stage in the pipeline for plug-ins which are to execute after the main operation. Plug-ins registered in this stage may execute outside the database transaction. This stage only supports Microsoft Dynamics CRM 4.0 based plug-ins

 

 

Transactional features in Crm 2011

One of the very handy features in Crm 2011 is the fact that plugins now execute within the transaction, not outside it anymore. This means that if an error occurs within a plugin that fires on Create of a record, the actions done by the code gets rolled back AND the actual record created to trigger this event is also rolled back. So no more duplicate records if a plugin fails and the user, thinking that the record did not create, resaves the record.
 
For more information on the pipelines in Crm 2011, see here.

Sunday, October 28, 2012

Binding html editable Table to Rest Service result



Binding html editable Table to Rest Service result

<html>
<head>
    <title>Dynamic Tables in HTML</title>
    <script type="text/javascript" src="Scripts/SDK.REST"></script>
    <script type="text/javascript" src="../ClientGlobalContext.js.aspx"></script>
    <script type="text/javascript" src="Scripts/json2"></script>
    <script type="text/javascript">
        var recordsGrid;
        document.onreadystatechange = function () {
            if (document.readyState == "complete") {
                recordsGrid = document.getElementById("recordsGrid");
                retrieveRecords();
            }
        };

        function retrieveRecords() {
            debugger;
            SDK.REST.retrieveMultipleRecords("new_businesstier", "$select=new_name,new_businesstierId,new_MinValue,new_Percent", results, errorHandler, oncomplete)
        }
        function results(result) {
            debugger;
            for (var i = 0; i < result.length; i++) {
                var aResult = result[i];
                var row = document.createElement("tr");
                var cell1 = document.createElement("td");
                // setElementText(cell1, (aResult.hpcrm_name == null) ? "" : aResult.hpcrm_name);
                cell1.innerText = aResult.hpcrm_name;
                row.appendChild(cell1);
                var cell2 = document.createElement("td");
                //setElementText(cell2, (aResult.hpcrm_Percent == null) ? "" : aResult.hpcrm_Percent);
                //cell2.innerText = aResult.hpcrm_Percent;
                cell2.innerHTML = "<INPUT onchange=\"javascript:changeContent('cell2" + i + "');\" id=\"cell2" + i + "\" type=text value=\"" + aResult.hpcrm_Percent + "\">";
                //cell2.firstChild.focus();
                //alert(cell2.innerText);
                row.appendChild(cell2);
                recordsGrid.appendChild(row);
            }
        }
        function oncomplete() {
            //OnComplete handler
        }
        function changeContent(tablecell) {
            debugger;
   var cell = document.getElementById(tablecell);
            alert(cell.value);
        }
        function submitNewName(textfield) {
            alert(textfield.value);
            textfield.parentNode.innerHTML = textfield.value;
        }
        function errorHandler(error) {
            writeMessage(error.message);
        }


        function changeimg() {
       
            alert("hi");
        }

    </script>
</head>
<body>
    <div align="center">
        <select id="listimg" onchange="changeimg()">
            <option value="image1">Percent</option>
            <option value="image2">Number</option>
        </select>
    </div>
    <div id="tableContainer">
        <table id="recordsTable" rules="groups" summary="This table displays the records retrieved.">
            <tbody id="recordsGrid" />
        </table>
    </div>
</body>
</html>
 

Saturday, October 27, 2012

An item with the same key has already been added

If  you get Error  -  An item with the same key has already been added


This can often happen if you are explicity adding an attribute value to the target entity in you plugin where it already has been added.
Rather than entity.Attributes.Add(...)
use entity["attributename"] = ...