Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Refresh When Xrm.WebApi.updateRecord finishing Execution

Posted on by 361

Hello

i am trying to refresh a sub-grid when my script finishing updating relating records  but the problem is my sub-grid refresh before all updates finish i have to refresh my sub-grid manually multiple times to see all my updates is there any way i can be sure that Xrm.WebApi.updateRecord finished executing in the server side, i know there is success call back function but it doesn't fulfill my need because it triggers after the request is finished not the execution in my server side then i came with another solution to delay my script using settimeout function but i don't think is consistent sometimes work and other don't here is an example from my code

function updateProducts(context){
	
	var formContext = context.getFormContext();
        var isDirty = formContext.data.entity.getIsDirty();
	if(!isDirty){
		Xrm.Utility.showProgressIndicator("Please wait...");
		var result;
		var myCollection = [];
		var entityId = Xrm.Page.data.entity.getId();
		entityId = entityId.replace("}", "").replace("{", "")
		var lookupMetier = Xrm.Page.getAttribute("tool_metier").getValue();
		var lookupPartenaire = Xrm.Page.getAttribute("msa_partnerid").getValue();
		var nameMetier ;
		var guidMetier ; 
		var entTypeMetier;
		var namePartenaire ;
		var guidPartenaire;
		var entTypePartenaire;
		if (lookupMetier != null)
		{
			nameMetier = lookupMetier[0].name;
			guidMetier = lookupMetier[0].id;
			guidMetier = guidMetier.replace("}", "").replace("{", "")
			entTypeMetier = lookupMetier[0].entityType;
		}
		if (lookupPartenaire != null)
		{
			namePartenaire = lookupPartenaire[0].name;
			guidPartenaire = lookupPartenaire[0].id;
			guidPartenaire = guidPartenaire.replace("}", "").replace("{", "")
			entTypePartenaire = lookupPartenaire[0].entityType;
		}

			parent.Xrm.WebApi.retrieveMultipleRecords("opportunityproduct", "?$select=opportunityproductid,tool_prix1&$filter=_tool_metier_value eq "+guidMetier+" and  _tool_partenaire_value eq "+guidPartenaire+" and  _opportunityid_value eq "+entityId+"").then(
		function success(result) {
			for (var i = 0; i < result.entities.length; i++) {
				var tauxDeMarge = Xrm.Page.getAttribute("tool_tauxdemarge").getValue();
				var tauxDevise = Xrm.Page.getAttribute("tool_tauxdevise").getValue();
				var fraisDapproche = Xrm.Page.getAttribute("tool_fraisdapproche").getValue();
				var prixDachat = result.entities[i].tool_prix1;
				var remise = Xrm.Page.getAttribute("tool_remise").getValue();

				var var0 =  remise/100;
				var var1 = 1-var0;
				var var2 = 1+tauxDeMarge;
				var var3 = (prixDachat*var1*var2)+prixDachat;
				var data =
				{
						"tool_fraisdapproche": fraisDapproche,
						"tool_tauxdemarge": tauxDeMarge,
						"tool_tauxdevise": tauxDevise,
						"tool_remise": remise,
						"tool_prixdevente": var3,
						
				}
				Xrm.WebApi.updateRecord("opportunityproduct", result.entities[i].opportunityproductid, data)
			}
		},
		function (error) {
			console.log(error.message);
			// handle error conditions
		}
	);

			setTimeout(function(){
				Xrm.Page.getControl("opportunityproductsGrid").refresh();
			},10000);
			Xrm.Utility.closeProgressIndicator();
	}
	else
	{
	var alertStrings = {confirmButtonLabel: "Save", text: "Please save before Update your products !" };
        var alertOptions = { height: 190, width: 360 };
        Xrm.Navigation.openAlertDialog(alertStrings,alertOptions).then(saveForm,errorCallback);
	}


	// Xrm.Utility.openWebResource("new_simple_modal", null, 250, 250);    
	
	/* var DialogOption = new Xrm.DialogOptions;
		DialogOption.width = 500;
		DialogOption.height = 620;    
		window.parent.Xrm.Internal.openDialog("/WebResources/tool_modalDialog", DialogOption, null, null, null);*/
}
function saveForm(){
    Xrm.Page.data.save();
}

