You need to sign in to do that
Don't have an account?
HelloSan
I need a test class for the below trigger for code coverage,both account and opportunity have record types
trigger newTrg on Opportunity (before insert) {
for(opportunity opp:trigger.new)
{
Set<Id> s1 = new Set<Id>();
if(opp.accountId != null){
s1.add(opp.accountid);
}
if(s1.size()>0)
{
Map<Id,Id> m1 = new Map<id,Id>();
for(Account acc : [Select id, ownerId from Account where id in:s1])
{
m1.put(acc.id,acc.ownerId);
}
for(Opportunity opp1:trigger.new){
//Custom Field
opp1.TestUser__c = m1.get(opp.accountId);
}
}
}
}
for(opportunity opp:trigger.new)
{
Set<Id> s1 = new Set<Id>();
if(opp.accountId != null){
s1.add(opp.accountid);
}
if(s1.size()>0)
{
Map<Id,Id> m1 = new Map<id,Id>();
for(Account acc : [Select id, ownerId from Account where id in:s1])
{
m1.put(acc.id,acc.ownerId);
}
for(Opportunity opp1:trigger.new){
//Custom Field
opp1.TestUser__c = m1.get(opp.accountId);
}
}
}
}
Below is the code that you are looking for:-
Please close the thread marking this answer as Best Answer if it really helped. Closing the thread help others finding the correct answer.
Regards,
Swayam Arora
All Answers
Below is the code that you are looking for:-
Please close the thread marking this answer as Best Answer if it really helped. Closing the thread help others finding the correct answer.
Regards,
Swayam Arora
trigger newTrg on Opportunity (before insert)
{
for(opportunity opp:trigger.new)
{
Set<Id> s1 = new Set<Id>();
if(opp.accountId != null)
{
s1.add(opp.accountid);
}
if(s1.size()>0)
{
Map<Id,Id> m1 = new Map<id,Id>();
for(Account acc : [Select id, ownerId from Account where id in:s1])
{
m1.put(acc.id,acc.ownerId);
}
for(Opportunity opp1:trigger.new)
{
//Custom Field
opp1.TestUser__c = m1.get(opp.accountId);
}
}
}
}
It seems you want to put last available opp's accountid to all opp's TestUser__c field.
For your reference, I came up with following test case. It only contains positive test case and I didn't confirm if there is any syntax issue.
@isTest
public class Opportunity_newTrg_Test
{
public static testMethod void PositiveTest()
{
List<Account> accounts = new List<Account>{};
List<Opportunity> opportunities = new List<Opportunity>{};
//build test account and insert
for(Integer i = 0; i < 5; i++)
{
Account a = new Account(Name = 'Test Account ' + i);
accounts.add(a);
}
insert accounts;
//build test opps and insert
for(Integer i = 0; i < 5; i++)
{
Opportunity opp = new Opportunity(Name = 'Test Opportunity ' + i, AccountId = accounts[i].id);
opportunities.add(opp);
}
insert opportunities;
//get the last opp's accountid
string lastaccid = accounts[4].id;
//retrieve the inserted opps
opportunities = [select TestUser__c from opportunity where name like 'Test Opportunity%'];
//assert
For (Opportunity opp : Opportunities)
{
system.asserteaquals(lastaccid,opp.TestUser__c);
}
}
}