Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Monday, March 21, 2011

Updating Read-Only Dynamics CRM 2011 Fields (Pipeline Phase and Probability) from a Sales Stage Dropdown

We’re setting up Dynamics CRM 2011 at my workplace and I had to figure out how to set up our pipeline. Here’s my solution:

fffcpplt

I created a Sales Stage field that updates the Pipeline Phase and Probability fields on change. I followed Ayaz Ahmad’s solution for hooking up a javascript function to my Opportunity form and then wrote the following function (after some iteration).

function new_SalesStage_OnChange()
{
var new_salesstageAttr = Xrm.Page.getAttribute("new_salesstage");
var stepnameAttr = Xrm.Page.getAttribute("stepname");
var closeprobabilityAttr = Xrm.Page.getAttribute("closeprobability");

closeprobabilityAttr.setSubmitMode("always");
stepnameAttr.setSubmitMode("always");

if (new_salesstageAttr.getSelectedOption() == null)
{
stepnameAttr.setValue("");
closeprobabilityAttr.setValue(null);
}
else
{
var ssVal = new_salesstageAttr.getSelectedOption().text;
var pl = ssVal.lastIndexOf('%');
var probability = parseInt(ssVal.substring(pl-2, pl));

stepnameAttr.setValue(ssVal);
closeprobabilityAttr.setValue(probability);
}
}

The biggest tricks were:



  • Using LOWER CASE names for my field names.  This breaks if I use getAttribute(“new_SalesStage”), which is it’s real name.  Booo, Microsoft!!!
  • Handling the null value.
  • Using the setSubmitMode(“always”) call to make sure the readonly fields were beiing saved.

This solution should have been quicker but I learned a lot on it.