Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011
Showing posts with label MS Dynamics. Show all posts
Showing posts with label MS Dynamics. Show all posts

Wednesday, December 30, 2020

How to find and debug your custom Javascript files in Dynamics CRM / 365

In this article we describe Step by step How to find and debug your custom Javascript files in Dynamics CRM / 365 in 5 minutes.
After you have coded you javascript files, you want to find them for debugging purposes. It could be a not easy task to find them between the hundreds of source files that Dynamics 365 will load to the browser:




How to find and debug your custom Javascript files in Dynamics CRM / 365

To debug quickly your javascript files, just follow this steps:

1) Open the FORM of the Entity your are working on, and go to the Properties of the Form. Open the Web Resources and find the JAVASCRIPT LIBRARY that you want to debug. Double-click it to open the editor:



2) Once opened the Editor, type into your  javascript file , the "debugger; " command:



3) Save it, save the Form, Publish it and open the Developer's Tools of the browser:


4) Then , just perform the Click or anything else that would trigger your javascript code: from here, you can add breakpoints to stop the script execution.
That's all...
In this article we've seen Step by step How to find and debug your custom Javascript files in Dynamics CRM / 365 in 5 minutes.
Enjoy Microsoft Dynamics 365 CRM!

by Carmel Schvartzman

כתב: כרמל שוורצמן










    Tuesday, April 16, 2019

    Dynamics 365 - How to Create a new Entity record using Web API and Javascript in 5 minutes

    In this article we describe Step by step  How to Create a new Entity record  using Web API and Javascript in 5 minutes. We'll use the JQuery already loaded by Dynamics 365 on the form.

    For this example, we'll create a Contact record, when clicking a button, using a Web Resource locally inside the Form.


    How to Create a new Entity record  using Web API and Javascript in 5 minutes



    This is the function to Create a Record , using an HTTP POST request: 


    var entity = {};
    entity.address1_telephone1 = "";
    entity.emailaddress1 = "";
    entity.lastname = "";

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts",
        data: JSON.stringify(entity),
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
            XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        async: true,
        success: function(data, textStatus, xhr) {
            var uri = xhr.getResponseHeader("OData-EntityId");
            var regExp = /\(([^)]+)\)/;
            var matches = regExp.exec(uri);
            var newEntityId = matches[1];
        },
        error: function(xhr, textStatus, errorThrown) {
            Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
        }
    });



    And this is the Web Resource EMBEDDED in the Form (that's why we wrote "parent" before calls to JQuery ) :
    1) Open the Form
    2) Add > Section > Web Resource
    3) Paste inside it the following HTML - JS code:



    <html><head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <script>

    var fnCreateContact = () => {
    var entity = {};
    entity.address1_telephone1 = "123123123";
    entity.emailaddress1 = "www.bender.com";
    entity.lastname = "Contact Name";

    parent.$.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts",
        data: JSON.stringify(entity),
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
            XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        async: true,
        success: function(data, textStatus, xhr) {
            var uri = xhr.getResponseHeader("OData-EntityId");
            var regExp = /\(([^)]+)\)/;
            var matches = regExp.exec(uri);
            var newEntityId = matches[1];

    document.getElementById('btnCreateContact').classList.add('disabled');
        },
        error: function(xhr, textStatus, errorThrown) {
            parent.Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
        }
    });
    }
    </script>
    <meta></head><body style="direction: rtl; overflow-wrap: break-word;">

    <div class="container">
      <div class="jumbotron">
        <h3>Create Contact</h3>   
        <p>This function creates a new Contact</p>
         <button id="btnCreateContact" onclick="fnCreateContact()" type="button" class="btn btn-primary">Create Contact</button>
      </div>
    </div>

    </body></html>






    That's all...
    In this article we've seen Step by step  How to Create a new Entity record  using Web API and Javascript in 5 minutes.
    Enjoy Microsoft Dynamics 365 CRM!

    by Carmel Schvartzman

    כתב: כרמל שוורצמן

    Sunday, August 31, 2014

    PlugIns - How to SAFELY COPY SOURCE VALUES TO TARGET FIELDS in a CRM 2013 C# Workflow

    by Carmel Schvartzman
    1. This is the Building Block C# code to SAFELY COPY SOURCE VALUES TO TARGET FIELDS in a CRM 2013 C# Workflow. When using the late binding approach , you would probably be faced with web services errors related to types mismatch . Usually , you would need to waste much time looking for the specific field which caused the error . The goal of this tutorial is to avoid falling into that kind of problems when developing CRM plugins with the late binding approach . 
    2. The present code relates to a custom  Plugin / Workflow in Dynamics CRM 2011/2013. Once you have developed your custom workflow, you deploy the assembly containing the plugin in the CRM web server, and register it with the Plugin Registration Tool of the CRM 2011 SDK.
    3. After you set up the Xrm Organization Service that you need to interact with the CRM organization, you can use the following code to COPY values fetched through the CRM Web Service, which we call here "sourceEntity",  to your local "targetEntity" . We include here 4 scenarios : an INT field, STRING, DECIMAL & DATETIME : each one of them will be copied SAFELY, meaning that because we are using late binding here, we will avoid causing web service exceptions :

    SAFELY COPY SOURCE VALUES TO TARGET FIELDS IN PLUGIN WORKFLOW

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
    ///////////////////////  CASE #1 ) STRING :  ////////////////////////////////////////
    if (sourceEntity.Attributes.ContainsKey("source_field"))               
         {                    
               targetEntity["target_field"] =  Convert.ToString(sourceEntity.Attributes["source_field"]);               
          }





    //////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////  CASE #2 ) INT :
    /////////////////////////////////

    if (sourceEntity.Attributes.ContainsKey("source_field") )            
    {                
       int iCode = 0;                
       if (Int32.TryParse(Convert.ToString(sourceEntity.Attributes["source_field"] ), out iCode))                
       {                   
           targetEntity.Attributes["target_field"] = iCode;                
       }            
    }




    //////////////////////////////////////////////////////////////////////////////////////////////////             
     /////////////////  CASE # 3 ) MONEY - DECIMAL : //////////////////////
    if (sourceEntity.Attributes.ContainsKey("source_field") )            
    {                
       decimal mValue = 0.0M;               
        if ( Decimal.TryParse(Convert.ToString(sourceEntity.Attributes["source_field"] ), out mValue ))               
        {                    
             targetEntity.Attributes["target_field"] = mValue;                
         }            
    }




    //////////////////////////////////////////////////////////////////////////////////////////////////                      
    //////////  CASE #4 ) DATETIME : /////////////////////////////////
    if (sourceEntity.Attributes.ContainsKey("source_field") )           
    {               
             decimal mValue = 0.0M;               
             if ( Decimal.TryParse(Convert.ToString(sourceEntity.Attributes["source_field"]), out mValue ))               
           {                   
                targetEntity.Attributes["target_field"] = Convert.ToDateTime( sourceEntity.Attributes["source_field"]);               
            }           
    }


    ////////////////////////////////////////////////////////////////////////////////////////////////





    The present article is about how to SAFELY COPY SOURCE VALUES TO TARGET FIELDS in a CRM 2013 C# Workflow.
    That's all...Enjoy Dynamics CRM 2013!!!


    כתב: כרמל שוורצמן