Saturday, August 6, 2011

Dynamics CRM 2011 : Perform JScript Validations on Entity Form Before Execution of Special Events


In many development scenarios there is a requirement to perform Jscript validations on entity form before the execution of special events by clicking on the entity form buttons i.e. Activate, Deactivate, Lead Qualify, Resolve Case, Cancel Case  etc. You can use the step by step guidelines below to overcome this scenario. You may also reffer to the table at bottom of this guide for a list of record saved event model values.

[Instructions]

1. Create a new JScript web resource or you can use existing JScript web resource, copy the following Jscript code in the web resource. Make sure to replace <Value> in code line wod_SaveEventVal = <Value>; with the event mode value as per required. For record saved event model values you can refer to the table at the bottom of the guide.



[Jscript: Code]


// Use the following function on Form Save Event,
// CRM will pass the execution context in function paramter prmContext

function FrmOnSave(prmContext) {
    // Local variable to store value indicating how the save event was initiated by the user.
    var wod_SaveMode, wod_SaveEventVal;

    // Change the Save Event Value as per required Save Event
    wod_SaveEventVal = <Value>;

    if (prmContext != null && prmContext.getEventArgs() != null) {

        wod_SaveMode = prmContext.getEventArgs().getSaveMode();

        // 1 will pass on Recalculate button click
        if (wod_SaveMode == wod_SaveEventVal) {
            // Write your validation code here

            alert("Write your validation code here");

            // Use the code line below only if validation is failed then abort function save event
            prmContext.getEventArgs().preventDefault();
        }
    }
}





































2. Open Entity Form (Settings->Customization->Customize the System->Entities-> <Entity> ->Forms->Main)

3. Click on Form Properties button, add Jscript web resource library if not already added. In Event Handlers section select OnSave in event field, click on Add button enter FrmOnSave value in function* field and make sure to check Enabled and Pass execution context as first parameter checkboxes.


































4. Save Form and Publish entity customizations.
[Table: Entity Record Saved Event Mode Values]

The following table describes the values returned for different ways entity records saved by the user. The save event will be called only for the actions that required form save.

Entity
Event Mode
Value
Activities
Save as Completed
58
Activities
Close Activity Note 2
5
Activities
To Opportunity Note 2
5
Activities
To Case Note 2
5
Activities
To Lead Note 2
5
All
Save Note 2
1
All
Save and Close
2
All
Deactivate
5
All
Reactivate
6
Article
Submit
10
Article
Approve
12
Article
Reject
11
Article
Unpublish
13
Campaign Activity
Close Campaign Activity
5
Campaign Activity
Distribute Campaign Activity
4
Campaign Response
Convert Campaign Response (Create new lead or Create new record for a customer)
54
Campaign Response
Convert Campaign Response (Convert Existing Lead -> Qualify)
16
Campaign Response
Convert Campaign Response (Convert Existing Lead-> Disqualify)
15
Campaign Response
Close Response
5
Case
Resolve Case
5
Case
Cancel Case
40
Case
Reactivate Case
6
Contract
Invoice Contract
38
Contract
Copy Contract
39
Contract
Recalculate Note 2
1
Contract
Hold Contract
31
Contract
Renew Contract
34
Contract
Cancel Contract
33
E-mail
Send
7
Goal
Recalculate
66
Goal
Close Goal
5
Invoice
Invoice Paid
57
Invoice
Cancel Invoice
27
Invoice
Recalculate
1
Invoice
Get Products
44
Invoice
Lock Pricing
52
Lead
Qualify
16
Lead
Disqualify
15
Opportunity
Close as Won Note 2
5
Opportunity
Close as Lost Note 2
5
Opportunity
Recalculate Opportunity Note 2
1
Order
Create Invoice
19
Order
Fullfill Order
56
Order
Cancel Order
26
Order
Recalculate Note 2
1
Order
Get Product
43
Order
Lock Pricing
50
Product
Convert to Kit
35
Product
Convert to Product
36
Queue
Approve E-mail Note 1
4
Queue
Reject E-mail Note 1
4
Quote
Recalculate Note 2
1
Quote
Get Products
28
Quote
Activate Quote
29
Quote
Create Order
17
Quote
Revise
24
Quote
Close Quote
25
User
Approve E-mail Note 1 & Note 2
4
User
Reject E-mail Note 1 & Note 2
4
User
Change Business Unit Note 1
4
User or Team Owned Entities
Assign
47

Special Notes Information
1. Event modes having Notes 1 means that system invoke the Save Form event after the execution of the event, so any validations written in the FrmOnSave Jscript method will be executed after the execution of event.

2. Event modes having Notes 2 means that the Save Event Mode values are duplicated with other buttons Save Event Mode values.


Files Download Link:

1. Table in Excel Sheet format: https://skydrive.live.com/?cid=06f61fc8aa6032c9&id=6F61FC8AA6032C9%21136#

2. JScript code file: https://skydrive.live.com/?cid=06f61fc8aa6032c9&sc=documents&uc=2&id=6F61FC8AA6032C9%21135#


