You need to sign in to do that
Don't have an account?
Test class for trigger
Hi All,
I have create custom User lookup field in Account,Field Name is SalesRep.When i select user name(John) and save the account. Opportunity Owner name chanege as (john) on Opportunity in the Account
I can't deploy it to production because I'm not able to create a test class for it... . The code is below, can someone help me build a test class for it?
trigger Opportunity_SalesRep on Account(after update) {
List<Id> accIds = new List<Id>();
for(Account acc : trigger.New){
//check if salesrep changed
if(acc.EXO_Sales_Rep__c != trigger.oldMap.get(acc.Id).EXO_Sales_Rep__c){
accIds.add(acc.Id);
}
}
//query all the matching opps
List<Opportunity > oppList = [SELECT Id,AccountId FROM Opportunity WHERE AccountId IN:accIds];
for(Opportunity opp : oppList ){
if(trigger.NewMap.containsKey(opp.AccountId)&&
trigger.NewMap.get(opp.AccountId).EXO_Sales_Rep__c != null){
opp.OwnerId = trigger.NewMap.get(opp.AccountId).EXO_Sales_Rep__c;
}
}
update oppList;
}
Something like the following should work - however, it doesn't provide bulk testing or include other best practices. The book "Advanced Apex Programming for Salesforce.com and Force.com" by Dan Appleman has some great testing solutions.
All Answers
Something like the following should work - however, it doesn't provide bulk testing or include other best practices. The book "Advanced Apex Programming for Salesforce.com and Force.com" by Dan Appleman has some great testing solutions.
thank u very much joannc