locked
How to Update records of a Sub-grid in CRM 2013 using OData JSON JQuery RRS feed

  • Question

  • Hello,

    Below is my Product sub grid on Quote Entity.

    Now On addition of new product , I am calculating Service tax and tax based on Price per unit, quantity and discount.

    Now i need to set calculated service tax and tax value in sub grid.


    I want to Update record of a Sub-grid in Quote Entity. I have tried below code, but still not able to update my sub grid .

    function OnRefresh() {  var gridControl = document.getElementById("quotedetailsGrid").control; var ids = gridControl.get_allRecordIds(); for (i = 0; i < ids.length; i++) { var quotedetail = new Object();

    quotedetail.new_service_tax = "1000"; var id = ids[i].replace("{", "").replace("}", "");

    QuoteProductRecord(id,quotedetail); } } function QuoteProductRecord(id,quotedetail) { var jsonEntity = window.JSON.stringify(quotedetail); var ODataPath = Xrm.Page.context.getServerUrl() + "/XrmServices/2011/OrganizationData.svc/QuoteDetailSet"; $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", datatype: "json", data: jsonEntity, url: ODataPath + "(guid'" + id + "')", beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE"); }, success: function (data, textStatus, XmlHttpRequest) { alert("Success"); }, error: function (XMLHttpRequest, textStatus, errorThrown) { } }); }

    Issue is that there is no error thrown, but still Service tax and tax value in sub grid is blank.

    In JSON function, i have also tried to alert success message , but neither i got any success message nor any error message.

    Tuesday, April 15, 2014 6:05 AM

