2
votes

I am new to CRM 2011. I created a javascript function in the Common.js file. This function will be called in other functions from many forms. Also this function contains FetchXML queries. In each form I created a specific function that contains a call to my generic function.I call this specific function on the OnSave event of a form. The issue is, when I call the specific function on the save, in the generic function I'll do a FetchXML query of the entity I just saved, BUT the FetchXML query returns the values before the saving has happened (The entity I just saved which called the function that has the FetchXML query, I can't see the changed attributes with their new values in the result of the FetchXML query!). I tried another solution, instead of calling the specific function from the OnSave Dialogue, I used the method Xrm.Page.data.entity.addOnSave(My specific function) to be sure that it will be called after the form is saved but it's still fetching the old values. Could you please help me out?

Thanks, Walloud

2
What is the use case requiring you to retrieve the previous values of an updated entity? As stated by Guide Preite below what you want to accomplish is not readily available but there are complex workarounds that could achieve your goals - they would require significant coding and a thorough understanding of CRM development, to read audit entree so you can retrieve the last update changes for an entity.Nicknow
If you want values from the current record, don't use FetchXML - get them straight off the form during the onSave event. If you want data from related records, it should not matter whether this runs before or after the save, since that data is coming from the database, unrelated to changes made to the current record.AdamV
Sorry guys, I think I didn't explain very well my request. I didn't say that I need the values before the update, it's the opposite. Think about a form which a field was changed, when I save I call a function in Common.js that Fetch many entites included the updated entity, but the result of the fetch doesn't contain the new value of the field :(Walloud

2 Answers

3
votes

Xrm.Page.data.entity.addOnSave will add a function inside the OnSave event, this means will still be executed before the record is saved.

I suggest to move the logic inside a plugin.

0
votes

There are two possibilities:

  1. I think you should use a form onload event and check if record is created for the first type by Xrm.Page.ui.getFormType()=1 the do not FetchXMl bceause new record is going to be created.

  2. if Xrm.Page.ui.getFormType()=2 then FetchXml

    Or you can use

     setInterval  (function() FetchXML},3000);