Monday 2 May 2011

List Index Number

Definition of List from Answers.com :keep a record(This is the keyword here); tabulate
Antonyms: forget

Ok ok I understand..Here comes the main post :

Force.com developers use collection type "List" quite often .We use List when the sequence(Index) of elements is important.

Lets take an Example :Assume we have two lists List1 and List2


As we added the Strings at the same time into two different lists we can be sure that respective strings have same Index no.

so  value at List1[i] = value at List2[i]  ..

Lets try to go through an USE CASE FOR THIS



Requirement  :Lets assume we have two objects Source(Custom Object) and  Contact(Standard Object) .Source object is a flat record (Normalized) object which stores contact Information and we need to push this data to Contact Object (De Normalizing)separately ie we need to insert two contacts as separate records.Also we need to track any error during insert operation at the source(custom object) level in a field called "Error Description" .( Note :For Simplicity only contact information is given)

Anatomy of Source Object:

Object Name :Source__c
Fields :
a) ContactName1__c
b) ErrorDescription__c
c)Email__c


Anatomy of Contact Object :
Object Name :Contact
Fields:
LastName
Email

Here in Contact Object Email is of Datatype "Email" .So proper email type value should be present in this field.

Following is the mapping: Assume we have two  records in the source object





In the above image we can observe that email mentioned for the second source record is not a valid one.So this would throw us an exception which is updated in the respective record's Error description field

Code for Insertion :



I executed the code in the system log and following is the Status now:










In the image we can observe Error Description is updated with the respective error.

This would be very useful when we are loading data into different objects from a single object and need to track the errors at the source object level so that the data can be cleansed



Happy Coding :) !!



No comments:

Post a Comment