Answers

  • Hello Prashant,

    I just tested below script to update quote value from opportunity screen and it's working fine and I am getting alert

    function OnRefresh() {  
    var gridControl = document.getElementById("opportunityQuotesGrid").control; //Change grid name here
    var ids = gridControl.get_allRecordIds();
    for (i = 0; i < ids.length; i++) {
    var quotedetail = new Object();
    quotedetail.Name = "Demo";    // change to update your field
    var id = ids[i].replace("{", "").replace("}", "");    
    QuoteProductRecord(id,quotedetail); } }

    function QuoteProductRecord(id,quotedetail)
    {
    var jsonEntity = JSON.stringify(quotedetail);
    var ODataPath = Xrm.Page.context.getServerUrl() + "/XrmServices/2011/OrganizationData.svc/QuoteSet"; //Change entity name here
    $.ajax({ type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    data: jsonEntity,
    url: ODataPath + "(guid'" + id + "')",
    beforeSend: function (XMLHttpRequest) {
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE"); },
    success: function (data, textStatus, XmlHttpRequest) { alert("Success"); },
    error: function (XMLHttpRequest, textStatus, errorThrown) { } }); }


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    • Proposed as answer by HIMBAPModerator Friday, April 18, 2014 7:22 AM
    • Edited by HIMBAPModerator Friday, April 18, 2014 7:23 AM
    • Marked as answer by PB5590 Monday, April 21, 2014 4:29 AM
    Friday, April 18, 2014 7:22 AM
    Moderator

All replies

  • Hi,

    Could you provide information where are you calling your function and when ??


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, April 15, 2014 6:24 AM
    Moderator
  • Hello Mahendar Pal,

    Thanks for your reply.

    I have bind On Refresh Event of sub grid On OnLoad event of Main Form .

    Please find below screen

    Here is Form_onload Function.

    // call this finction on form load
    function Form_onload() {
    
    // time out set as the grod will load littile late asynchronously after form laod , so we are waiting 2.5 seconds
        setTimeout("SubGridRefresh();", 2500);
    }
    
    
    // Attach  Sub grid refresh event
    function SubGridRefresh() {
    
    var grid = null;
         grid = document.getElementById("quotedetailsGrid");
    
         // Chek gris controll loaded or not 
        if (grid.readyState != "complete") {
            // delay one second and try after one second whether grid is loaded or not  
            setTimeout(SubGridRefresh, 1000);
            return;
        }
    
        if (grid) {
        // Attach  grid refresh event
            grid.attachEvent("onrefresh", OnRefresh);
    
          //cALLING THE CUSTOM LOGIC WHICH NEED TO BE EXECUTED R SUB GRID REFRESH
            grid.control.add_onRefresh(OnRefresh);
    
        }
    }
    

    Please let me know if you need anything else.

    Tuesday, April 15, 2014 6:33 AM
  • Which browser you are using ??

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, April 15, 2014 6:42 AM
    Moderator
  • Hi,

    I am using Internet Explorer 10.

    Tuesday, April 15, 2014 6:45 AM
  • Hi,

    Just try to use addEventListener instead of attachEvent


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, April 15, 2014 6:59 AM
    Moderator
  • Here in my code, Subgrid attach event is firing properly because it is calling function OnRefresh(), and there I have kept many alert messages and all the subgrid values are coming up fine.

    But the problem is with Ajax function to update the subgrid record, that is what I think.

    I have confusion about the below line,

    url: ODataPath + "(guid'" + id + "')",


    What should be the id here?

    From where exactly we are getting it ? Is it a row Guid of Subgrid? 

    As I tried to see that in Alert when I add same product, it gives me different Guids. What does that mean? 

    Is there anything which I am missing in this Ajax function? AS I have never caught the "Success" alert.


    Tuesday, April 15, 2014 8:33 AM
  • Hi,

    This should be records id, which is basically id of the entity record that you are showing in sub grid.

    Before using this function in attachevent,

    1. First check if you are getting currect guid format.

    2. Just test this function on form itself may be onchange of some field the see if your function is working correctly.


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, April 15, 2014 8:56 AM
    Moderator
  • Hi,

    I have checked GUID format, i am getting correct GUID format.

    I have also checked  OnRefresh function and it is working fine as i have kept many alert message in between function.

    Tuesday, April 15, 2014 9:33 AM
  • Did you try to debug your code and see how your request going through ?

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, April 16, 2014 4:00 AM
    Moderator
  • Hi,

    I have tried to debug all function by putting alert message. i have also put alert message on  QuoteProductRecord function and try to get value of variable " jsonEntity "," id " and " ODataPath ".

    if you could please guide me how to debug other way, then it will be really helpful.

    Wednesday, April 16, 2014 4:19 AM
  • You can write debugger keyword in your code and then use Developers tool, special check like below

    var url=ODataPath + "(guid'" + id + "')";

    and then try to browse this url if you are getting result in browser or not.

    HTH



    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, April 16, 2014 4:26 AM
    Moderator
  • Hi,

    I have tried to debug and got value of url.

    var url = "../XrmServices/2011/OrganizationData.svc/QuoteDetailSet(guid'C7F1C1D0-20C5-E311-93FB-00155D0A241E')"

    Wednesday, April 16, 2014 4:47 AM
  • Did you browse this url ?

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, April 16, 2014 6:05 AM
    Moderator
  • Yes,

    But it shows HTTP 404 Not Found error page.

    Wednesday, April 16, 2014 6:09 AM
  • That could be the issue, because you should be able to browse this url and you should see your entity record, please try to compare the url with you record id, you can get it from entity form, and also make sure you are using correct field name

    quotedetail.new_service_tax = "1000"; //I am assuming you are using schema name of the field correct


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    Wednesday, April 16, 2014 7:24 AM
    Moderator
  • I have copy URL link from entity form, and got the below URL.

    <http://.../userdefined/edit.aspx?etc=1085&id=%7bC7F1C1D0-20C5-E311-93FB-00155D0A241E%7d>

    i think that id is same only.

    Wednesday, April 16, 2014 7:38 AM
  • Are you able to debug your code and able to fix it ?? if yes could you close this thread.

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, April 18, 2014 1:02 AM
    Moderator
  • Hi,

    i have debug my code, but unable to fix it.

    Friday, April 18, 2014 4:02 AM
  • Hello Prashant,

    I just tested below script to update quote value from opportunity screen and it's working fine and I am getting alert

    function OnRefresh() {  
    var gridControl = document.getElementById("opportunityQuotesGrid").control; //Change grid name here
    var ids = gridControl.get_allRecordIds();
    for (i = 0; i < ids.length; i++) {
    var quotedetail = new Object();
    quotedetail.Name = "Demo";    // change to update your field
    var id = ids[i].replace("{", "").replace("}", "");    
    QuoteProductRecord(id,quotedetail); } }

    function QuoteProductRecord(id,quotedetail)
    {
    var jsonEntity = JSON.stringify(quotedetail);
    var ODataPath = Xrm.Page.context.getServerUrl() + "/XrmServices/2011/OrganizationData.svc/QuoteSet"; //Change entity name here
    $.ajax({ type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    data: jsonEntity,
    url: ODataPath + "(guid'" + id + "')",
    beforeSend: function (XMLHttpRequest) {
    XMLHttpRequest.setRequestHeader("Accept", "application/json");
    XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE"); },
    success: function (data, textStatus, XmlHttpRequest) { alert("Success"); },
    error: function (XMLHttpRequest, textStatus, errorThrown) { } }); }


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.


    • Proposed as answer by HIMBAPModerator Friday, April 18, 2014 7:22 AM
    • Edited by HIMBAPModerator Friday, April 18, 2014 7:23 AM
    • Marked as answer by PB5590 Monday, April 21, 2014 4:29 AM
    Friday, April 18, 2014 7:22 AM
    Moderator
  • Hi,

    i have uploaded my solution in my production environment, which is CRM Online.

    and guess what , it worked perfectly. 

    Monday, April 21, 2014 4:30 AM
  • Glad to hear it's working for you finally !!

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, April 21, 2014 4:48 AM
    Moderator