function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gateway Bikes Group4Gateway Bikes Group4 

Hi I'm new to salesforce Development and working on Triggers

/*Write a trigger that automatically changes a Contact’s email address to “hello@world.com” whenever a Contact is created. Verify it works!
*/
trigger ContactEmail on Contact(before insert){
   
 for(Contact con:Trigger.new){
        
 con.Email='hello@world.com';
 
  
}
}

@isTest
private class ContactEmailTest {

    @isTest static void createContact() {
        
// Implement test code
      
    
           Contact con = new Contact(
  
                    FirstName='fname',
 
                    LastName ='lname',
 
                    Email = 'email@gmail.com',
  
                    Phone = '9743800309');
               
                         insert con;

}
}

 
Raj VakatiRaj Vakati
Its correct  
 
trigger ContactEmail on Contact(before insert){
 for(Contact con:Trigger.new){
 con.Email='hello@world.com';
}
}


and Test class also correct 
@isTest
private class ContactEmailTest {

    @isTest static void createContact() {
        
    
           Contact con = new Contact(
  
                    FirstName='fname',
 
                    LastName ='lname',
 
                    Email = 'email@gmail.com',
  
                    Phone = '9743800309');
               
                         insert con;

}
}

 
Gateway Bikes Group4Gateway Bikes Group4
Hi Raj,
Thanks for answering the Question,but after runining the Test class.unable to get Code coverage 
Raj VakatiRaj Vakati
I am getting 100 % code coverage .. can you please check the test class is failing or runnning? there might be some error in another trigger or validation 
Gateway Bikes Group4Gateway Bikes Group4
Hi Raj,
Sorry fo the late response..Its was syntatical error..It worked
Raj VakatiRaj Vakati
Cool!
Gateway Bikes Group4Gateway Bikes Group4
Hi Raj,

I'm working on trigger
Use case:I want to count the number of opportunitties that are assoiciated with help custom field on Account object.
When ever opportunites are deleted,updated,insert count has to update
Here the code i'm working & lost some where??

trigger updateoppCount on Opportunity (after insert,after update,after delete) {
set<id> slds = new set<id>();
if(trigger.isupdate||trigger.isinsert){
  for(opportunity opt:trigger.new){
          slds.add(opt.Accountid);
          
          if(trigger.isupdate){
          if(opt.Accountid!=trigger.oldmap.get(opt.id).Accountid){
          slds.add(trigger.oldmap.get(opt.id).Accountid);
            }
          }
         }
       }
    
    if(trigger.isdelete){
    for(opportunity opp:trigger.old){
    slds.add(opp.Accountid);
       }
    }
    slds.remove(null);
    
    List<Account> postlist = [select id,name,OpportunityCount__c from Account where id in:slds];
    }

//Apex class

trigger updateoppCount on Opportunity (after insert,after update,after delete) {

set<id> slds = new set<id>();
if(trigger.isupdate||trigger.isinsert){
  for(opportunity opt:trigger.new){
          slds.add(opt.Accountid);
          
          if(trigger.isupdate){
          if(opt.Accountid!=trigger.oldmap.get(opt.id).Accountid){
          slds.add(trigger.oldmap.get(opt.id).Accountid);
            }
          }
         }
       }
    
    if(trigger.isdelete){
    for(opportunity opp:trigger.old){
    slds.add(opp.Accountid);
       }
    }
    slds.remove(null);
    
    List<Account> postlist = [select id,name,OpportunityCount__c from Account where id in:slds];
 }
Raj VakatiRaj Vakati
try this
 
trigger updateoppCount  on Opportunity (after delete,after update,after insert,after undelete) {
    
    set<ID> AccIds = new set<ID>();   
    
    if(trigger.isinsert || trigger.isundelete){
        for(opportunity opp : trigger.new){
            AccIds.add(opp.AccountId);
        }
    }
    if(trigger.isdelete){
        for(opportunity opp : trigger.old){
            AccIds.add(opp.AccountId);           
        }        
    }
       
    if(trigger.isupdate){
        for(opportunity opp:trigger.new){
            AccIds.add(opp.AccountId);
            if(trigger.oldmap.get(opp.id).AccountId != opp.AccountId && trigger.oldmap.get(opp.id).AccountId != null ){
                AccIds.add(trigger.oldmap.get(opp.id).AccountId);
            }            
        }
    }    
    map<id,Integer> amtmap = new map<id,Integer>();
    for(aggregateresult ag : [select AccountId ,count(id) cc from opportunity where AccountId in:AccIds   group by AccountId]){
        amtmap.put((ID)ag.get('AccountId'), Integer.valueof(ag.get('cc')));

     }
    list<account>acclist = new list<account>();
   
    for(id iid : AccIds){
        account acnt = new account(id=iid);
        if(amtmap.containskey(iid)){
            acnt.OpportunityCount__c  = amtmap.get(iid);
        }else{
            acnt.OpportunityCount__c  = 0;
        } 
        acclist.add(acnt);       
    }
   
    if(acclist.size()>0){
        update acclist;
    }
   
}

 
Gateway Bikes Group4Gateway Bikes Group4
HI,
I have been workin on Trigger with TestClass with Code Coverage

Write a trigger that creates two identical Opportunities when ever an Account is created.Make sure both opportunities are associates with the Account.Use any values for the fields on the opportunities.Just make sure to use variables when populating the fields of each opportunity to make sure they are identical.

trigger IdenticalOpp on Account(after insert){
list<Opportunity> opplist = new list<Opportunity>();
  for(Account a:Trigger.new)
    {
        Opportunity opp=new Opportunity();
        opp.AccountId=a.id;
        opp.Name='Created from account';
        opp.StageName='Prospecting';
        opp.CloseDate=Date.toDay();
        opplist.add(opp);
        
        Opportunity opp1=new Opportunity();
        opp1.AccountId=a.id;
        opp1.Name='Created from account';
        opp1.StageName='Prospecting';
        opp1.CloseDate=Date.toDay();
        opplist.add(opp1);
        }
       
    if(opplist != null){
        insert opplist ;
    }
    
   
}



@isTest
private class IdenticalOppTest {
    
@isTest
static void Opportunitycreate() {
         Account acct = new Account(Name='Test Account');
        insert acct;
        Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
                                       StageName='Prospecting',
                                       CloseDate=System.today().addMonths(1),
                                       AccountId=acct.Id);
        insert opp;