-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
2Questions
-
6Replies
Need help with test class for the following Trigger. I was able to cover 71% but could'nt do the rest. Any suggestion on how to cover the Url.get parameter??
trigger RG_OpportunityTgr on Opportunity (after insert, before update) {
if(Trigger.isInsert){
Map<Id, Opportunity> oppMap=new Map<Id, Opportunity>();
RG_OpportunityTgrCls.OpportunityAssignUpdateCount(Trigger.new, oppMap, TRUE, FALSE);
}
IF(Trigger.IsUpdate){
RG_OpportunityTgrCls.OpportunityAssignUpdateCount(Trigger.new, Trigger.oldMap, false, TRUE);
Id userProfileId = userinfo.getProfileId();
Id currentUserId = UserInfo.getUserId();
String userProfileName = [SELECT ID, Name from Profile Where Id = : userProfileId].Name;
if( (userProfileName != 'System Administrator' && currentUserId != System.Label.Natalie_userId
&& currentUserId != System.Label.Barry_Fitts_UserId)){
RG_OpportunityTgrCls.OwnerReasonCheck(Trigger.new, Trigger.oldMap);
}
}
}
APex: class
public class RG_OpportunityTgrCls {
public static void OpportunityAssignUpdateCount(List<Opportunity> oppList, Map<Id, Opportunity> oldOppMap, boolean isInsert, boolean IsUpdate){
Map<Id,Integer> userIdTocountMap = new Map<Id, integer>();
Map<Id, Integer> userIdToCountMinusMap= new Map<Id, Integer>();
Map<Id,Boolean> usedIdToPlusMinusMap = new Map<Id, Boolean>();
for(Opportunity opp: OppList){
if(opp.LeadSource != 'SOI' && opp.T2T_Agent__c == Null){
Integer count = 1;
IF(isInsert){
// IF(userIdTocountMap.containsKey(opp.OwnerId)){
// count+=userIdTocountMap.get(opp.OwnerId);
// userIdTocountMap.put(opp.OwnerId, count);
//}ELSE
userIdTocountMap.put(opp.OwnerId, count);
usedIdToPlusMinusMap.put(opp.OwnerId,True);
}
if(IsUpdate && oldOppMap.GET(opp.Id)!=NULL && opp.OwnerId!=oldOppMap.GET(opp.Id).OwnerId){
//new user to add count
opp.Pullback_Campaign__c = System.today();
if(userIdTocountMap.containsKey(opp.OwnerId)){
count+=userIdTocountMap.get(opp.OwnerId);
userIdTocountMap.put(opp.OwnerId, count);
usedIdToPlusMinusMap.put(opp.OwnerId,True);
}else{
userIdTocountMap.put(opp.OwnerId, count);
usedIdToPlusMinusMap.put(opp.OwnerId,True);
}
//previous user to minus count
if(userIdTocountMap.containsKey(oldOppMap.GET(opp.Id).OwnerId)){
count+=userIdTocountMap.get(oldOppMap.GET(opp.Id).OwnerId);
userIdTocountMap.put(oldOppMap.GET(opp.Id).OwnerId, count);
usedIdToPlusMinusMap.put(oldOppMap.GET(opp.Id).OwnerId, FALSE);
}else{
userIdTocountMap.put(oldOppMap.GET(opp.Id).OwnerId, count);
usedIdToPlusMinusMap.put(oldOppMap.GET(opp.Id).OwnerId, FALSE);
}
}
system.debug('userIdTocountMap+32'+userIdTocountMap);
}
System.debug('userIdTocountMap==>'+userIdTocountMap);
List<User> updateUserList = new List<User>();
IF(userIdTocountMap.size()>0){
for(User u : [SELECT Id,LeadAssignCount__c,Daily_Lead__c FROM User WHERE Id in : userIdTocountMap.keySet()]){
decimal leadAssignCountt=0;
decimal dailylead=0;
IF(usedIdToPlusMinusMap.get(u.Id)!=NULL && usedIdToPlusMinusMap.get(u.Id) && userIdTocountMap.GET(U.ID)!=NULL){
leadAssignCountt= ( u.LeadAssignCount__c!=NULL?u.LeadAssignCount__c:0 ) + userIdTocountMap.GET(U.ID);
dailylead= ( u.Daily_Lead__c!=NULL?u.Daily_Lead__c:0 ) + userIdTocountMap.GET(U.ID);
}
else IF(usedIdToPlusMinusMap.get(u.Id)!=NULL && !usedIdToPlusMinusMap.get(u.Id) && userIdTocountMap.GET(U.ID)!=NULL){
leadAssignCountt= ( u.LeadAssignCount__c!=NULL?u.LeadAssignCount__c:0 ) - userIdTocountMap.GET(U.ID);
dailylead= ( u.Daily_Lead__c!=NULL?u.Daily_Lead__c:0 ) - userIdTocountMap.GET(U.ID);
}
updateUserList.add(new User(Id=u.Id, LeadAssignCount__c =leadAssignCountt<0?0:leadAssignCountt, Daily_Lead__c =dailylead<0?0:dailylead));
}
system.debug('updateUserList==>'+updateUserList);
IF(!updateUserList.isEmpty())
UPDATE updateUserList;
}
}
}
public static void OwnerReasonCheck(List<Opportunity> oppList, Map<Id, Opportunity> oldOppMap){
Set<Id> oppIds = new Set<Id>();
set<Id> UserIds = new set<Id>();
List<Ownership_Assignment__c> ownAssList = new List<Ownership_Assignment__c>();
List<User> userlist = new List<user>();
List<User> userexemptlist = new List<user>();
set<ID> userexempt = new set<id>();
userexemptlist = [Select Id,profileid from User where ID =:userinfo.getuserid()];
for(user u:userexemptlist){
if(u.id != System.Label.Barry_Fitts_UserId && u.id != System.Label.Natalie_userId ){
userexempt.add(u.id);
}
}
if(userexempt.size()>0){
for(Opportunity opp: OppList){
if(opp.OwnerId!=oldOppMap.get(opp.Id).OwnerId ){
oppIds.add(opp.Id);
userIds.add(opp.ownerid);
}
}
ownAssList = [Select Id from Ownership_Assignment__c where Appointment_Name__c IN :oppIds];
userlist = [Select Id,In_Out__c,LRT_qualifications__c from User where ID IN :userIds];
for(Opportunity opp: OppList){
for(user us:userlist){
if (ownAssList.isempty()&&opp.OwnerId!=oldOppMap.get(opp.Id).OwnerId
&& !URL.getCurrentRequestUrl().getQuery().contains('IFrameOrigin') && us.In_Out__c == TRUE &&
opp.Service_Request__c == null ){
opp.addError(System.Label.LRT_Validation);
}
else if (ownAssList.isempty()&&opp.LRT_Reason__c==null&&opp.OwnerId!=oldOppMap.get(opp.Id).OwnerId
&& !URL.getCurrentRequestUrl().getQuery().contains('IFrameOrigin') &&
opp.Service_Request__c == null){
opp.addError(System.Label.LRT_Reason);
}
if(!String.isBlank(us.LRT_Qualifications__c)){
List<String> pickValues = us.LRT_Qualifications__c.split(';');
String pickValuesStr ='' ;
for(String str : pickValues){
pickValuesStr = pickValuesStr + '\n' + str;
}
if(opp.LRT_Reason__c==null&&opp.OwnerId!=oldOppMap.get(opp.Id).OwnerId
&& !URL.getCurrentRequestUrl().getQuery().contains('IFrameOrigin') &&(!pickValuesStr.contains(opp.Consultation_Type__c))
&& opp.Service_Request__c == null){
opp.addError('This agent does not serve that consultation type.');
}
}
else{
opp.adderror('This Agent has no approved consultation types at this time.');
//return;
}
}
}
}
}
}
Apex test class:
@isTest
public without sharing class RG_OpportunityTgrCls_Test {
static testMethod void oppTriggerTestMethod(){
Profile p = [Select id from profile where name = 'Listing Agent (Full License)'];
ActionPlansTestUtilities ge = new ActionPlansTestUtilities();
User u = ge.createTestUser(p);
system.runAs(u){
Id RecordTypeIdContact = Schema.SObjectType.Service_Request__c.getRecordTypeInfosByName().get('SOI').getRecordTypeId();
Service_Request__c srq = new Service_Request__c ();
srq.RecordTypeId = RecordTypeIdContact;
srq.First_Name__c = 'srqtest';
srq.Last_Name__c = 'lasttest';
srq.Lead_Type__c = 'Buyer';
srq.Market_Area__c = 'Atlanta';
srq.Primary_Email__c = 'testsrq@test.com';
srq.Primary_Phone__c = '3456789';
insert srq;
Opportunity op = new Opportunity();
op.Name ='Op1';
op.StageName = 'Won';
op.StageName = 'Won';
op.Service_Request__c =srq.id;
op.Conversion_Type__c = 'Inbound';
op.Consultation_Type__c = 'Phone Consultation';
op.Conversion_Type__c = 'Inbound';
op.CloseDate = Date.today();
op.GoogleDrive_Link__c = 'test.test.com';
op.OwnerId = u.Id;
insert op;
Ownership_Assignment__c oa = new Ownership_Assignment__c();
oa.Appointment_Name__c = op.Id;
insert oa;
Test.startTest();
DateTime dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
op.Appointment_time__c = Time.newInstance(18, 0, 0, 0);
op.Market_Area__c = 'Athens';
op.LeadSource ='Billboard';
op.Description ='test test';
op.LRT_Reason__c='test reason';
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
op.ISA_Agent__c =UserInfo.getUserId();
op.CloseDate = myDate;
op.Type ='Buyer';
op.OwnerId=UserInfo.getUserId();
update op;
Test.stopTest();
system.assertEquals(u.Id, op.OwnerId);
}
}
static testMethod void oppTriggerTestMethod2(){
Profile p = [Select id from profile where name = 'System Administrator'];
User u2 = new User(Alias = 'newUser', Email='newuser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Test11ing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='ne11wuser@testorg.com', LRT_qualifications__c ='Phone Consultation');
Opportunity op = new Opportunity();
op.Name ='Op1';
op.StageName = 'Won';
op.StageName = 'Won';
op.Conversion_Type__c = 'Inbound';
op.Consultation_Type__c = 'Phone Consultation';
op.Conversion_Type__c = 'Inbound';
op.CloseDate = Date.today();
op.GoogleDrive_Link__c = 'test.test.com';
op.OwnerId = UserInfo.getUserId();
DateTime dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
insert op;
Ownership_Assignment__c oa = new Ownership_Assignment__c();
oa.Appointment_Name__c = op.Id;
insert oa;
User us = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Test12ing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='systaduser@testorg.com', LRT_qualifications__c ='Phone Consultation');
insert us;
System.runAs(u2) {
Test.startTest();
op.Type ='Buyer';
op.Appointment_time__c = Time.newInstance(18, 0, 0, 0);
op.Market_Area__c = 'Athens';
op.LeadSource ='Billboard';
op.Description ='test test';
op.LRT_Reason__c='test reason';
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
op.ISA_Agent__c =UserInfo.getUserId();
op.CloseDate = myDate;
op.ownerid = us.id;
update op;
us.LeadAssignCount__c = 2;
update us;
Test.stopTest();
}
}
static testMethod void oppTriggerTestMethod3(){
Profile p = [Select id from profile where name = 'Lead Coordinator'];
User u2 = new User(Alias = 'newUser', Email='newuser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Test11ing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='ne11wuser@testorg.com', LRT_qualifications__c ='Phone Consultation');
Opportunity op = new Opportunity();
op.Name ='Op1';
op.StageName = 'Won';
op.StageName = 'Won';
op.Conversion_Type__c = 'Inbound';
op.Consultation_Type__c = 'Phone Consultation';
op.Conversion_Type__c = 'Inbound';
op.CloseDate = Date.today();
op.GoogleDrive_Link__c = 'test.test.com';
op.OwnerId = UserInfo.getUserId();
DateTime dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
insert op;
User us = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Test12ing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='systaduser@testorg.com', LRT_qualifications__c ='Phone Consultation',In_Out__c = True);
insert us;
System.runAs(u2) {
Test.startTest();
ApexPages.StandardController opportunity;
ApexPages.currentPage().getParameters().put('id',op.id);
op.Type ='Buyer';
op.Appointment_time__c = Time.newInstance(18, 0, 0, 0);
op.Market_Area__c = 'Athens';
op.LeadSource ='Billboard';
op.Description ='test test';
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
op.ISA_Agent__c =UserInfo.getUserId();
op.CloseDate = myDate;
op.ownerid = us.id;
update op;
us.LeadAssignCount__c = 2;
update us;
Test.stopTest();
}
}
}
- ravi teja gutha
- March 25, 2020
- Like
- 0
Can Somebody Help me with this Test Class
global class SendNPSbutton {
@AuraEnabled
public static void sendNPSEmail(Id cId)
{
EmailTemplate emiailTemp = new EmailTemplate();
String htmlBody;
String plainBody;
String subject;
List<String> sendTo = new List<String>();
contact cc = [select id from contact limit 1];
Closing__c cpps = [SELECT Id,RecordTypeId,RecordType.Name,Seller_Email__c,Buyer_Email__c,Owner.FirstName,
Buyer_Account__c,Seller_Account__c,Seller_Account__r.Name,Appointment__r.Name
FROM Closing__c where Id=: cId LIMIT 1];
if(cpps.Id != null)
{
if(cpps.RecordType.Name =='Listing Team'){
sendTo.add(cpps.Seller_Email__c);
emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_npsSeller'];
subject = emiailTemp.Subject;
htmlBody= emiailTemp.HtmlValue;
htmlBody = htmlBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__r.name);
htmlBody = htmlBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
plainBody = emiailTemp.Body;
plainBody = plainBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__c);
plainBody = plainBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
}else{
sendTo.add(cpps.Buyer_Email__c);
emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_nps'];
subject = emiailTemp.Subject;
htmlBody= emiailTemp.HtmlValue;
htmlBody = htmlBody.replace('{!Closing__c.Buyer_Account__c}', cpps.Appointment__r.Name);
htmlBody = htmlBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
plainBody = emiailTemp.Body;
plainBody = plainBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__c);
plainBody = plainBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
}
// process the merge fields
// String subject = emailTemplate.Subject;
// subject = subject.replace('{!Contact.FirstName}', c.FirstName);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(emiailTemp.Id);
List<String> ccTo = new List<String>();
ccTo.add('xyz@gomail.com');
mail.setCcAddresses(ccTo);
mail.setReplyTo('xyz@gomail.com');
mail.setSenderDisplayName('XYZ');
mail.setTargetObjectId(cc.id);
mail.setTreatTargetObjectAsRecipient(false);
mail.setWhatId(cpps.Id);
mail.setToAddresses(sendTo);
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setHtmlBody(htmlBody);
mail.setSubject(subject);
mail.setPlainTextBody(plainBody);
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
TEST Class:
@istest(SeeAllData=true)
public class SendNPSButton_test{
static testmethod void testvalidate(){
List<String> sendTo = new List<String>();
Account acc = new Account();
acc.Name = 'Name test';
insert acc;
Closing__c cl = new Closing__c();
//cl.RecordTypeId=Schema.SObjectType.Closing__c.getRecordTypeInfosByName().get('Buying Team').getRecordTypeId();
cl.Buyer_Email__c = 'ravi.7293@gmail.com';
cl.Seller_Email__c ='ravi.7293@gmail.com';
cl.Seller_Account__c = acc.Id;
insert cl;
EmailTemplate emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_npsSeller'];
/*Closing__c cpps = [SELECT Id,RecordTypeId,RecordType.Name,Seller_Email__c,Buyer_Email__c,Owner.FirstName,
Buyer_Account__c,Seller_Account__c,Seller_Account__r.Name,Appointment__r.Name
FROM Closing__c where Id=: cl.Id LIMIT 1];*/
system.debug('cl>>: '+ cl);
Test.startTest();
// Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// mail.setTemplateId(emiailTemp.Id);
//mail.setToAddresses(sendTo);
//Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
system.debug('cl11>>: '+ cl.Seller_Email__c);
system.debug('cID>>: '+ cl.ID);
SendNPSbutton.sendNPSEmail(cl.Id);
//Integer emailInvocations = Limits.getEmailInvocations();
Test.stopTest();
//system.assertEquals(1, emailInvocations, 'An email should be sent');
}
}
- ravi teja gutha
- October 14, 2019
- Like
- 0
Need help with test class for the following Trigger. I was able to cover 71% but could'nt do the rest. Any suggestion on how to cover the Url.get parameter??
trigger RG_OpportunityTgr on Opportunity (after insert, before update) {
if(Trigger.isInsert){
Map<Id, Opportunity> oppMap=new Map<Id, Opportunity>();
RG_OpportunityTgrCls.OpportunityAssignUpdateCount(Trigger.new, oppMap, TRUE, FALSE);
}
IF(Trigger.IsUpdate){
RG_OpportunityTgrCls.OpportunityAssignUpdateCount(Trigger.new, Trigger.oldMap, false, TRUE);
Id userProfileId = userinfo.getProfileId();
Id currentUserId = UserInfo.getUserId();
String userProfileName = [SELECT ID, Name from Profile Where Id = : userProfileId].Name;
if( (userProfileName != 'System Administrator' && currentUserId != System.Label.Natalie_userId
&& currentUserId != System.Label.Barry_Fitts_UserId)){
RG_OpportunityTgrCls.OwnerReasonCheck(Trigger.new, Trigger.oldMap);
}
}
}
APex: class
public class RG_OpportunityTgrCls {
public static void OpportunityAssignUpdateCount(List<Opportunity> oppList, Map<Id, Opportunity> oldOppMap, boolean isInsert, boolean IsUpdate){
Map<Id,Integer> userIdTocountMap = new Map<Id, integer>();
Map<Id, Integer> userIdToCountMinusMap= new Map<Id, Integer>();
Map<Id,Boolean> usedIdToPlusMinusMap = new Map<Id, Boolean>();
for(Opportunity opp: OppList){
if(opp.LeadSource != 'SOI' && opp.T2T_Agent__c == Null){
Integer count = 1;
IF(isInsert){
// IF(userIdTocountMap.containsKey(opp.OwnerId)){
// count+=userIdTocountMap.get(opp.OwnerId);
// userIdTocountMap.put(opp.OwnerId, count);
//}ELSE
userIdTocountMap.put(opp.OwnerId, count);
usedIdToPlusMinusMap.put(opp.OwnerId,True);
}
if(IsUpdate && oldOppMap.GET(opp.Id)!=NULL && opp.OwnerId!=oldOppMap.GET(opp.Id).OwnerId){
//new user to add count
opp.Pullback_Campaign__c = System.today();
if(userIdTocountMap.containsKey(opp.OwnerId)){
count+=userIdTocountMap.get(opp.OwnerId);
userIdTocountMap.put(opp.OwnerId, count);
usedIdToPlusMinusMap.put(opp.OwnerId,True);
}else{
userIdTocountMap.put(opp.OwnerId, count);
usedIdToPlusMinusMap.put(opp.OwnerId,True);
}
//previous user to minus count
if(userIdTocountMap.containsKey(oldOppMap.GET(opp.Id).OwnerId)){
count+=userIdTocountMap.get(oldOppMap.GET(opp.Id).OwnerId);
userIdTocountMap.put(oldOppMap.GET(opp.Id).OwnerId, count);
usedIdToPlusMinusMap.put(oldOppMap.GET(opp.Id).OwnerId, FALSE);
}else{
userIdTocountMap.put(oldOppMap.GET(opp.Id).OwnerId, count);
usedIdToPlusMinusMap.put(oldOppMap.GET(opp.Id).OwnerId, FALSE);
}
}
system.debug('userIdTocountMap+32'+userIdTocountMap);
}
System.debug('userIdTocountMap==>'+userIdTocountMap);
List<User> updateUserList = new List<User>();
IF(userIdTocountMap.size()>0){
for(User u : [SELECT Id,LeadAssignCount__c,Daily_Lead__c FROM User WHERE Id in : userIdTocountMap.keySet()]){
decimal leadAssignCountt=0;
decimal dailylead=0;
IF(usedIdToPlusMinusMap.get(u.Id)!=NULL && usedIdToPlusMinusMap.get(u.Id) && userIdTocountMap.GET(U.ID)!=NULL){
leadAssignCountt= ( u.LeadAssignCount__c!=NULL?u.LeadAssignCount__c:0 ) + userIdTocountMap.GET(U.ID);
dailylead= ( u.Daily_Lead__c!=NULL?u.Daily_Lead__c:0 ) + userIdTocountMap.GET(U.ID);
}
else IF(usedIdToPlusMinusMap.get(u.Id)!=NULL && !usedIdToPlusMinusMap.get(u.Id) && userIdTocountMap.GET(U.ID)!=NULL){
leadAssignCountt= ( u.LeadAssignCount__c!=NULL?u.LeadAssignCount__c:0 ) - userIdTocountMap.GET(U.ID);
dailylead= ( u.Daily_Lead__c!=NULL?u.Daily_Lead__c:0 ) - userIdTocountMap.GET(U.ID);
}
updateUserList.add(new User(Id=u.Id, LeadAssignCount__c =leadAssignCountt<0?0:leadAssignCountt, Daily_Lead__c =dailylead<0?0:dailylead));
}
system.debug('updateUserList==>'+updateUserList);
IF(!updateUserList.isEmpty())
UPDATE updateUserList;
}
}
}
public static void OwnerReasonCheck(List<Opportunity> oppList, Map<Id, Opportunity> oldOppMap){
Set<Id> oppIds = new Set<Id>();
set<Id> UserIds = new set<Id>();
List<Ownership_Assignment__c> ownAssList = new List<Ownership_Assignment__c>();
List<User> userlist = new List<user>();
List<User> userexemptlist = new List<user>();
set<ID> userexempt = new set<id>();
userexemptlist = [Select Id,profileid from User where ID =:userinfo.getuserid()];
for(user u:userexemptlist){
if(u.id != System.Label.Barry_Fitts_UserId && u.id != System.Label.Natalie_userId ){
userexempt.add(u.id);
}
}
if(userexempt.size()>0){
for(Opportunity opp: OppList){
if(opp.OwnerId!=oldOppMap.get(opp.Id).OwnerId ){
oppIds.add(opp.Id);
userIds.add(opp.ownerid);
}
}
ownAssList = [Select Id from Ownership_Assignment__c where Appointment_Name__c IN :oppIds];
userlist = [Select Id,In_Out__c,LRT_qualifications__c from User where ID IN :userIds];
for(Opportunity opp: OppList){
for(user us:userlist){
if (ownAssList.isempty()&&opp.OwnerId!=oldOppMap.get(opp.Id).OwnerId
&& !URL.getCurrentRequestUrl().getQuery().contains('IFrameOrigin') && us.In_Out__c == TRUE &&
opp.Service_Request__c == null ){
opp.addError(System.Label.LRT_Validation);
}
else if (ownAssList.isempty()&&opp.LRT_Reason__c==null&&opp.OwnerId!=oldOppMap.get(opp.Id).OwnerId
&& !URL.getCurrentRequestUrl().getQuery().contains('IFrameOrigin') &&
opp.Service_Request__c == null){
opp.addError(System.Label.LRT_Reason);
}
if(!String.isBlank(us.LRT_Qualifications__c)){
List<String> pickValues = us.LRT_Qualifications__c.split(';');
String pickValuesStr ='' ;
for(String str : pickValues){
pickValuesStr = pickValuesStr + '\n' + str;
}
if(opp.LRT_Reason__c==null&&opp.OwnerId!=oldOppMap.get(opp.Id).OwnerId
&& !URL.getCurrentRequestUrl().getQuery().contains('IFrameOrigin') &&(!pickValuesStr.contains(opp.Consultation_Type__c))
&& opp.Service_Request__c == null){
opp.addError('This agent does not serve that consultation type.');
}
}
else{
opp.adderror('This Agent has no approved consultation types at this time.');
//return;
}
}
}
}
}
}
Apex test class:
@isTest
public without sharing class RG_OpportunityTgrCls_Test {
static testMethod void oppTriggerTestMethod(){
Profile p = [Select id from profile where name = 'Listing Agent (Full License)'];
ActionPlansTestUtilities ge = new ActionPlansTestUtilities();
User u = ge.createTestUser(p);
system.runAs(u){
Id RecordTypeIdContact = Schema.SObjectType.Service_Request__c.getRecordTypeInfosByName().get('SOI').getRecordTypeId();
Service_Request__c srq = new Service_Request__c ();
srq.RecordTypeId = RecordTypeIdContact;
srq.First_Name__c = 'srqtest';
srq.Last_Name__c = 'lasttest';
srq.Lead_Type__c = 'Buyer';
srq.Market_Area__c = 'Atlanta';
srq.Primary_Email__c = 'testsrq@test.com';
srq.Primary_Phone__c = '3456789';
insert srq;
Opportunity op = new Opportunity();
op.Name ='Op1';
op.StageName = 'Won';
op.StageName = 'Won';
op.Service_Request__c =srq.id;
op.Conversion_Type__c = 'Inbound';
op.Consultation_Type__c = 'Phone Consultation';
op.Conversion_Type__c = 'Inbound';
op.CloseDate = Date.today();
op.GoogleDrive_Link__c = 'test.test.com';
op.OwnerId = u.Id;
insert op;
Ownership_Assignment__c oa = new Ownership_Assignment__c();
oa.Appointment_Name__c = op.Id;
insert oa;
Test.startTest();
DateTime dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
op.Appointment_time__c = Time.newInstance(18, 0, 0, 0);
op.Market_Area__c = 'Athens';
op.LeadSource ='Billboard';
op.Description ='test test';
op.LRT_Reason__c='test reason';
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
op.ISA_Agent__c =UserInfo.getUserId();
op.CloseDate = myDate;
op.Type ='Buyer';
op.OwnerId=UserInfo.getUserId();
update op;
Test.stopTest();
system.assertEquals(u.Id, op.OwnerId);
}
}
static testMethod void oppTriggerTestMethod2(){
Profile p = [Select id from profile where name = 'System Administrator'];
User u2 = new User(Alias = 'newUser', Email='newuser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Test11ing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='ne11wuser@testorg.com', LRT_qualifications__c ='Phone Consultation');
Opportunity op = new Opportunity();
op.Name ='Op1';
op.StageName = 'Won';
op.StageName = 'Won';
op.Conversion_Type__c = 'Inbound';
op.Consultation_Type__c = 'Phone Consultation';
op.Conversion_Type__c = 'Inbound';
op.CloseDate = Date.today();
op.GoogleDrive_Link__c = 'test.test.com';
op.OwnerId = UserInfo.getUserId();
DateTime dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
insert op;
Ownership_Assignment__c oa = new Ownership_Assignment__c();
oa.Appointment_Name__c = op.Id;
insert oa;
User us = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Test12ing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='systaduser@testorg.com', LRT_qualifications__c ='Phone Consultation');
insert us;
System.runAs(u2) {
Test.startTest();
op.Type ='Buyer';
op.Appointment_time__c = Time.newInstance(18, 0, 0, 0);
op.Market_Area__c = 'Athens';
op.LeadSource ='Billboard';
op.Description ='test test';
op.LRT_Reason__c='test reason';
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
op.ISA_Agent__c =UserInfo.getUserId();
op.CloseDate = myDate;
op.ownerid = us.id;
update op;
us.LeadAssignCount__c = 2;
update us;
Test.stopTest();
}
}
static testMethod void oppTriggerTestMethod3(){
Profile p = [Select id from profile where name = 'Lead Coordinator'];
User u2 = new User(Alias = 'newUser', Email='newuser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Test11ing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='ne11wuser@testorg.com', LRT_qualifications__c ='Phone Consultation');
Opportunity op = new Opportunity();
op.Name ='Op1';
op.StageName = 'Won';
op.StageName = 'Won';
op.Conversion_Type__c = 'Inbound';
op.Consultation_Type__c = 'Phone Consultation';
op.Conversion_Type__c = 'Inbound';
op.CloseDate = Date.today();
op.GoogleDrive_Link__c = 'test.test.com';
op.OwnerId = UserInfo.getUserId();
DateTime dT = System.now();
Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
insert op;
User us = new User(Alias = 'standt', Email='standarduser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Test12ing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='systaduser@testorg.com', LRT_qualifications__c ='Phone Consultation',In_Out__c = True);
insert us;
System.runAs(u2) {
Test.startTest();
ApexPages.StandardController opportunity;
ApexPages.currentPage().getParameters().put('id',op.id);
op.Type ='Buyer';
op.Appointment_time__c = Time.newInstance(18, 0, 0, 0);
op.Market_Area__c = 'Athens';
op.LeadSource ='Billboard';
op.Description ='test test';
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
op.ISA_Agent__c =UserInfo.getUserId();
op.CloseDate = myDate;
op.ownerid = us.id;
update op;
us.LeadAssignCount__c = 2;
update us;
Test.stopTest();
}
}
}
- ravi teja gutha
- March 25, 2020
- Like
- 0
Can Somebody Help me with this Test Class
global class SendNPSbutton {
@AuraEnabled
public static void sendNPSEmail(Id cId)
{
EmailTemplate emiailTemp = new EmailTemplate();
String htmlBody;
String plainBody;
String subject;
List<String> sendTo = new List<String>();
contact cc = [select id from contact limit 1];
Closing__c cpps = [SELECT Id,RecordTypeId,RecordType.Name,Seller_Email__c,Buyer_Email__c,Owner.FirstName,
Buyer_Account__c,Seller_Account__c,Seller_Account__r.Name,Appointment__r.Name
FROM Closing__c where Id=: cId LIMIT 1];
if(cpps.Id != null)
{
if(cpps.RecordType.Name =='Listing Team'){
sendTo.add(cpps.Seller_Email__c);
emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_npsSeller'];
subject = emiailTemp.Subject;
htmlBody= emiailTemp.HtmlValue;
htmlBody = htmlBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__r.name);
htmlBody = htmlBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
plainBody = emiailTemp.Body;
plainBody = plainBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__c);
plainBody = plainBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
}else{
sendTo.add(cpps.Buyer_Email__c);
emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_nps'];
subject = emiailTemp.Subject;
htmlBody= emiailTemp.HtmlValue;
htmlBody = htmlBody.replace('{!Closing__c.Buyer_Account__c}', cpps.Appointment__r.Name);
htmlBody = htmlBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
plainBody = emiailTemp.Body;
plainBody = plainBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__c);
plainBody = plainBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
}
// process the merge fields
// String subject = emailTemplate.Subject;
// subject = subject.replace('{!Contact.FirstName}', c.FirstName);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(emiailTemp.Id);
List<String> ccTo = new List<String>();
ccTo.add('xyz@gomail.com');
mail.setCcAddresses(ccTo);
mail.setReplyTo('xyz@gomail.com');
mail.setSenderDisplayName('XYZ');
mail.setTargetObjectId(cc.id);
mail.setTreatTargetObjectAsRecipient(false);
mail.setWhatId(cpps.Id);
mail.setToAddresses(sendTo);
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setHtmlBody(htmlBody);
mail.setSubject(subject);
mail.setPlainTextBody(plainBody);
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
TEST Class:
@istest(SeeAllData=true)
public class SendNPSButton_test{
static testmethod void testvalidate(){
List<String> sendTo = new List<String>();
Account acc = new Account();
acc.Name = 'Name test';
insert acc;
Closing__c cl = new Closing__c();
//cl.RecordTypeId=Schema.SObjectType.Closing__c.getRecordTypeInfosByName().get('Buying Team').getRecordTypeId();
cl.Buyer_Email__c = 'ravi.7293@gmail.com';
cl.Seller_Email__c ='ravi.7293@gmail.com';
cl.Seller_Account__c = acc.Id;
insert cl;
EmailTemplate emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_npsSeller'];
/*Closing__c cpps = [SELECT Id,RecordTypeId,RecordType.Name,Seller_Email__c,Buyer_Email__c,Owner.FirstName,
Buyer_Account__c,Seller_Account__c,Seller_Account__r.Name,Appointment__r.Name
FROM Closing__c where Id=: cl.Id LIMIT 1];*/
system.debug('cl>>: '+ cl);
Test.startTest();
// Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
// mail.setTemplateId(emiailTemp.Id);
//mail.setToAddresses(sendTo);
//Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
system.debug('cl11>>: '+ cl.Seller_Email__c);
system.debug('cID>>: '+ cl.ID);
SendNPSbutton.sendNPSEmail(cl.Id);
//Integer emailInvocations = Limits.getEmailInvocations();
Test.stopTest();
//system.assertEquals(1, emailInvocations, 'An email should be sent');
}
}
- ravi teja gutha
- October 14, 2019
- Like
- 0