web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Refresh When Xrm.WebApi.updateRecord finishing Execution

(0) ShareShare
ReportReport
Posted on by 365

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

I have the same question (0)
  • Suggested answer
    Daniel Wikell Profile Picture
    2,360 on at

    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.

  • Anas Rafik Profile Picture
    365 on at

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

  • Community Member Profile Picture
    on at

    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

  • Rajesh Chungath Profile Picture
    467 on at

    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.  

  • Anas Rafik Profile Picture
    365 on at

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

    qsdfqsdfqzef.PNG

  • gdas Profile Picture
    50,091 Moderator on at

    Please translate the error message in English and post here.

  • Verified answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at

    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

    }

    );

  • Anas Rafik Profile Picture
    365 on at

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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans