Friday, June 1, 2012

Remove item from picklist programatically in CRM 2011 with javascript

In Dynamics CRM 2011, I have a pick-list for my Opportunities with the follwing options, which influences my Sales Pipeline:
1 - Prospect
2 - Proposal
3 - Negotiation

What i had to do is to filter the picklist so that only some of the options to be available, not all of them in the same time, ensuring that the user can't jump stages (jumping from 1 to 3), and that the user can't go backwards (from 2 to 1, from 3 to 2 or 1).

The code is:

// Get value of Pipeline Phase set by user
if (Xrm.Page.getAttribute("new_salesstagecode").getSelectedOption() != null)
{var val_salesstagecode = Xrm.Page.getAttribute("new_salesstagecode").getSelectedOption().value;}
else {var val_salesstagecode = null;}

// Filter Pipeline Phase picklist to avoid users jumping stages
if (val_salesstagecode == 1)
{ Xrm.Page.getControl("new_salesstagecode").removeOption(3); }

else if (val_salesstagecode == 2)
{ Xrm.Page.getControl("new_salesstagecode").removeOption(1); }

else if (val_salesstagecode == 3)
{
Xrm.Page.getControl("new_salesstagecode").removeOption(1);
Xrm.Page.getControl("new_salesstagecode").removeOption(2);
}

No comments:

Post a Comment