You need to sign in to do that
Don't have an account?
pj_27
Test class issue for OppOwner display trigger
Hi
I am writing a test class for the trigger that populates Proposed Opportunity Owner field (ProposedOpportunityOwner__c) with the Opportunity owner name. Trigger fires only if the user belongs to the Canada profile.This trigger is working fine and the field ProposedOpportunityOwner__c is populated with the Opportunity Owner name.
In a test class I have created a Canada user and inserted an Opportunity in that users context.However for this inserted Opportunity I am getting the NULL value for ProposedOpportunityOwner__c field.
Please suggest.Thanks.
Just checking, by context do you mean you're using System.runAs(CanadaUser)
yes.
Please check the following code :
@istest
private class OpportunityOwnerDisplay
{
public static testMethod void testRunAs()
{
// This code runs as the Canada Proposal Team user
Profile p = [select id from profile where name='Canada Sales Ops'];
User u = new User(alias = 'caduser', email='canadauser@ca.fujitsu.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,
timezonesidkey='America/Los_Angeles', username='caduser@ca.fujitsu.com.uat');
System.runAs(u)
{
String Uname = [select Name from User u where username ='caduser@ca.fujitsu.com.uat'][0].Name;
System.debug('The Username !@#!@#!@# '+Uname);
Id OpportunityRTId = [Select r.Id,r.Name From RecordType r where r.Name ='CA Express Services - Simple/Pure'][0].Id;
Id OppOwnerId = [Select u.Id From User u Where username = 'caduser@ca.fujitsu.com.uat'][0].Id;
Id AccntSaaSRTId = [Select r.Id From RecordType r Where Name = 'SaaS Account Record'][0].Id;
District__c ObjDist = new District__c(Name = 'TestDist',CurrencyIsoCode= 'USD');
insert ObjDist;
Account ObjAcc = new Account( Name='MyTestAcc_',RecordTypeId = AccntSaaSRTId,
Industry2__c = 'Accommodation & Food', Type = 'Business Partner', CurrencyIsoCode = 'USD',
BillingCity ='New York',BillingCountry = 'United States',BillingState ='MA',
BillingStreet ='27,Myaddress',BillingPostalCode = '81024');
insert ObjAcc; //Insert the Account Object
Opportunity objOpportunity = new Opportunity(Name = 'MyTestOpportunity_',
RecordTypeId = OpportunityRTId ,Lead_Source_Name__c = 'MytestLead',OwnerId = OppOwnerId,
AccountId = ObjAcc.Id,CurrencyIsoCode ='USD',StageName ='Qualifying Deal',
Contract_Type__c = 'T&M',ForecastCategoryName ='Pipeline',Location__c = ObjDist.Id ,
CloseDate = Date.today().addDays(10), LeadSource='SAP',Type='New Client',
Risk_Category__c ='Standard',Using_Proposal_Center__c = 'No');
insert objOpportunity;
System.debug('ProposedOpportunityOwner Name:@#@#@#@#'+ objOpportunity.ProposedOpportunityOwner__c);
}
}
}
hey, it does not suffice to merely create the User in memory, as far as I'm aware.
you need insert statements.
so
insert u;
before you attempt to do a run as.
I have updated the code by inserting the user as per your suggestion , still not working:
Code -
// featching the profile Id of to create the user of that profile('Canada Sales Ops')
Profile p = [select id from profile where name='Canada Sales Ops'];
User u = new User(alias = 'caduser', email='canadauser@ca.fujitsu.com',
emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,
timezonesidkey='America/Los_Angeles', username='caduser@ca.fujitsu.com.uat');
insert u ;
System.runAs(u)
{
}
I'd recommend you double-check that this query Profile p = [select id from profile where name='Canada Sales Ops']; is actually returning a Profile and not null.
No, It does not returns NULL ; it gives me the Id of the 'Canada Sales Ops' profile.
Hello there ,any can please help on this ?