Asked by:
Problem using the Activity 'statecode' to fire a javascript function

Question
-
I'm trying to execute a javascript function on the 'statecode' field in the Activity entities. By default when an activity is Open, it is set to '0', however I want to execute two other functions (one for 'Complete' and one for 'Cancel') based on the statecode status changes (1 & 2 respectively). However, when an activity is either marked as Complete or Cancelled; neither of these functions are firing, instead the activity is deactivated. I need the functions to run so how can I achieve this?Monday, July 22, 2013 9:44 AM
All replies
-
Hi Marcus,
You can't do it with javascript cause javascript is a client side scripting whereas statecode is getting changed in server.So all you need to do is you have to write some plugin on state change of the activity.
If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful" Thank you, DT
- Proposed as answer by Payman BiukaghazadehEditor Monday, July 22, 2013 3:13 PM
Monday, July 22, 2013 10:04 AM -
You will have to do this on save event. first check :-
Xrm.Page.ui.getFormType() ==4 //form is disabaled than check status and do the work
Regards Faisal
- Proposed as answer by Payman BiukaghazadehEditor Monday, July 22, 2013 3:11 PM
Monday, July 22, 2013 10:06 AM -
Thanks for the reply, how do I use the above line? In an if/else statement during an onSave event?Monday, July 22, 2013 2:37 PM
-
Yes, you could use it through an if condition as Faisal mentioned. However, the first reply is better and you could use plugin for a reliable solution.
- Edited by Payman BiukaghazadehEditor Monday, July 22, 2013 3:13 PM forget to mention the plugin
Monday, July 22, 2013 3:11 PMModerator -
Thanks for the reply. I'm not sure how to go about creating a plugin but do you have an example of how Faisal's code is used? I'm not sure how to go about implementing that.Monday, July 22, 2013 3:44 PM
-
Hi Marcus,
You can update the fields in Form Save event using JavaScipt.
1.You should pass the execution context
2.Below are the values of event mode for Activity on Save event
EntityEvent ModeValueActivitiesSave as Completed58ActivitiesClose Activity <sup>Note 2</sup>53.You can check if the Value is "58" or '5" call your JavaScript function to set the value .
Just refer following post to achieve this.
http://worldofdynamics.blogspot.in/2011/08/dynamics-crm-2011-perform-jscript.html
Hope this helps.
Regards,
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Thanks and Regards, Mohammad Yusuf Ansari http://microxrm.blogspot.in
- Edited by Mohammad Yusuf Ansari Tuesday, July 23, 2013 5:58 AM Update
Tuesday, July 23, 2013 5:58 AM