• hlsimoneau
  • NEWBIE
  • 10 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hello,
I've written a trigger that updates an opportunity to Closed Won and sets a value (NetSuite Sales Order ID) in a custom opp field based on a related Sales Order case being updated with certain values (NetSuite Sales Order ID) and the case being closed. The trigger works great and updates values on the Opp as expected, but I'm either facing errors in my test class or running into other code/validations because I'm setting the opp to Closed Won. If it's the former, I'd appreciate any help or suggestions on how better to structure my test class and/or my trigger. If it's the later, is there anything I can put in my code to see what other code or validations are causing the problem?

I continue to get errors like this when I run the test class:
System.AssertException: Assertion Failed: Expected: Pending Sales Order, Actual: Closed Won
OR (if I comment out the line causing the above issue)
System.AssertException: Assertion Failed: Expected: 12345, Actual: null

Here's my trigger:
trigger SalesOrderUpdateStageToClosedWon on Case (after Update) {
List<Opportunity> oppList=new List <Opportunity>();
set<id> oppIds = new Set<id>();
RecordType rt = [SELECT Id,Name,SobjectType FROM RecordType WHERE Name = 'Sales Order Case' AND SobjectType = 'Case' LIMIT 1];

for(case c:trigger.new)
{
oppIds.add(c.Opportunity__c);
}
Map<id,Opportunity>oppMap=new Map<id,Opportunity>([Select Id from Opportunity 
Where id in :oppIds]);
List<case> myCases = [select ID from case where ID in :oppIds]; 
Set<ID> myOppsIDs = new Set<ID>();

for (case c : myCases)
{
myOppsIDs.add(c.Opportunity__c);
}
for (case c :trigger.new)
{
if(c.RecordTypeId==rt.Id&&c.NetSuite_Sales_Order_ID__c <> null&&c.Status=='Closed'){
opportunity opp=oppMap.get(c.Opportunity__c);
opp.StageName='Closed Won';   
opp.NetSuite_Sales_Order_ID__c=c.NetSuite_Sales_Order_ID__c;       
oppList.add(opp);
}


}

update oppList;
}


And here's my test class:
@isTest (SeeAllData=True)
public class SalesOrderUpdateStageToClosedWon_Test{

    
    static testMethod void testOppClosedWon(){ 
    test.startTest();  
    //Create Account and Opportunity Record
    Date dToday = Date.today();
    Account acc = new Account(Name = 'Test Account');
    insert acc;
    Opportunity oppRec = new Opportunity (Name = 'Test Opportunity', AccountId = acc.Id, StageName='Pending Sales Order', 
    CloseDate=dTODAY,Ship_Date__c=dTODAY+1,Customer_Notes_Completed__c=true,Approval_Status__c='Approved',
                                          Number_of_Locations__c = 1,Renewal_Term_months__c=1,RecordTypeID='012R0000000DBjt');
     insert oppRec;
        
    //Create Case
  
    
   
    
    Case caseRec = new Case(Opportunity__c = oppRec.Id, POS_Install_Date__c=Date.newInstance(2014,12,31),RecordTypeID='012R00000004y6K',Status='Closed',NetSuite_Sales_Order_ID__c='12345');
    system.debug('About to insert Case' + caseRec);
    insert caseRec;
    
    
    System.Debug('Value of Insert Opp ID ------------------------>' + oppRec.Id);
    System.Debug('Value of Insert Opp StageName ------------------------>' + oppRec.StageName);   
    
    Case testCase = [Select Opportunity__c,NetSuite_Sales_Order_ID__c from Case where Id =: caseRec.Id];
    Opportunity testOpp = [Select Id,StageName, CloseDate,NetSuite_Sales_Order_ID__c from Opportunity where Id =: oppRec.Id];
    
    //Id caseRecId = caseRec.OpportunityId;
    System.assertEquals(testOpp.Id, testCase.Opportunity__c);
    System.assertEquals(testOpp.StageName,'Closed Won');
    System.assertEquals(testCase.NetSuite_Sales_Order_ID__c,testOpp.NetSuite_Sales_Order_ID__c);
    test.stopTest();
    }
    
}