*This post is locked for comments

  • Anas Rafik Profile Picture
    Anas Rafik 361 on at
    RE: Refresh When Xrm.WebApi.updateRecord finishing Execution

    this the translation of the error : Invalid check the server could not process the request because the syntax is not valid

  • Verified answer
    Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: Refresh When Xrm.WebApi.updateRecord finishing Execution

    Try this arrow function, if its working for you. This will make sure when the execution of promise returned by CRM web api call is successful.

    Xrm.WebApi.updateRecord("opportunityproduct", result.entities[i].opportunityproductid, data).then(

     result => {

    Xrm.Page.getControl("opportunityproductsGrid").refresh();

    },

    function (error) {

     console.log(error.message);

     // handle error conditions

    }

    );

  • gdas Profile Picture
    gdas 50,085 on at
    RE: Refresh When Xrm.WebApi.updateRecord finishing Execution

    Please translate the error message in English and post here.

  • Anas Rafik Profile Picture
    Anas Rafik 361 on at
    RE: Refresh When Xrm.WebApi.updateRecord finishing Execution

    i spent two hours trying to execute your example of code but it doesn't work it gives this error

    qsdfqsdfqzef.PNG

  • Rajesh Chungath Profile Picture
    Rajesh Chungath 465 on at
    RE: Refresh When Xrm.WebApi.updateRecord finishing Execution

    Hi

    Take total record count to a variable. You can do it on parent.Xrm.WebApi.retrieveMultipleRecords(..) success method then add a success block to  Xrm.WebApi.updateRecord("opportunityproduct", result.entities[i].opportunityproductid, data). In Xrm.WebApi.updateRecord method success block validate the current "i" value and total record count. If both are equal you can refresh the grid.  

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Refresh When Xrm.WebApi.updateRecord finishing Execution

    Hi,

    1. Have you tried creating your own synchronous update request with the web api ? 

    2. If you are updating multiple records, have you tried creating a batch request and refreshing the grid after that

    // Maybe it could be something like this
    // I have not tested this at all, just thought it could possibly help in your case
    function success(result) { var data = []; data.push('--batch_123456'); data.push('Content-Type: multipart/mixed;boundary=changeset_BBB456'); data.push(''); for (var i = 0; i < results.entities.length; i++) { data.push('--changeset_BBB456'); data.push('Content-Type:application/http'); data.push('Content-Transfer-Encoding:binary'); var id = i + 1; data.push('Content-ID:' + id); data.push(''); data.push('PATCH ' + Xrm.Page.context.getClientUrl() + WEB_API_URL + 'opportunityproducts(' + result.entities[i].opportunityproductid + ') HTTP/1.1'); data.push('Content-Type:application/json;type=entry'); data.push(''); var tauxDeMarge = Xrm.Page.getAttribute("tool_tauxdemarge").getValue(); var tauxDevise = Xrm.Page.getAttribute("tool_tauxdevise").getValue(); var fraisDapproche = Xrm.Page.getAttribute("tool_fraisdapproche").getValue(); var prixDachat = result.entities[i].tool_prix1; var remise = Xrm.Page.getAttribute("tool_remise").getValue(); var var0 = remise/100; var var1 = 1-var0; var var2 = 1+tauxDeMarge; var var3 = (prixDachat*var1*var2)+prixDachat; var data = { "tool_fraisdapproche": fraisDapproche, "tool_tauxdemarge": tauxDeMarge, "tool_tauxdevise": tauxDevise, "tool_remise": remise, "tool_prixdevente": var3 }; data.push(data); } data.push('--changeset_BBB456--'); data.push('--batch_123456--'); var payload = data.join('\r\n'); $.ajax( { method: 'PATCH', url: Xrm.Page.context.getClientUrl() + WEB_API_URL + '$batch', headers: { 'Content-Type': 'multipart/mixed;boundary=batch_123456', 'Accept': 'application/json', 'Odata-MaxVersion': '4.0', 'Odata-Version': '4.0' }, data: payload, async: false, success: function (data, textStatus, XmlHttpRequest) { Xrm.Page.getControl("opportunityproductsGrid").refresh(); }, error: function (xhr, textStatus, errorThrown) { //Handle error conditions } }); }

    Hope this can help you somehow

  • Anas Rafik Profile Picture
    Anas Rafik 361 on at
    RE: Refresh When Xrm.WebApi.updateRecord finishing Execution

    i already tested this solution but it refresh before updates finishes execution on the server side

  • Suggested answer
    Daniel Wikell Profile Picture
    Daniel Wikell 2,360 on at
    RE: Refresh When Xrm.WebApi.updateRecord finishing Execution

    Hi

    Instead of trying with timeouts (which will be wrong a lot of the times since the total time taken for your requests might vary) you should add a success method to the call Xrm.WebApi.updateRecord. At the final loop in your for-loop, this success method can then call the refesh method. At that Point you know that all updates are finished.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans