locked
How to get the Save Button of Form in CRM 2011 using Javascript RRS feed

  • Question

  • Is there any way to get the save button of a form which is shown above. I want to shown hide the save area based on the user role. I want to perfome this operation on load of the form based on user role I will show hide the save area of the form.
    Asim Sajjad Software Engineer http://asimsajjad.blogspot.com
    Tuesday, May 3, 2011 5:22 AM

Answers

All replies

  • Hi Asim, 1- you can use this xml code within the <CustomActions> tag to hide the save button for every user <HideCustomAction Location="Mscrm.Form.new_payment.MainTab.Save" HideActionId="Sample.Form.new_payment.Save.HideAction" /> 2- have a hidden flag (boolean) on your entity, lets say new_CanUserSave(Yes,No), set it to Yes through Javascript for the users who have access to save functionality or set it to No through javascript for the users who dont have access to the save functionality. 3- in this case, if the value of new_CanUserSave is true(Yes) then display the ribbon save button. you can use this xml code within <DisplayRules> tag <EnableRule Id="WHE.transaction.form.RaiseFirstInvoice.EnableRule"> <ValueRule Field="new_CanUserSave" Value="true" /> </EnableRule> so by having this combination, you display/hide save button. hope it helps,
    cheers, S.Khan MCTS
    Tuesday, May 3, 2011 6:32 AM
  • Hi Asim, 1- you can use this xml code within the <CustomActions> tag to hide the save button for every user <HideCustomAction Location="Mscrm.Form.new_payment.MainTab.Save" HideActionId="Sample.Form.new_payment.Save.HideAction" /> 2- have a hidden flag (boolean) on your entity, lets say new_CanUserSave(Yes,No), set it to Yes through Javascript for the users who have access to save functionality or set it to No through javascript for the users who dont have access to the save functionality. 3- in this case, if the value of new_CanUserSave is true(Yes) then display the ribbon save button. you can use this xml code within <DisplayRules> tag <EnableRule Id="WHE.transaction.form.RaiseFirstInvoice.EnableRule"> <ValueRule Field="new_CanUserSave" Value="true" /> </EnableRule> so by having this combination, you display/hide save button. hope it helps,
    cheers, S.Khan MCTS
    Tuesday, May 3, 2011 6:33 AM
  • I strongly suggest using a display or enable rule, maybe <RecordPrivilegeRule /> might come in handy determining the user roles.

    But if you really need to access the save button maybe you can use the IE developer tools (F12) to get the Save button's id (e.g. in my setup it is "contact|NoRelationship|Form|Mscrm.Form.contact.Save-Large".

    Tuesday, May 3, 2011 6:35 AM
  • I have found the id but how to get the object for further operation ?
    Asim Sajjad Software Engineer http://asimsajjad.blogspot.com
    Tuesday, May 3, 2011 7:08 AM
  • htmlElement.control should give you the object as defined with the behavior and control extensions.

    Tuesday, May 3, 2011 7:27 AM
  • Can you please give me the statement which return as I don't wehre htmlElement.control is as I know Xrm.Page is the namespace for the javascript. Please give me exact statement.
    Asim Sajjad Software Engineer http://asimsajjad.blogspot.com
    Tuesday, May 3, 2011 7:37 AM
  • you said you were able to get the id... so I was refering to:

     

    var htmlElement = document.getElementById("<Id of the button>");
    var control = htmlElement.control;
    

    Tuesday, May 3, 2011 7:38 AM
  • I have followed your above statement but it return null. here is my code

     

    document.getElementById("new_student|NoRelationship|Form|Mscrm.Form.new_student.MainTab.Save")
    


    Asim Sajjad Software Engineer http://asimsajjad.blogspot.com
    Tuesday, May 3, 2011 7:43 AM
  • parent.document.getElementById("") should do it... I am sorry, I didnt think about the fact that the scripts are executed in the child content iframe...

    Tuesday, May 3, 2011 8:17 AM
  • it didn't work as well, can you please test and then tell me how to get the object. I am frustrated by this is this possible or not
    Asim Sajjad Software Engineer http://asimsajjad.blogspot.com
    Tuesday, May 3, 2011 8:31 AM
  • I tried and posted the code, I am not sure what is going on....

    var saveButton = parent.document.getElementById("new_student|NoRelationship|Form|Mscrm.Form.new_student.MainTab.Save");
    
    

    this code returns the htmlElement, yes it does not have control like other composite controls but this is the most you can do with javascript.

    Tuesday, May 3, 2011 8:36 AM
  • I have run your above code but saveButton is null. don't know what is happinging. I am testing it on CRM Live is that the issue ?
    Asim Sajjad Software Engineer http://asimsajjad.blogspot.com
    Tuesday, May 3, 2011 8:50 AM
  • I have to do the same thing (hide button in javascript) but it does not work like Asim said.

    The variable with  parent.document.getElementById("new_student|NoRelationship|Form|Mscrm.Form.new_student.MainTab.Save"); is null

    Has somebody found a solution ?

    Thanks for your help !

    Thursday, May 26, 2011 1:04 PM
  • This is because the ribbon gets loaded after the onload event.

    Have a look at this blog for a better appoach

    http://gtcrm.wordpress.com/2011/02/23/hiding-a-ribbon-button-in-crm-2011/

    Regards,

    Juan

     

    Thursday, May 26, 2011 2:10 PM
  • Thanks for your answer.

    I know I can hide a button with the .xml file but I only need to hide the button in some situations

    To know if the button needs to be hidden, I do some verifications in a javascript function.

    If the verifications are not Ok, I want to hide the button.

    I think I can't execute a javascript file in the xml file when I use HideCustomAction.

    So how can I do this ?

    Thank you very much !

    Thursday, May 26, 2011 2:52 PM
  • Alex,

    In that case, why don't you perform the check on the onsave event, preventing the save and displaying a message "The record cannot be modified" to the user...?

    Regards,

    Juan

    Thursday, May 26, 2011 3:38 PM
  • Yes, you are right, it's a possibility but I don't believe my client will be happy ;)

    Thanks again for your help Juan !

    Thursday, May 26, 2011 3:53 PM
  • var x=parent.document.getElementById("new_student|NoRelationship|Form|Mscrm.Form.new_student.MainTab.Save");
    x.style.display="none";

    Try this. It is working fine for me.

     -Anand

    Monday, June 25, 2012 12:59 PM