anusha(salesforce developer)

Thursday 11 August 2016

Senario for Trigger with testcode:

Senario for Trigger with testcode:
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.
Open eclipse and right click on salesforce project and select Create new Trigger, as shown in below image




Code:

=========
trigger Trigonstudent on Student__c(before insert){


List<Student__c> studentlist=Trigger.new;
set<String> emailset=new set<String>();


        for(Student__c s:studentlist){
        
              emailset.add(s.Email__c);
  
}
List<Student__c> duplicatestudentlist=[select s.name,s.email__c from Student__c   s where s.Email__c in : emailset];
set<string> duplicateemailset=new set<string>();


for(Student__c s:duplicatestudentlist){


   duplicateemailset.add(s.Email__c);
   }
   for(Student__c s:studentlist){


      if(duplicateemailset.contains(s.Email__c)) {


       s.Email__c.addError('Record already exist with some emailid');
           }
    }
}




Then We get  the things like below:








Note: You can add, edit, or delete Apex using the Salesforce.com user interface only in a Developer Edition organization, a Salesforce.com Enterprise Edition trial organization, or sandboxorganization. In a Salesforce.com production organization, you can only make changes to Apex by using the Metadata API


deploy call, the Force.com IDE, or theForce.com Migration Tool. The Force.com IDE and Force.com Migration Tool are free resources provided by salesforce.com to support its users and partners, but are not considered part of our Services for purposes of the salesforce.com Master Subscription Agreement.

Test Cases in Salesforce :

Test case integral part of code developement.
·         You must have at least 75% of your Apex scripts covered by unit tests to deploy your scripts to production environments. In addition, all triggers should have some test coverage.
·         Salesforce.com recommends that you have 100% of your scripts covered by unit tests, where possible.
·         Calls to System.debug are not counted as part of Apex code coverage in unit tests.
So, here we are going to create Test Case for trigger which we have written:


Testclass for it:
=============
@isTest 
private class TestonstudentTriggers {
    static testMethod void myUnitTest() {
         Student__c s = new Student__c();
            s.Name = 'Om Test';
            s.Lastname__c = 'LastName';
            s.Installment__c = 2000;
            s.Email__c = 'admin@shivasoft.in';
      try{
             insert s;
        }
        catch(System.DMLException e){
                                                System.assert(e.getMessage().contains('Record already exist with same email Id'));
       }
 }
}

When we run this we get through setup->Develop->ApexClasses




3)Trigger on Account
===============
Q)When we create an account then automatically 5 or more  contacts are created with that account name.

trigger accountTestTrggr2 on Account (after insert,after update,after undelete) {
List<Account> acc=[select BillingCountry from Account];
List<Contact> con=new List<Contact>(); 
List<Case> lstcase=new List<Case>();
if(Trigger.isInsert){
for(Account a: Trigger.new){
  for(Integer i=1;i<=5;i++){
      Contact con1=new Contact(accountid=a.id,lastname=a.Name+'.'+i,MailingCountry=a.BillingCountry);
       con.add(con1);
    
  }
  }
if(con.size()>0){
      insert con;
     } 
}

  if(trigger.isUndelete){
for(Account actobj:trigger.new){
Case csobj=new  Case();
csobj.Accountid=actobj.id;
csobj.Status='New';
csobj.origin='Email';
csobj.Description='An Account :'+actobj.Name+' has been created from Recyclebin';
lstcase.add(csobj);
}
if(lstcase!=null && lstcase.size()>0){
 insert lstcase;
}
  }
}


No comments:

Post a Comment