Hello,
I've written a trigger that updates an opportunity to Closed Won and sets a value (NetSuite Sales Order ID) in a custom opp field based on a related Sales Order case being updated with certain values (NetSuite Sales Order ID) and the case being closed. The trigger works great and updates values on the Opp as expected, but I'm either facing errors in my test class or running into other code/validations because I'm setting the opp to Closed Won. If it's the former, I'd appreciate any help or suggestions on how better to structure my test class and/or my trigger. If it's the later, is there anything I can put in my code to see what other code or validations are causing the problem?

I continue to get errors like this when I run the test class:
System.AssertException: Assertion Failed: Expected: Pending Sales Order, Actual: Closed Won
OR (if I comment out the line causing the above issue)
System.AssertException: Assertion Failed: Expected: 12345, Actual: null

Here's my trigger:
trigger SalesOrderUpdateStageToClosedWon on Case (after Update) {
List<Opportunity> oppList=new List <Opportunity>();
set<id> oppIds = new Set<id>();
RecordType rt = [SELECT Id,Name,SobjectType FROM RecordType WHERE Name = 'Sales Order Case' AND SobjectType = 'Case' LIMIT 1];

for(case c:trigger.new)
{
oppIds.add(c.Opportunity__c);
}
Map<id,Opportunity>oppMap=new Map<id,Opportunity>([Select Id from Opportunity 
Where id in :oppIds]);
List<case> myCases = [select ID from case where ID in :oppIds]; 
Set<ID> myOppsIDs = new Set<ID>();

for (case c : myCases)
{
myOppsIDs.add(c.Opportunity__c);
}
for (case c :trigger.new)
{
if(c.RecordTypeId==rt.Id&&c.NetSuite_Sales_Order_ID__c <> null&&c.Status=='Closed'){
opportunity opp=oppMap.get(c.Opportunity__c);
opp.StageName='Closed Won';   
opp.NetSuite_Sales_Order_ID__c=c.NetSuite_Sales_Order_ID__c;       
oppList.add(opp);
}


}

update oppList;
}


And here's my test class:
@isTest (SeeAllData=True)
public class SalesOrderUpdateStageToClosedWon_Test{

    
    static testMethod void testOppClosedWon(){ 
    test.startTest();  
    //Create Account and Opportunity Record
    Date dToday = Date.today();
    Account acc = new Account(Name = 'Test Account');
    insert acc;
    Opportunity oppRec = new Opportunity (Name = 'Test Opportunity', AccountId = acc.Id, StageName='Pending Sales Order', 
    CloseDate=dTODAY,Ship_Date__c=dTODAY+1,Customer_Notes_Completed__c=true,Approval_Status__c='Approved',
                                          Number_of_Locations__c = 1,Renewal_Term_months__c=1,RecordTypeID='012R0000000DBjt');
     insert oppRec;
        
    //Create Case
  
    
   
    
    Case caseRec = new Case(Opportunity__c = oppRec.Id, POS_Install_Date__c=Date.newInstance(2014,12,31),RecordTypeID='012R00000004y6K',Status='Closed',NetSuite_Sales_Order_ID__c='12345');
    system.debug('About to insert Case' + caseRec);
    insert caseRec;
    
    
    System.Debug('Value of Insert Opp ID ------------------------>' + oppRec.Id);
    System.Debug('Value of Insert Opp StageName ------------------------>' + oppRec.StageName);   
    
    Case testCase = [Select Opportunity__c,NetSuite_Sales_Order_ID__c from Case where Id =: caseRec.Id];
    Opportunity testOpp = [Select Id,StageName, CloseDate,NetSuite_Sales_Order_ID__c from Opportunity where Id =: oppRec.Id];
    
    //Id caseRecId = caseRec.OpportunityId;
    System.assertEquals(testOpp.Id, testCase.Opportunity__c);
    System.assertEquals(testOpp.StageName,'Closed Won');
    System.assertEquals(testCase.NetSuite_Sales_Order_ID__c,testOpp.NetSuite_Sales_Order_ID__c);
    test.stopTest();
    }
    
}