You need to sign in to do that
Don't have an account?
Ganga
how to write a testclass for trigger please help me ....
Hi all
I am new to salesforce i just want to write atest class on the trigger below is the code please help me .... urgent
Thanks in Advance
trigger Updatactcontact on Opportunity (after update) {
Set<Id> accountidset=new Set<Id>();
Map<Id,Account> mapaccount=new Map<Id,Account>();
Map<Id,List<Contact>> mapcontacts=new Map<Id,List<Contact>>();
List<Contact> lstcontact=new List<Contact>();
for(Opportunity oppobj:trigger.new){
if(trigger.newmap.get(oppobj.id).stagename!=trigger.oldmap.get(oppobj.id).stageName)
{
if(oppobj.accountid!=null){
accountidset.add(oppobj.accountid);
}
}
}
if(accountidset!=null && accountidset.size()>0){
for(Account actobj:[select id,name,description,(select
id,name,OpportunityUpdate__c from Contacts) from Account where id in:accountidset]){
mapaccount.put(actobj.id,actobj);
mapcontacts.put(actobj.id,actobj.contacts);
}
}
for(Opportunity oppobj:trigger.new){
if(trigger.newmap.get(oppobj.id).stagename!=trigger.oldmap.get(oppobj.id).stageName)
{
if(oppobj.AccountiD!=null)
{
mapaccount.get(oppobj.AccountId).Description='Opportunity :'+oppobj.Name+'stagename changed from'+trigger.oldmap.get(oppobj.id).stageName+'to '+ trigger.newmap.get(oppobj.id).stageName;
for(Contact cont:mapcontacts.get(oppobj.AccountId)){
Contact contobj=new
Contact(id=Cont.id,OpportunityUpdate__c='Opportunity :'+oppobj.Name+' stagename changed from '+trigger.oldmap.get(oppobj.id).stageName+' to '+
trigger.newmap.get(oppobj.id).stageName);
lstcontact.add(contobj);
}
}
}
}
if(lstcontact!=null && lstcontact.size()>0)
update lstcontact;
if(mapaccount!=null && mapaccount.size()>0)
update mapaccount.values();
}
I am new to salesforce i just want to write atest class on the trigger below is the code please help me .... urgent
Thanks in Advance
trigger Updatactcontact on Opportunity (after update) {
Set<Id> accountidset=new Set<Id>();
Map<Id,Account> mapaccount=new Map<Id,Account>();
Map<Id,List<Contact>> mapcontacts=new Map<Id,List<Contact>>();
List<Contact> lstcontact=new List<Contact>();
for(Opportunity oppobj:trigger.new){
if(trigger.newmap.get(oppobj.id).stagename!=trigger.oldmap.get(oppobj.id).stageName)
{
if(oppobj.accountid!=null){
accountidset.add(oppobj.accountid);
}
}
}
if(accountidset!=null && accountidset.size()>0){
for(Account actobj:[select id,name,description,(select
id,name,OpportunityUpdate__c from Contacts) from Account where id in:accountidset]){
mapaccount.put(actobj.id,actobj);
mapcontacts.put(actobj.id,actobj.contacts);
}
}
for(Opportunity oppobj:trigger.new){
if(trigger.newmap.get(oppobj.id).stagename!=trigger.oldmap.get(oppobj.id).stageName)
{
if(oppobj.AccountiD!=null)
{
mapaccount.get(oppobj.AccountId).Description='Opportunity :'+oppobj.Name+'stagename changed from'+trigger.oldmap.get(oppobj.id).stageName+'to '+ trigger.newmap.get(oppobj.id).stageName;
for(Contact cont:mapcontacts.get(oppobj.AccountId)){
Contact contobj=new
Contact(id=Cont.id,OpportunityUpdate__c='Opportunity :'+oppobj.Name+' stagename changed from '+trigger.oldmap.get(oppobj.id).stageName+' to '+
trigger.newmap.get(oppobj.id).stageName);
lstcontact.add(contobj);
}
}
}
}
if(lstcontact!=null && lstcontact.size()>0)
update lstcontact;
if(mapaccount!=null && mapaccount.size()>0)
update mapaccount.values();
}
@isTest
private class TestUpdateContact {
static testMethod void testUpdateContact() {
Account acct = new Account(Name='Test');
insert acct;
Contact c = new Contact(LastName='Test');
insert c;
Opportunity opp = new Opportunity(
Name='Test Opp',
AccountId=acct.Id,
StageName='Prospecting',
CloseDate=Date.Today()
);
insert opp;
//Now, we can update the opp with a new stage name
opp.StageName = 'Closed Won';
update opp;
//Here, we'll test the account and contact description values were updated correctly
//Note that we have to query for the account and contact to get the updated values
String checkString = 'Opportunity stagename changed from Prospecting to Closed Won';
system.assertEquals(checkString, [SELECT Description FROM Account WHERE Id = :acct.Id].Description);
system.assertEquals(checkString, [SELECT Description FROM Contact WHERE Id = :c.Id].Description);
}
}
All Answers
@isTest
private class TestUpdateContact {
static testMethod void testUpdateContact() {
Account acct = new Account(Name='Test');
insert acct;
Contact c = new Contact(LastName='Test');
insert c;
Opportunity opp = new Opportunity(
Name='Test Opp',
AccountId=acct.Id,
StageName='Prospecting',
CloseDate=Date.Today()
);
insert opp;
//Now, we can update the opp with a new stage name
opp.StageName = 'Closed Won';
update opp;
//Here, we'll test the account and contact description values were updated correctly
//Note that we have to query for the account and contact to get the updated values
String checkString = 'Opportunity stagename changed from Prospecting to Closed Won';
system.assertEquals(checkString, [SELECT Description FROM Account WHERE Id = :acct.Id].Description);
system.assertEquals(checkString, [SELECT Description FROM Contact WHERE Id = :c.Id].Description);
}
}