22 comments:

  1. Hi Javeed,

    I want to know when the case gets reactivated on load of form. I followed your code, to know the save mode of the case, but when I perform the check on execution context of the page, like
    context.getEventArgs(), it throws an error saying "object does not support this property or method".
    What might be wrong !!?
    fyi, I'm using CRM 2011 online.

    Thanks
    Srinath

    ReplyDelete
  2. Hi Srinath,

    Make sure that in Save event handler properties you have checked the option Pass execution conext as first paramter, as shown in step number 3 image.

    ReplyDelete
  3. How can I fire my onSave script before Resolve case dialog?

    ReplyDelete
    Replies
    1. Hi Anonymous,

      There is not supported customization available in MS Dynamics CRM to fire onSave script before Resolve Case dialog, you have to either overload the Resolve Case button JScript function (which is not supported customization and not recommended) or you can follow the solution below:

      1. Create a new webpage or silverlight web resource that will contain all resolve case fields and it will close the case using CrmWebservice CloseIncidentRequest (http://www.resultondemand.nl/support/sdk/T_Microsoft_Crm_Sdk_Messages_CloseIncidentRequest.htm)
      2. Add ribbon button for case resolution and hide existing button
      3. In ribbon button JScript method call your script and then open the custom case resolution webpage/silverlight web resource

      Delete
  4. Hi Jehhanzeb,

    I tried the code and entered value 6 so i can "reactivate a case" but now i get a error message when trying to reactivate a closed case which says "message from webpage Write your validation code here"

    Help!!

    Thanx

    ReplyDelete
    Replies
    1. Hi Raz,

      Yes need to replace this code line with your JScript validations code that you want to perform on Reactivating a closed case i.e. may be you want to check if the Case Owner == "Javeed" then show alert message and return without saving:

      Xrm.Page.getAttribute("ownerid")[0].name

      // Use the following function on Form Save Event,
      // CRM will pass the execution context in function paramter prmContext

      function FrmOnSave(prmContext) {
      // Local variable to store value indicating how the save event was initiated by the user.
      var wod_SaveMode, wod_SaveEventVal;

      // Change the Save Event Value as per required Save Event
      wod_SaveEventVal = 6;

      if (prmContext != null && prmContext.getEventArgs() != null) {

      wod_SaveMode = prmContext.getEventArgs().getSaveMode();

      // 1 will pass on Recalculate button click
      if (wod_SaveMode == wod_SaveEventVal) {
      // Write your validation code here

      if (Xrm.Page.getAttribute("ownerid")[0].name != "Javeed")
      {
      alert("To reactivate the case the user name should be Javeed");
      // Use the code line below only if validation is failed then abort function save event
      prmContext.getEventArgs().preventDefault();
      }


      }
      }
      }

      Delete
  5. Hi Raz

    That is be cause there is an alert in the code - if you don't want the the laert to appear, then comment this line out...

    Cheers,
    H

    ReplyDelete
    Replies
    1. Thank you Anonymous for taking your time to help the CRM community :)

      Delete
  6. Hi Raz
    i used you code above but even after i put the validation code {which is to set a field to required level} the case wont be resolved what would the issue be?

    ReplyDelete
  7. It's not just the explosion of Starbucks across the nation that's fueling it, though their marketing has certainly awakened the taste buds
    of a whole generation. Organic coffee can be purchased in bags and in bulk.
    Jura Coffee machine has all vast range of coffee machines to match with your requirements.


    Also visit my web site :: Coffee cleanse max supplement (http://sibutramina-rivotril.blogspot.co.il/)

    ReplyDelete
  8. Oh my goodness! Awesome article dude! Many thanks, However I am experiencing issues with your RSS.
    I don't understand why I cannot subscribe to it. Is there anybody getting identical RSS problems?
    Anyone that knows the solution will you kindly respond?

    Thanks!!

    Feel free to visit my weblog; Christian Louboutin Sale

    ReplyDelete
  9. you're actually a just right webmaster. The web site loading pace is incredible.

    It seems that you're doing any unique trick. In addition,
    The contents are masterwork. you have performed a wonderful activity
    in this topic!

    Here is my web page - Christian Louboutin Shoes (www.fluhrerreed.com)

    ReplyDelete
  10. I was wondering if you ever thought of changing
    the layout of your blog? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it
    better. Youve got an awful lot of text for only having one or two pictures.
    Maybe you could space it out better?

    Check out my webpage - Christian Louboutin Boots (caladan.us.es)

    ReplyDelete
  11. I was excited to discover this web site. I want to to thank you for ones time due to this wonderful read!!
    I definitely liked every part of it and I have you
    saved to fav to check out new information on your site.


    Here is my site: Pirater un Compte facebook

    ReplyDelete
  12. Nevertheless, after only twenty minutes, the star of your day was released: the iPhone 5.

    Great attribute of jailbreak iphone Mobile can it be comes with assurance to go back your money.


    my blog post: cydia download free for android

    ReplyDelete
  13. Good way of describing, and good paragraph to take information about my presentation subject, which i am going to convey in college.


    Take a look at my web blog buy likes for facebook page uk

    ReplyDelete
  14. My рartner and I stumbled oѵer here by a diffеrent web
    pɑge and thought I might as well checҝ things out.
    I liҝe what I see so i am just following yoս. Look forward to
    looking οver your web рage agaіn.

    Feel free to visit my page weight loss Rewards

    ReplyDelete
  15. I am curious to find out what blog platform you happen to be working with?
    I'm experiencing some small security issues
    with my latest website and I'd like to find something more risk-free.
    Do you have any suggestions?

    Here is my site: sbo promotion

    ReplyDelete
  16. In many development scenarios there is a requirement to perform Jscript validations on entity form before the execution of special events by clicking on the entity form buttons i.e. Activate, Deactivate, Lead Qualify, Resolve Case, Cancel Case etc. You can use the step by step guidelines below to overcome this scenario. You may also reffer to the table at bottom of this guide for a list of record saved event model values.
    black and red salwar ,
    red black salwar suit ,

    ReplyDelete