Thursday 25 September 2014

Sample Triggers and test classes

trigger createopp on Account (after insert) {
    list<opportunity> opp = new list<opportunity>();
    for(account a : trigger.new){
        if(a.industry == 'Education'){
            opportunity op = new opportunity();
            op.name = a.Name;
            op.AccountId = a.Id;
            op.Amount = 500;
            op.CloseDate = system.today()+30;
            op.StageName = 'prospecting';
            opp.add(op);
        }
       
    }
    insert opp;

}
Testclass:
@istest
public class createopp {
    public static testmethod void insertopp(){
        account a = new account(name = 'parugu',industry = 'education');
        opportunity o = new opportunity(name ='parugu',accountid = a.id,amount = 500,
                                        closedate = system.today()+30,StageName = 'prospecting');
       
        insert a;
        insert o;
       
    }

}



trigger samenoonaccount on Contact (before insert) {

   list<account> acc = new list<account>();
  
   for(contact c:trigger.new){
   acc = [select id,name,phone from account where phone =:c.accountid];
   if(acc.size()>0){
  
      c.adderror('phone no aleredy exists in account');
     
   }
  
   }

}
Testclass
@isTest
public class samnoaccount {
    public static testmethod void testphone(){
        string mphone = '9090909090';
        string adderror ;
        account a = new account(name = 'vedam',phone = mphone);
        contact c = new contact(lastname = 'anushka',accountid = a.id,phone = '9090909090');
        insert a;
        list<account> acc = [select id,phone from account where phone =: c.phone];
        if(acc.size()>0)
        try{
            insert c;
        }
        catch(Exception e){
           
            System.AssertEquals('phone no aleredy exists in account', adderror);
        }
     
       
       
    }

}
--------------------------------------------------------------------------------------------------------------------------



No comments:

Post a Comment

Complete Salesforce CPQ training Free videos

Salesforcestart:: We are excited to announce that our YouTube channel, Salesforcestart, is your one-stop-shop for all things Salesforce CPQ!...