Ramblings about MS Dynamics CRM 2011 and coding basics.

Wednesday, January 23, 2008

Auto-fill Opportunity's topic attribute

This might seem really simple, but sometimes it is really useful.

A lot of people don't know what to write in the TOPIC field of an opportunity record. If you don't write something, then the record appears to be unnamed to the user.

You can auto-fill the topic several ways:
1. You can have all Opportunities have a default name:
//OnLoad Code
if (crmForm.all.name.DataValue==null)
{
crmForm.all.name.DataValue="Opportunity";
}

2. You can set the CustomerID name into the TOPIC field:
//This will name the Opportunity after the client.
//OnChange for the CustomerID attribute
crmForm.all.name.DataValue = crmForm.all.customerid.DataValue[0].name;

Labels: , , , , , , , , , , , ,

Using User's Business Unit

This is something that I have done before, and it utilized Michael Höhne's code again. I made a modification to the User/Datetime Stamp that I previously posted.

In this code, I use the XML call to get the current user's business unit, and use this information to set a picklist. This "defaults" the picklist for the user, based on what his/her business unit normally picks.

Practical uses: This was requested by a client, to ensure that Opportunities were typed according to their business unit, so that the pipeline would be trackable by what business unit the user was in at the time.


(Sorry for the screenshot: Again, I don't know the best way to post XML without it rendering directly in the webpage.)

Labels: , , , , , , , , ,

Tuesday, December 18, 2007

Datetime User Stamp

I modified some of Michael Höhne's genius to compile an excellent datetime/user stamp.

Example Scenario: Checkbox on the Opportunity form called "Reviewed" When a user checks the bit that it is reviewed, you can kick off this action to automatically capture the current user's name and the datetime in other fields.

I normally make the new_text and new_date fields disabled and then use OnSave code to ForceSubmit those fields. This ensures that those fields can't be tampered with.

You can do other modifications to this, such as: disabled the checkbox once it is checked. This ensures that it can't be rechecked and have the datetime/user information changed in the future. If you do this, then you should place some JS in the OnLoad to check for that bit's value. If it is true, then disable it, so that it won't be accessible again.

Note: when doing this, it ties your hands if someone accidently checks the box and then saves the form. Remember, just be logical. :)

Please comment and be sure to check out Michael's site.

I couldn't figure out how to get the code formatted cleanly on the webpage without the tags rendering. If you have a better solution than "xmp" (it just isn't friendly). Please let me know and I will repost it.

Thanks.




Labels: , , , , , , , , , , , ,

Monday, December 17, 2007

Non-Sequential Dynamic Picklist

I know I haven't posted to my blog in a long time because I have been busy with work.

Here is something that I finished up today.

I needed to create a set of dynamic picklists that could be non-sequential. The SDK gives a solution for a sequential based picklist, but that doesn't take in account for additional values in the future. I found this post by Greg Owens' post here and have edited and rearchitected parts of it. Since he helped me out, I have decided to post it to help you out!

Feel free to post comments. Thank you again Greg!

//****************************************************************
//**Non-Sequential Dynamic Picklist
//**------------------------
//**Developers: Greg Owens (Initial structure)
//** Mark Nagao (Rearchitecture)
//**Date: December 17, 2007
//**Purpose: Dynamic SubPicklist of non-sequential values
//**MSCRM 3.0: OnChange Event Code
//****************************************************************
//****************************************************************
//**Assign your main and sub picklist
var oPicklist= crmForm.all.new_mainpicklist;
var oSubPicklist = crmForm.all.new_subpicklist;

//**End of Assignment editing
//****************************************************************

//Saving and Resetting Original Picklist Values
if(!oSubPicklist.originalPicklistValues)
{
oSubPicklist.originalPicklistValues = oSubPicklist.Options;
}
else
{
oSubPicklist.Options = oSubPicklist.originalPicklistValues;
}

//****************************************************************
//**Add Arrays and specify values to display in SubPicklist

var oArray1 = new Array(0,1,3,4);
var oArray2 = new Array(0,2,5,6);


