<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jonathanrico. &#187; apex</title>
	<atom:link href="http://jonathanrm.com/tag/apex/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonathanrm.com</link>
	<description></description>
	<lastBuildDate>Sun, 01 Aug 2010 18:01:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<script type="text/javascript">

if (typeof Meebo == 'undefined') {

Meebo=function(){(Meebo._=Meebo._||[]).push(arguments)};
(function(q){

	var args = arguments;
	if (!document.body) { return setTimeout(function(){ args.callee.apply(this, args) }, 100); }
	var d=document, b=d.body, m=b.insertBefore(d.createElement('div'), b.firstChild); s=d.createElement('script');
	m.id='meebo'; m.style.display='none'; m.innerHTML='<iframe id="meebo-iframe"></iframe>';
	s.src='http'+(q.https?'s':'')+'://'+(q.stage?'stage-':'')+'cim.meebo.com/cim/cim.php?network='+q.network;
	b.insertBefore(s, b.firstChild);

})({network:'jonathanrico_jo56qo'});	}</script>	<item>
		<title>Force.com Apex Merge/Replace Class ;)</title>
		<link>http://jonathanrm.com/2010/02/force-com-apex-mergereplace-class/</link>
		<comments>http://jonathanrm.com/2010/02/force-com-apex-mergereplace-class/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 07:26:10 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[apex]]></category>
		<category><![CDATA[force.com]]></category>
		<category><![CDATA[visualforce]]></category>

		<guid isPermaLink="false">http://jonathanrm.com/?p=85</guid>
		<description><![CDATA[Hello again! I just developed an Apex class for performing replace/merge operations on any kind of object, and yes any kind of relations as well! , so it doesn&#8217;t matter if you have master-detail relationships that can&#8217;t be modified, objects tied in a master-detail relationship will be cloned, deleted and inserted with the new values [...]]]></description>
			<content:encoded><![CDATA[<p>Hello again!</p>
<p>I just developed an Apex class for performing replace/merge operations on any kind of object, and yes any kind of relations as well! , so it doesn&#8217;t matter if you have master-detail relationships that can&#8217;t be modified, objects tied in a master-detail relationship will be cloned, deleted and inserted with the new values to simulate a merge operation!</p>
<p>To use the Merge class  just create an ObjectMerge instance, passing as an argument of the constructor your current record:</p>
<p><strong>ObjectMerge oMerge = new ObjectMerge(SourceObject); </strong></p>
<p>And to peform the merge operation just use the replaceRecord method and thats it!, you can specify if you want to delete the source record after the merge operation is done.</p>
<p><strong>oMerge.replaceRecord(MergeToThisObjectID,DeleteSourceObject);</strong></p>
<p>You can limit the number of records that will be processed in the merge operation for Master-Detail relations  to avoid hitting governor limits. The ObjectMerge class will let you know if there are any objects left, and if this is the case just continue performing the merge operation.</p>
<p>Force.com provides a merge DML operation but it&#8217;s only available for some of their standard objects, this class can help you get around that.  There are still some limitations with this class due to governor limits in the number of describe calls but unless you have a large amount of objects related to the replaced/merged record it should work pretty well.</p>
<p>For now, the Object Merge class only merges/replaces custom objects related to your record, but this can be changed  in the class , however keep in mind that no more than 10 relations can be processed due to describe calls limitations so be careful when removing the custom objects limitation because there can be many Non-Custom objects related to your record.</p>
<p>I&#8217;ll leave a link in this post to the files but for now here&#8217;s an example of how to use the ObjectMerge class in a VisualForce controller that merges contacts, if you have custom objects tied a contact A and want to merge contact A to Contact B you will see how the ObjectMerge class changes all of the objects from contact A to contact B then deletes contact A. Remember, you can try it with any custom object <img src='http://jonathanrm.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />   Enjoy!</p>
<p>Download the files from here &#8211;&gt; <a title="ObjectMerge Files" href="http://jonathanrm.com/wp-content/uploads/2010/02/ObjectMerge1.zip" target="_blank">ObjectMerge</a></p>
<p>(NOTE: There&#8217;s a bug with the code viewer plug-in, please ignore the second viewer)</p>
<img style='display:none' id="post-85-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://jonathanrm.com/2010/02/force-com-apex-mergereplace-class/',title:'Force.com Apex Merge/Replace Class ;)',tweet:'Hello again! I just developed an Apex class for performing replace/merge operations on any kind of o',description:'Hello again! I just developed an Apex class for performing replace/merge operations on any kind of o'})"><script type='text/javascript'>document.getElementById("post-85-blankimage").onload();</script><textarea cols="40" rows="10" name="code" class="Java">public class MergeController{
    
    //This is just an Aux Object to get a Lookup Field in the VisualForce Page
    public Contact MergeAux{get;set;}
    //Declare an instance of the Object Merge Class
    private ObjectMerge oMerge;
    //Save the current contact in the controller (optional)
    private final Contact currentContact;
        
    public MergeController(ApexPages.StandardController controller){

        //Initialize all of our objects
        currentContact = (Contact)controller.getRecord();    
        MergeAux =new Contact();
        oMerge = new ObjectMerge(currentContact);        
        
    }
    
    //Merge Action
    public PageReference doMerge(){

        try{
     
            //Run the merge operation!, you can specify a second argument to delete the record after merge or not
            oMerge.replaceRecord(MergeAux.ReportsToId,false);
            
            //Do corresponding actions if merge operation was successful
            if(oMerge.bAppliedReplace){
                PageReference ref = new PageReference('/'+MergeAux.ReportsToId);
                ref.setRedirect(true);
                return ref;
            }else{
                return null;


        }catch(Exception e){
            
            ApexPages.addMessages(e);
            return null;    
        }
        
    }   

}

//And the Visual Force page will look something like this:

<apex:page standardController="Contact" extensions="MergeController">
     
     <apex:form >
         <apex:pageBlock >
             <apex:pageMessages id="messages">
             </apex:pageMessages>
             
             <apex:pageBlockButtons >
                 <apex:commandButton value="Merge" action="{!doMerge}" rerender="messages"/>
             </apex:pageBlockButtons>
             <apex:pageBlockSection columns="1">
                 <apex:outputField value="{!Contact.Name}"/>
                 <apex:inputField value="{!MergeAux.ReportsToId}"/>                
                 
             </apex:pageBlockSection>
         </apex:pageBlock>
     </apex:form>
     
</apex:page>

</textarea>
	<!-- WordPress Code Snippet -->
	<script type="text/javascript" src="http://jonathanrm.com/wp-content/plugins/wordpress-code-snippet/js/shCore.js"></script><script type="text/javascript" src="http://jonathanrm.com/wp-content/plugins/wordpress-code-snippet/js/shBrushJava.js"></script>
	<link type="text/css" rel="stylesheet" href="http://jonathanrm.com/wp-content/plugins/wordpress-code-snippet/css/SyntaxHighlighter.css"/>
	
	<script language="javascript">
	dp.SyntaxHighlighter.ClipboardSwf = 'http://jonathanrm.com/wp-content/plugins/wordpress-code-snippet/js/clipboard.swf';
	dp.SyntaxHighlighter.HighlightAll('code');
	</script>
	<!-- End WordPress Code Snippet -->
	]]></content:encoded>
			<wfw:commentRss>http://jonathanrm.com/2010/02/force-com-apex-mergereplace-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interesting ways of architecting Apex Triggers</title>
		<link>http://jonathanrm.com/2010/01/interesting-ways-of-architecting-apex-triggers/</link>
		<comments>http://jonathanrm.com/2010/01/interesting-ways-of-architecting-apex-triggers/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 05:54:55 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[apex]]></category>
		<category><![CDATA[force.com]]></category>

		<guid isPermaLink="false">http://jonathanrm.com/?p=65</guid>
		<description><![CDATA[Hello everyone! I came across a blog post at gokubi.com (http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers) about some interesting ways of architecting Apex Triggers. We have developed a lot of logic in Apex Triggers and ended with some really nasty code in some scenarios; this post got me thinking about new ways to architect triggers using a more object-oriented approach. [...]]]></description>
			<content:encoded><![CDATA[<p>Hello everyone!</p>
<p>I came across a blog post at gokubi.com (<a href="http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers" target="_blank">http://gokubi.com/archives/two-interesting-ways-to-architect-apex-triggers</a>) about some interesting ways of architecting Apex Triggers.</p>
<p>We have developed a lot of logic in Apex Triggers and ended with some really nasty code in some scenarios; this post got me thinking about new ways to architect triggers using a more object-oriented approach. So just to follow the thoughts in this post I&#8217;ll show you what I did to organize our triggers and it has been working pretty good so far.</p>
<p>NOTE: Please refer to the code section below</p>
<p>//Step 1</p>
<p>I started by creating an Apex Class to replicate some of the triggers functionality&#8230;</p>
<p>//Step 2</p>
<p>The next step will be to start coding our Apex Class that will perform all of the logic in our triggers, so let&#8217;s say the we are building a trigger for My_Object__c custom object.</p>
<p>//Step 3</p>
<p>And finally, we just need to create the Apex Triggers that will just initialize our Trigger class and initialize an instance of the corresponding Apex Class that will handle all of the logic.</p>
<p>And that&#8217;s it! Just remeber to cast the the collections correctly before accessing the collections from the ExtTrigger class because we can&#8217;t work with generic sobject collections.</p>
<p>Some of the benefits that I&#8217;ve seen by organizing Apex Triggers in this way are the following:</p>
<ul>
<li>100% Test Coverage in Apex Triggers (although all of the Test Coverage should be done in the handler Apex Class)</li>
<li>Since we can call functions to perform specific actions in Apex Classes, we end up with more organized code, it&#8217;s easier to read in the sense that you can just go to see the calls inside the &#8220;onBeforeInsert&#8221; section for example..</li>
<li>Avoid the max number of characters governor limit:
<ul>
<li>Maximum number of characters for a class: 100,000</li>
<li>Maximum number of characters for a trigger: 32,000</li>
</ul>
</li>
</ul>
<p>Thanks to everyone that posted in gokubi&#8217;s blog post any feedback on this is appreciated <img src='http://jonathanrm.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<img style='display:none' id="post-65-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://jonathanrm.com/2010/01/interesting-ways-of-architecting-apex-triggers/',title:'Interesting ways of architecting Apex Triggers',tweet:'Hello everyone! I came across a blog post at gokubi.com (http://gokubi.com/archives/two-interesting-',description:'Hello everyone! I came across a blog post at gokubi.com (http://gokubi.com/archives/two-interesting-'})"><script type='text/javascript'>document.getElementById("post-65-blankimage").onload();</script><textarea cols="40" rows="10" name="code" class="Java">
//--------------Step 1

public class ExtTrigger {

	public List newValues;
	public List oldValues;
	public Map newMap;
	public Map oldMap;

	public Boolean isInsert = false;
	public Boolean isUpdate = false;
	public Boolean isDelete = false;
	public Boolean isUndelete = false;
	public Boolean isBefore = false;
	public Boolean isAfter = false;

	public ExtTrigger(List oldValues,List newValues,Map oldMap,Map newMap){

		this.newValues = newValues;
		this.oldValues = oldValues;
		this.oldMap = oldMap;
		this.newMap = newMap;

	}

}

//--------------Step 2

public class My_Object_Trigger {

	//Attributes
	public ExtTrigger MTrigger;

	public My_Object_Trigger(ExtTrigger MTrigger){

		this.MTrigger = MTrigger;

	}

	public void onBeforeInsert(){

		try{

			doSomething();
			doSomethingElse();

		}catch(Exception e){

		System.debug('Trigger Insert Exception '+e);
		((My_Object_c)MTrigger.newValues[0]).addError(sExceptionMessage);

		}

	}

	public void onBeforeUpdate(){

		try{

			doSomething();
			doSomethingElse();

		}catch(Exception e){

			System.debug('Trigger Update Exception '+e);
			((My_Object_c)MTrigger.oldValues[0]).addError(sExceptionMessage);

		}

	}

	//Etc....

	private void doSomething(){}
	private void doSomethingElse(){}

}

//--------------Step 3

trigger My_Object_Trigger on My_Object__c (before insert, before update) {

	//Create the instance of ExtTrigger and call the constructor in our apex class
	My_Object_Trigger triggerHandler = new My_Object_Trigger(
	new ExtTrigger(Trigger.old,Trigger.new,Trigger.oldMap,Trigger.newMap)
	);

	if(Trigger.isBefore){

		//Now we set the attributes for our trigger depending on the current context
		triggerHandler.MTrigger.isBefore = true;
	
		if(Trigger.isInsert){
	
			triggerHandler.MTrigger.isInsert = true;
			triggerHandler.onBeforeInsert();
	
		}
	
		if(Trigger.isUpdate){
	
			triggerHandler.MTrigger.isUpdate = true;
			triggerHandler.onBeforeUpdate();
	
		}

	}

}</textarea>
	<!-- WordPress Code Snippet -->
	<script type="text/javascript" src="http://jonathanrm.com/wp-content/plugins/wordpress-code-snippet/js/shCore.js"></script><script type="text/javascript" src="http://jonathanrm.com/wp-content/plugins/wordpress-code-snippet/js/shBrushJava.js"></script>
	<link type="text/css" rel="stylesheet" href="http://jonathanrm.com/wp-content/plugins/wordpress-code-snippet/css/SyntaxHighlighter.css"/>
	
	<script language="javascript">
	dp.SyntaxHighlighter.ClipboardSwf = 'http://jonathanrm.com/wp-content/plugins/wordpress-code-snippet/js/clipboard.swf';
	dp.SyntaxHighlighter.HighlightAll('code');
	</script>
	<!-- End WordPress Code Snippet -->
	]]></content:encoded>
			<wfw:commentRss>http://jonathanrm.com/2010/01/interesting-ways-of-architecting-apex-triggers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Force.com Spring &#8217;10 Release</title>
		<link>http://jonathanrm.com/2010/01/force-com-spring-10-release/</link>
		<comments>http://jonathanrm.com/2010/01/force-com-spring-10-release/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 04:16:26 +0000</pubDate>
		<dc:creator>jonathan</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[apex]]></category>
		<category><![CDATA[force.com]]></category>

		<guid isPermaLink="false">http://jonathanrm.com/?p=61</guid>
		<description><![CDATA[Salesforce&#8217;s Spring &#8217;10 is packed with some really nice enhancements, new interface, code scheduler, no limits in collections, and more! For more information go to: http://developer.force.com/releases/release/Spring10 Here&#8217;s a small demo of how code scheduler works: document.getElementById("post-61-blankimage").onload();]]></description>
			<content:encoded><![CDATA[<p>Salesforce&#8217;s Spring &#8217;10 is packed with some really nice enhancements, new interface, code scheduler, no limits in collections, and more!</p>
<p>For more information go to: <a href="http://developer.force.com/releases/release/Spring10">http://developer.force.com/releases/release/Spring10</a></p>
<p>Here&#8217;s a small demo of how code scheduler works:</p>
<p><object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/nbfPgr7LLfc&#038;hl=en_US&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed wmode="transparent" src="http://www.youtube.com/v/nbfPgr7LLfc&#038;hl=en_US&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object></p>
<img style='display:none' id="post-61-blankimage" onload="Meebo('discoverSharable', {element: ((this.parentNode.className.match('post')) ? this.parentNode : this.parentNode.parentNode) ,url:'http://jonathanrm.com/2010/01/force-com-spring-10-release/',title:'Force.com Spring &#8217;10 Release',tweet:'Salesforce&#8217;s Spring &#8217;10 is packed with some really nice enhancements, new interface, cod',description:'Salesforce&#8217;s Spring &#8217;10 is packed with some really nice enhancements, new interface, cod'})"><script type='text/javascript'>document.getElementById("post-61-blankimage").onload();</script>]]></content:encoded>
			<wfw:commentRss>http://jonathanrm.com/2010/01/force-com-spring-10-release/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
