You need to sign in to do that
Don't have an account?
Gateway 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;
}
}
*/
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;
}
}
and Test class also correct
Thanks for answering the Question,but after runining the Test class.unable to get Code coverage
Sorry fo the late response..Its was syntatical error..It worked
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];
}
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;