//**End of ARRAY Editing
//****************************************************************

//Check that Main Picklist value is not Null
if(oPicklist.DataValue != null)
{
switch(oPicklist.SelectedText)
{

//****************************************************************
//**Edit Case: Add a case for each Array

case "Array1":
filterPicklist(oArray1);
break;

case "Array2":
filterPicklist(oArray2);
break;


//**End of CASE editing
//****************************************************************

}
}

//Function to Filter SubPicklist
function filterPicklist(oDesiredOptions)
{

//Create temporary array to hold selected Values
var oTempArray = new Array();
//FOR loop to cycle through all SubPicklist Values
for (var i=oSubPicklist.length;i >= 0;i--)
{
//FOR loop to cycle through desired SubPicklist Values
for (var j=oDesiredOptions.length;j >= 0;j--)
{
//Compare i(SubPicklist Value) and j(Desired Value)
if (i == oDesiredOptions[j])
{
oTempArray[i] = true;
oDesiredOptions.splice(j,1);
}
}
}
//FOR loop to cycle through all SubPicklist Values
for (var i=oSubPicklist.length;i >= 0;i--)
{
//Compare with Temporay Array
if(oTempArray[i] != true)
{
//Remove Value from list if not TRUE in Temporary Array
oSubPicklist.remove(i)
}
}
}

//****************************************************************
//**Notes:
// - Place code in the OnChange of the Main Picklist
// - In the array, you must have two values, so if there is
// only one value, then include the 0 (null) value
//****************************************************************
//**End of Non-Sequential Dynamic Picklist Code
//****************************************************************

Labels: , , , , , , , ,

Tuesday, October 31, 2006

JavaScript Math Functions

Since CRM 3.0 uses JS natively in the OnLoad and OnSave events, I have decided to post several key math functions in JS.

Also remember to use parentheses around the condition when writing an if statement, such as:
if (condition)
{
action;
}

//General math calculations
+ //Addition
- //Subtraction
/ //Division
* //Multiplication
% //Modulus (remainder after division)
++ //Increment by 1
-- //Decrement by 1

//Higher math calculations
MATH.ABS(X) //Absolute value of X
MATH.ACOS(X) //Arc Cosine of X
MATH.ASIN(X) //Arc Sine of X
MATH.ATAN(X) //Arc Tangent of X
MATH.ATAN2(X,Y) //Arc Tangent of X/Y
MATH.CEIL(X) //Integer closest to X and not less than X
MATH.COS(X) //Cosine of X
MATH.E //Euler's constant
MATH.EXP(X) //Exponent of X
MATH.FLOOR(X) //Integer closest to X and not greater than X
MATH.FLOOR(MATH.RANDOM()*10) //Returns random value between 0 and 9
MATH.LN10 //Natural Log of 10
MATH.LN2 //Natural Log of 2
MATH.LOG(X) //Log of X base E
MATH.LOG10E //Base-10 Log of E
MATH.LOG2E //Base-2 Log of E
MATH.MAX(X,Y) //Returns the maximum value of X or Y
MATH.MIN(X,Y) //Returns the minimum value of X or Y
MATH.PI //Pi
MATH.POW(X,Y) //X to the power of Y
MATH.RANDOM() //Returns random value between 0 and 1
MATH.ROUND(2.8) //This would equal 3
MATH.SIN(X) //Sine of X
MATH.SQRT(1_2) //Square Root of 1/2
MATH.SQRT(2) //Square Root of 2
MATH.TAN(X) //Tangent of X

//Operators
X=Y //X equals Y
X+=Y //X equals X plus Y
X-=Y //X equals X minus Y
X*=Y //X equals X multiplied by Y
X/=Y //X equals X divided by Y
X%=Y //X equals X modulus by Y
== //Equivalent
=== //Exact equivalent
!= //Not equal to
> //Greater than
< //Less than
>= //Greater than or equal to
<= //Less than or equal to
&& //And
//Or
! //Not

There are a lot of other powerful functions or more complex ways of putting these above listed functions together, but this should get you started.

Labels: , , , , ,