Tuesday, August 25, 2009

Triggers in SFDC

Apex triggers are the scripts that executes before and after the DML events.
Triggers that are associated with different business objects inside the Salesforce can be found at Setup->Develop->Apex Triggers and in the respective business object details page.

Syntax of writing a Trigger

trigger triggerName on ObjectName (trigger_events)
{
// code_block
}


types of trigger_events are:
1.before insert
2.before update
3.before delete
4.after insert
5.after update
6.after delete
7.after undelete

Single trigger can be of 32000 characters in length.

Lets see an example of how to send mail from the trigger

trigger mailMyObject on myObject (after update)
{
if(Trigger.isafter)
{
for(myObject s : Trigger.new)
{
String[] toAddresses;
String[] ccAddresses;
String[] replyTo;

//Business Logic

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
mail.setCcAddresses(ccAddresses);
mail.setReplyTo(replyTo;);
mail.setSenderDisplayName('SFDC');
mail.setSubject('Mail from SFDC');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setHtmlBody('Mail Body');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}

Monday, August 24, 2009

Named and Optional Arguments

One of the interesting feature of C # 4.0 is to have the Named and Optional Arguments, Optional Argument gives the flexibility of omitting the parameters in the call, and Named Argument gives the flexibility of passing the arguments by name in place of position.

Named Argument:
It provides us the flexibility or positioning the parameters by name in place of position, the value can be be provided by the use of argument. Lets see an example

student s = new student(name : "Ram", age : 25)

Optional Argument:
Any call for the function having the Optional Arguments have to specify only the required argument and can omit the optional arguments. Each of the Optional argument must have the default value assigned so that that value can be used when the value is not provided in the method call.

Method definition
void getClass (string Name, int age=20) (…)
Method Call
s.getClass(“Ram”);

When we invoke the method above it will pass Name as the parameter provided in the method call and the default value of age will be used.

Now let us consider the overloaded methods
void getClass (string Name, int age=20) (…)
void getClass (object o) (…)
void getClass (int age , string name = “Ram”) (…)
void getClass (int age) (…)

and if we call
s.getClass(4);

In the above overloaded functions the function getClass (string Name, int age=20) cannot be called as the parameter are not matching so remaining 3 are the better bet, in the remaining 3 converting the value 4 to int is better than converting to object.Among the last two overloaded functions, signature of the method getClass(int age) matches exactly to the call due to which the optional argument method will get lower priority and method getClass(int age) will be called.

Dynamic in C#

Dynamic Keyword allows us to invoke things dynamically, any operations that are called on the object, will be analyzed only at the runtime and the behavior will be decided at that instance.

static void Main(string[] args)
{
dynamic d1 = 1; //1
int i = d1; //2
dynamic d2 = GetDynamicObject(); //3
d2.foo(1,2) ; //4
var x = d2 + 10; //5

}

Let us diagnose the above-mentioned code how the things are working
1. Implicit Conversion of an integer.
2. Assignment Conversion to an integer.
3. Implicit conversion of an object to dynamic, this statement will be skipped at the compile time and will be resolved only at runtime and the compiler will allow us to call a method with any name and argument.
4. As the method foo is called, compiler will instruct that at runtime method foo have to be called with two arguments. Therefore, the runtime instance of d2 will decide the method availability.
5. As the operator + is having the dynamic typed argument it will be resolved at the runtime, at runtime object d2 will search for the operator overloaded resolution for +.


Compile time: At compile time for all the dynamic lookups uses DLR (Dynamic Language Runtime). DLR takes advantage of its call sites for all the dynamic operations.

Run time: All Methods and properties are binded by using reflection

customize newform/Editform in Sharepoint 2007

Steps to customize newform/Editform in Sharepoint 2007 using sharepoint designer.
1. Create a custom list
2. Open the site in Sharepoint designer
3. Navigate to newform.aspx of newly created custom list in Sharepoint designer
4. Take the back up of newform.aspx in the same folder.
5. Open the newform.aspx in the Edit mode and select the Split view.
6. Select the webpart in PlaceHolderMain (Custom) in “Design Mode”, and move to the “Code View” and Hide the webpart by putting the webpart code a separate table and Hide the row. You can hide the row by setting style 'display:none'.
We need to Hide the default content in place of deleting the code otherwise the reference with the parent code will be missing.
7. Switch to Design Mode and place the cursor on the new row of the table.
8. Navigate to Insert->Sharepoint Control->Custom List Form


9. A new Popup will appear for selecting the List

As we are editing the Newform.aspx select “New item form” in the “Type of Form to create” section.
10. Now the page is ready for the editing purpose and you can design by changing the location of the controls on the page