Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Javascript Xrm.Page.data.entity.save() ignored?

(0) ShareShare
ReportReport
Posted on by 25

Hi Guys, 

I have a simple JavaScript running in the Task entity.

When a user  enters data into two separate fields, the script create a subtask record (custom entity w/related-id field)  

Since in certain cases the user might enter data when they create the task (on create). i don't have the ID to create the related field.

I figured if am in create mode I could save the entity (Xrm.Page.data.entity.save() ) before creating my related record, that way i would be able to read the id. 

but the JavaScript appears to ignore the save command? 

is this normal behaviour?  

if i put code in the "on_Save" form event will i be able to read the id? 

  • whydoihavetocreateanotherlogin Profile Picture
    25 on at
    RE: Javascript Xrm.Page.data.entity.save() ignored?

    not Really,

    I don't know why I need to call the function without () in order for the get.id to work (in my subfunction)

    it make no sence to me, but i'm sure there is a reason!

    Because I can't use the () I cannot pass variables So I had to put al the code in the "Task_createtasklog" function".

    I would have perfered to read and change data on the main entity before running the "save" and then create the related entity....

    But I'm very new to JS and i'm sure i will eventually figure out why this is not working

    no ID is returned... (when in form is in create, not update)

    Xrm.Page.data.save().then(Tasks_createtasklog(), function () { alert("Fail"); });

    function Tasks_createtasklog() {

               alert(Xrm.Page.data.entity.getId().toString());              

       }

    to

    ID is returned... (when in form is in create, not update)

    Xrm.Page.data.save().then(Tasks_createtasklog, function () { alert("Fail"); });

    function Tasks_createtasklog() {

               alert(Xrm.Page.data.entity.getId().toString());              

       }

  • Community Member Profile Picture
    on at
    RE: Javascript Xrm.Page.data.entity.save() ignored?

    Hi Partner,

    Has the problem been solved? Any updates?

    Please click Yes under "Did this answer your question?" to close this thread.

    pastedimage1604568718630v1.png

    Thanks.

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Javascript Xrm.Page.data.entity.save() ignored?

    Hi partner,

    The js event will be trigger before writing data back to the database, so when user click save it can work.

    And if you want to refresh current form, please click refresh button not do F5, F5 can clear cache, so it resets the task form to "new task".

  • whydoihavetocreateanotherlogin Profile Picture
    25 on at
    RE: Javascript Xrm.Page.data.entity.save() ignored?

    I ended up putting all the code in the function and call it wihtout any parameters, not as clean as passing variables ...

    it works, i have a small issue with a attribute that is not setting itself to null. but when the user press save it works out.

    the only other issue i have is when the user does save the form and do F5, it resets the task form to "new task" instead of refreshing current form.

    so... for now its ok (i guess)

    function Tasks_addduration()
    {    
        
        if ((Xrm.Page.getAttribute('new_addtotaskdescription').getValue() != null) && (Xrm.Page.getAttribute('new_addtoduration').getValue() != null) && (Xrm.Page.getAttribute('new_addtoduration').getValue() != 0)) {         
            if (Xrm.Page.ui.getFormType()==1){
                Xrm.Page.data.save().then(Tasks_createtasklog ,  function(){alert("La creation du Task Log a échoué voir tony");});      
            } else  {  
                Tasks_createtasklog();            
                Xrm.Page.getAttribute('new_addtoduration').setValue(null);    
                Xrm.Page.data.entity.save();
            }        
        }    
    
        function Tasks_createtasklog()
        {
            var duration=Xrm.Page.getAttribute('new_addtoduration').getValue();
            var tasklogdes=Xrm.Page.getAttribute('new_addtotaskdescription').getValue();
            // do much more... 
        }

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Javascript Xrm.Page.data.entity.save() ignored?

    Hi Partner,

    It’s so strange, I’m not sure what happen in your test.

    So my advice is not to use the save() function in js, you can first fill required fields on the form and then click the save button manually.

    Then put the trigger event on the OnChange event of the other fields to avoid a lot of unnecessary errors and save time.

    Just like:

    function sumofcurrency(executionContext) {
    var formContext = executionContext.getFormContext();
    alert(formContext.data.entity.getId());
    }

    pastedimage1604369071014v2.png

    pastedimage1604369065263v1.png

  • whydoihavetocreateanotherlogin Profile Picture
    25 on at
    RE: Javascript Xrm.Page.data.entity.save() ignored?

    Arg!! ok so this works!?

    i removed the " ()" at the end of the function call from your example...
    And now I can read the ID in the Sub Function?  its strange,

    The function was being executed just couldn't read the id....

    from:

    Xrm.Page.data.save().then(Tasks_createtasklog()function () { alert("Fail"); });
    to
    Xrm.Page.data.save().then(Tasks_createtasklog, function () { alert("Fail"); });

    function test(executionContext) {
        alert("in function")
        Xrm.Page.getAttribute("subject").setValue("test910");//fill required field        
        Xrm.Page.data.save().then(Tasks_createtasklog, function () { alert("Fail"); });
        
        function Tasks_createtasklog() {
                alert(Xrm.Page.data.entity.getId().toString());              
        }
    }
    

  • whydoihavetocreateanotherlogin Profile Picture
    25 on at
    RE: Javascript Xrm.Page.data.entity.save() ignored?

    I noticed that you put the function "in the function" where as I put it outside.

    I tried your example but still on my side it does not work.

    I even tried to pass the ID with the function "inside" and still does not work...


    function test(executionContext) {
        alert("in function")
        Xrm.Page.getAttribute("subject").setValue("test910");//fill required field        
        Xrm.Page.data.save().then(Tasks_createtasklog(Xrm.Page.data.entity.getId()), function () { alert("Fail"); });
        
        function Tasks_createtasklog(id) {
                alert(Xrm.Page.data.entity.getId().toString());  
                alert(id.toString());  
        }
    }

    This also appears not to work  "getFormContext()" it throw and error. (the box to pass it is ticked, maybe not supported in crm2015 )

    There are threads online with others who can't get formContext, but right now its another hopefully unrelated issue

    How do you attach jscode to "after save" event? in the form properties I only have "on save" and "on load" ?

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Javascript Xrm.Page.data.entity.save() ignored?

    Hi Partner,

    I have try to add js you provide above to OnLoad event, they both work successfully.

    However, when I test with second code, I remove parameters(tasklogdes,duration).

    function test(executionContext) {
        var formContext = executionContext.getFormContext();
        formContext.getAttribute("new_name").setValue("test3366");//fill required field
    
        Xrm.Page.data.save().then(Tasks_createtasklog(), function () { alert("Fail"); });
    
        function Tasks_createtasklog() {
            alert(Xrm.Page.data.entity.getId().toString());  
        }
    }

    pastedimage1604304456327v1.png

    The OnSave event will trigger before actually writing data back to the database, so the GUID does not yet have a value.

    So you don’t need add Save() function to js code, you can trigger js event to get GUID of the current record after clicking save button manually, which will be easier.

  • whydoihavetocreateanotherlogin Profile Picture
    25 on at
    RE: Javascript Xrm.Page.data.entity.save() ignored?

    ok, so i figured out i need to use the "then" clause...

    can someone answer this for me?

    if i use this  : I get the id!

     Xrm.Page.data.save().then(function(){alert(Xrm.Page.data.entity.getId().toString());;} ,  function(){alert("fail");});        

    But if i do this, I don't get the id?

    Xrm.Page.data.save().then(Tasks_createtasklog(tasklogdes,duration) ,  function(){alert("Fail");});

    function Tasks_createtasklog(tasklogdes,duration)

    {

       alert(Xrm.Page.data.entity.getId().toString());

    }

    Don't both do the same thing? (sorry i am very very new to javascript... and yes i could write a plugin, but i feel i'm a line of code away from solving this)

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,869 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,733 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans