{"id":1241,"date":"2010-10-11T09:55:59","date_gmt":"2010-10-11T04:25:59","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1241"},"modified":"2010-10-11T09:55:59","modified_gmt":"2010-10-11T04:25:59","slug":"step-by-step-salesforce-tutorial-creating-trigger-and-test-cases-6-of-6","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/step-by-step-salesforce-tutorial-creating-trigger-and-test-cases-6-of-6\/","title":{"rendered":"Step by Step Salesforce Tutorial \u2013 Creating Trigger and Test cases \u2013 6 of 6"},"content":{"rendered":"<div style=\"background-color: #f5e2ba; border: 1px solid #ccc; width: 100%; padding-top: 10px; margin-top: 10px; color: #140b5c;\">\n<ol>\n<li><a style=\"color: #140b5c !important;\" href=\"https:\/\/jitendrazaa.com\/blog\/webtech\/salesforce\/step-by-step-salesforce-tutorial-creating-custom-object-1-of-n\/\">Creating custom object <\/a><\/li>\n<li><a style=\"color: #140b5c !important;\" href=\"https:\/\/jitendrazaa.com\/blog\/webtech\/salesforce\/step-by-step-salesforce-tutorial-%E2%80%93-creating-fields-%E2%80%93-2-of-n\/\">Creating Fields<\/a><\/li>\n<li><a style=\"color: #140b5c !important;\" href=\"https:\/\/jitendrazaa.com\/blog\/webtech\/salesforce\/step-by-step-salesforce-tutorial-%E2%80%93-creating-tab-and-validation-rule-%E2%80%93-3-of-n\/\">Creating Tab and Validation Rule<\/a><\/li>\n<li><a style=\"color: #140b5c !important;\" href=\"https:\/\/jitendrazaa.com\/blog\/webtech\/salesforce\/step-by-step-salesforce-tutorial-%E2%80%93-creating-email-template-%E2%80%93-4-of-n\/\">Creating Email Template<\/a><\/li>\n<li><a style=\"color: #140b5c !important;\" href=\"https:\/\/jitendrazaa.com\/blog\/webtech\/salesforce\/step-by-step-salesforce-tutorial-%E2%80%93-creating-workflow-rule-%E2%80%93-5-of-n\/\">Creating Workflow rule<\/a><\/li>\n<li><a style=\"color: #140b5c !important;\" href=\"https:\/\/jitendrazaa.com\/blog\/webtech\/salesforce\/step-by-step-salesforce-tutorial-%E2%80%93-creating-trigger-and-test-cases-%E2%80%93-6-of-6\/\">Creating Trigger and Test cases <\/a><\/li>\n<\/ol>\n<\/div>\n<p>This is the last tutorial in series and we will see that how to create a <strong>Trigger and Test Cases<\/strong> in salesforce.<\/p>\n<p><strong>A trigger\u00a0is an\u00a0Apex\u00a0script that executes before or after specific data manipulation language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted.<\/strong><\/p>\n<p>Triggers are stored as metadata in\u00a0Salesforce.com. A list of all triggers in your organization is located at\u00a0<strong>Setup<\/strong> |\u00a0<strong>Develop<\/strong> |\u00a0<strong>Apex\u00a0Triggers<\/strong>.\u00a0In addition to this list, triggers are associated and stored with specific objects.<\/p>\n<p>To define a trigger:<\/p>\n<p><!--more--><\/p>\n<ol id=\"define\">\n<li>For a standard object, click\u00a0<strong>Setup<\/strong> |\u00a0<strong>Customize<\/strong>, click the name of the object, then click\u00a0<strong>Triggers<\/strong>.For a custom object, click\u00a0<strong>Setup<\/strong> |\u00a0<strong>Create<\/strong> |\u00a0<strong>Objects<\/strong> and click the name of the object.For campaign members, click\u00a0<strong>Setup<\/strong> |\u00a0<strong>Customize<\/strong> |\u00a0<strong>Campaigns<\/strong> |\u00a0<strong>Campaign Member<\/strong> |\u00a0<strong>Triggers<\/strong>.For case comments, click\u00a0<strong>Setup<\/strong> |\u00a0<strong>Cases<\/strong> |\u00a0<strong>Case Comments<\/strong> |\u00a0<strong>Triggers<\/strong>.\n<p id=\"EmailMessTrigPath\">For email messages, click\u00a0<strong>Setup<\/strong> |\u00a0<strong>Cases<\/strong> |\u00a0<strong>Email Messages<\/strong> |\u00a0<strong>Triggers<\/strong>.<\/p>\n<\/li>\n<li>In the Triggers related list, click\u00a0<strong>New<\/strong>.<\/li>\n<li>Click\u00a0<strong>Version Settings <\/strong>to specify the version of\u00a0Apex\u00a0and the\u00a0API\u00a0used with this trigger. If your organization has installed managed packages from the\u00a0AppExchange, you can also specify which version of each managed package to use with this trigger. Generally, you should use the default values for all versions. This associates the trigger with the most recent version of\u00a0Apex\u00a0and the\u00a0API, as well as each managed package.\u00a0You can specify an older version of a managed package if you want to access components or functionality that differs from the most recent package version. You can specify an older version of\u00a0Apex\u00a0and the\u00a0API\u00a0to maintain specific behavior.<\/li>\n<li>Select the\u00a0Is Active\u00a0checkbox if the trigger should be compiled and enabled. Leave this checkbox deselected if you only want to store the script in your organization&#8217;s metadata. This checkbox is selected by default.<\/li>\n<li>In the\u00a0Body\u00a0text box, enter the\u00a0Apex\u00a0for the trigger. A single trigger can be up to &#8220;1 million&#8221;\u00a0characters in length.<\/li>\n<\/ol>\n<p><a title=\"Trigger governor limit\" href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/apexcode\/Content\/apex_gov_limits.htm\">Read this URL for Governor limit\u00a0reference<\/a>.<\/p>\n<p>To define a trigger, use the following syntax:<\/p>\n<pre>trigger <var>triggerName<\/var> on <var>ObjectName<\/var> (<var>trigger_events<\/var>) {\n   <var>code_block<\/var>\n}<\/pre>\n<p>where trigger_events can be a comma-separated list of one or more of the following events:<\/p>\n<ul>\n<li>before insert<\/li>\n<li>before update<\/li>\n<li>before delete<\/li>\n<li>after insert<\/li>\n<li>after update<\/li>\n<li>after delete<\/li>\n<li>after undelete<\/li>\n<\/ul>\n<p>So, lets start with creating trigger.<\/p>\n<p>I want that duplicate student should not be created on the basis of Email id. we can achieve this by other way also, like during creation of email field, we can specify it as unique field.<\/p>\n<p>Open eclipse and right click on salesforce project and select Create new Trigger, as shown in below image<\/p>\n<figure id=\"attachment_1258\" aria-describedby=\"caption-attachment-1258\" style=\"width: 503px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Creating-Trigger-in-Salesforce-using-force.com-IDE.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1258\" title=\"Creating Trigger in Salesforce using force.com IDE\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Creating-Trigger-in-Salesforce-using-force.com-IDE.jpg?resize=503%2C501&#038;ssl=1\" alt=\"Creating Trigger in Salesforce using force.com IDE\" width=\"503\" height=\"501\" \/><\/a><figcaption id=\"caption-attachment-1258\" class=\"wp-caption-text\">Creating Trigger in Salesforce using force.com IDE<\/figcaption><\/figure>\n<p>As you can see, to create trigger we have to select the Apex version and operations in wizard.<\/p>\n<p>During Trigger creation, keep in mind that it may be required in bulk operations so\u00a0governor limit may be problem.<\/p>\n<p>So instead of getting query for all triggers individually, I created an array and after that created a set of email entered for all records, and loop through all records invidually and checked for duplicity.<br \/>\nOnly one SOQL is fired instead of all triggers individually and thus can work for bulk insert.<\/p>\n<p>For SOQL Best Practice refer :<br \/>\n<a title=\"SOQL best practice\" href=\"http:\/\/wiki.developerforce.com\/index.php\/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops\"> http:\/\/wiki.developerforce.com\/index.php\/Best_Practice:_Avoid_SOQL_Queries_ Inside_FOR_Loops<\/a><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\ntrigger DuplicateStudentCheck on Student__c (before insert) {\n\n       \/\/Get all Student__c related to the incoming Student records in a single SOQL query.\n       Student__c&#x5B;] studentsList = Trigger.new;\n       Set emailSet = new Set();\n       for(Student__c s : studentsList)\n       {\n       \temailSet.add(s.Email__c);\n       }\n\n       \/\/Get list of duplicate Students\n       List duplicateStudentList = &#x5B;Select s.Name, s.Email__c From Student__c s\nwhere s.Email__c IN :emailSet];\n\n       Set duplicateEmailIds = new Set();\n\n       for(Student__c s : duplicateStudentList)\n       {\n       \tduplicateEmailIds.add(s.Email__c);\n       }\n\n       for(Student__c s : studentsList)\n       {\n       \t    if(duplicateEmailIds.contains(s.Email__c))\n       \t    {\n       \t    \ts.Email__c.addError('Record already exist with same email Id');\n       \t    }\n       }\n}\n<\/pre>\n<p><strong><span style=\"text-decoration: underline;\">Note:<\/span><\/strong> You can add, edit, or delete\u00a0Apex\u00a0using the\u00a0Salesforce.com\u00a0user interface only in a Developer Edition organization, a\u00a0Salesforce.com\u00a0Enterprise Edition trial organization, or\u00a0<a href=\"https:\/\/na2.salesforce.com\/help\/doc\/en\/create_test_instance.htm\">sandbox<\/a> organization. In a\u00a0Salesforce.com\u00a0production organization, you can only make changes to\u00a0Apex\u00a0by using the\u00a0Metadata API <strong>deploy<\/strong> call, the\u00a0Force.com IDE, or theForce.com Migration Tool. The\u00a0Force.com IDE\u00a0and\u00a0Force.com Migration Tool\u00a0are free resources provided by\u00a0salesforce.com\u00a0to support its users and partners, but are not considered part of our Services for purposes of the\u00a0salesforce.com\u00a0Master Subscription Agreement.<\/p>\n<p><strong>Test Cases in Salesforce :<\/strong><\/p>\n<p><strong> <\/strong><br \/>\nTest case integral part of code developement.<\/p>\n<ul id=\"code_coverage_list\">\n<li>You must have <strong>at least 75% <\/strong>of your\u00a0Apex\u00a0scripts covered by unit tests to deploy your scripts to production environments. In addition,<strong> all triggers should have some test coverage<\/strong>.<\/li>\n<li>Salesforce.com\u00a0recommends that you have 100% of your scripts covered by unit tests, where possible.<\/li>\n<li>Calls to<br \/>\n<samp>System.debug\u00a0<\/samp>are not counted as part of\u00a0Apex\u00a0code coverage in unit tests.<\/li>\n<\/ul>\n<p>So, here we are going to create Test Case for trigger which we have written:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@isTest\nprivate class TestTriggers {\n\n    static testMethod void myUnitTest() {\n        Student__c s = new Student__c();\n        s.Name = 'Om Test';\n        s.l_Name__c = 'LastName';\n\n        Course__c c = new Course__c();\n        c.Name = 'SFDC';\n        c.Fees__c = 2000;\n        insert c;\n\n        s.Course__c = c.Id;\n        s.Installment_1__c = 2000;\n        s.Email__c = 'admin@JitendraZaa.com';\n        try\n        {\n        \tinsert s;\n        }\n        catch(System.DMLException e)\n        {\n        \tSystem.assert(e.getMessage().contains('Record already exist with same email Id'));\n        }\n    }\n}\n<\/pre>\n<p>To run the test case from Eclipse, right click on test class as shown in below image:<\/p>\n<figure id=\"attachment_1259\" aria-describedby=\"caption-attachment-1259\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Run-Test-Case-using-Eclipse-in-Salesforce.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1259\" title=\"Run Test Case using Eclipse in Salesforce\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Run-Test-Case-using-Eclipse-in-Salesforce.jpg?resize=500%2C180&#038;ssl=1\" alt=\"Run Test Case using Eclipse in Salesforce\" width=\"500\" height=\"180\" \/><\/a><figcaption id=\"caption-attachment-1259\" class=\"wp-caption-text\">Run Test Case using Eclipse in Salesforce<\/figcaption><\/figure>\n<p>The output of the test case, Test coverage result:<\/p>\n<figure id=\"attachment_1260\" aria-describedby=\"caption-attachment-1260\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Test-Coverage-result-Eclipse-in-Salesforce.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1260\" title=\"Test Coverage result Eclipse in Salesforce\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Test-Coverage-result-Eclipse-in-Salesforce.jpg?resize=410%2C45&#038;ssl=1\" alt=\"Test Coverage result Eclipse in Salesforce\" width=\"410\" height=\"45\" \/><\/a><figcaption id=\"caption-attachment-1260\" class=\"wp-caption-text\">Test Coverage result Eclipse in Salesforce<\/figcaption><\/figure>\n<p>Run test cases from salesforce.com browser:<\/p>\n<p id=\"code_run_tests_2\">click\u00a0<strong>Setup<\/strong> |\u00a0<strong>Develop<\/strong> |\u00a0<strong>Apex\u00a0Classes<\/strong>, click the name of the class, then click\u00a0<strong>Run Test<\/strong>. If your class calls another class or causes a trigger to execute, those\u00a0Apex\u00a0scripts are included in the total amount used for calculating the percentage of code covered.<\/p>\n<p>To run all the unit tests in your organization, click\u00a0<strong>Setup<\/strong> |\u00a0<strong>Develop<\/strong> |\u00a0<strong>Apex\u00a0Classes<\/strong>, then click\u00a0<strong>Run All Tests<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Step by Step Salesforce Tutorial \u2013 Creating Trigger and test cases \u2013 6 of 6 tutorials series<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9],"tags":[331,203],"class_list":["post-1241","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-salesforce","tag-trigger"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1192,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/step-by-step-salesforce-tutorial-creating-custom-object-1-of-n\/","url_meta":{"origin":1241,"position":0},"title":"Step by Step Salesforce Tutorial \u2013 Creating custom object \u2013 1 of 6","author":"Jitendra","date":"October 6, 2010","format":false,"excerpt":"Step by Step Salesforce Tutorial - Creating custom object - 1 of n tutorial series","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Create Custom object Eclipse Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Create-Custom-object-Eclipse-Salesforce-300x290.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1200,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/step-by-step-salesforce-tutorial-creating-fields-2-of-n\/","url_meta":{"origin":1241,"position":1},"title":"Step by Step Salesforce Tutorial \u2013 Creating fields \u2013 2 of 6","author":"Jitendra","date":"October 7, 2010","format":false,"excerpt":"Step by Step Salesforce Tutorial \u2013 Creating fields \u2013 2 of n series tutorial","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Create Custom object though Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Create-Custom-object-though-Salesforce-300x238.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1233,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/step-by-step-salesforce-tutorial-creating-workflow-rule-5-of-n\/","url_meta":{"origin":1241,"position":2},"title":"Step by Step Salesforce Tutorial \u2013 Creating Workflow rule \u2013 5 of 6","author":"Jitendra","date":"October 10, 2010","format":false,"excerpt":"Step by Step Salesforce Tutorial \u2013 Creating Workflow rule \u2013 5 of n","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce creating Workflow rule step 1","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Salesforce-creating-Workflow-rule-step-1.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1213,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/step-by-step-salesforce-tutorial-creating-tab-and-validation-rule-3-of-n\/","url_meta":{"origin":1241,"position":3},"title":"Step by Step Salesforce Tutorial \u2013 Creating Tab and Validation Rule \u2013 3 of 6","author":"Jitendra","date":"October 8, 2010","format":false,"excerpt":"Step by Step Salesforce Tutorial \u2013 Creating Tab and Validation Rule \u2013 3 of n","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Edit Page Layout Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Edit-Page-Layout-Salesforce-300x105.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1224,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/step-by-step-salesforce-tutorial-creating-email-template-4-of-n\/","url_meta":{"origin":1241,"position":4},"title":"Step by Step Salesforce Tutorial \u2013 Creating Email Template \u2013 4 of 6","author":"Jitendra","date":"October 9, 2010","format":false,"excerpt":"Step by Step Salesforce Tutorial \u2013 Creating Email Template \u2013 4 of n","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Create Email Template in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Create-Email-Template-in-Salesforce.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2797,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/creating-trigger-on-attachment-in-salesforce\/","url_meta":{"origin":1241,"position":5},"title":"Creating Trigger on Attachment in Salesforce","author":"Jitendra","date":"March 28, 2012","format":false,"excerpt":"Example and tutorial on creating the trigger for attachment in salesforce which will not allow to upload the file in opportunity if file contains some predefined text","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Before insert Trigger on Attachment using eclipse in Salesforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Before-insert-Trigger-on-Attachment-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Before-insert-Trigger-on-Attachment-Salesforce.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Before-insert-Trigger-on-Attachment-Salesforce.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1241"}],"version-history":[{"count":1,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1241\/revisions"}],"predecessor-version":[{"id":6876,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1241\/revisions\/6876"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}