+ Start a Discussion
Dev2IndiaDev2India 

Help in Test class

trigger createSplitCommissionOnOpportunityX on Opportunity (after insert, after update) {
   
    List<Split_Commissions__c> SCToInsert = new List<Split_Commissions__c>  ();
  
    for (Opportunity o : Trigger.new) {
       
      
    if ( ((Trigger.isInsert) ||
          (Trigger.isUpdate) && (Trigger.oldMap.get(o.id).StageName !=  'Closed Won')) &&
         (o.StageName == 'Closed Won') )
    {
        Split_Commissions__c SC = new Split_Commissions__c ();        
       
        sc.Opportunity_Name__c = o.id;
       
        SCToInsert.add(sc);
       
       
        }//end if
       
    }//end for o
   
    //once loop is done, you need to insert new records in SF
    // dml operations might cause an error, so you need to catch it with try/catch block.
    try {
        insert SCToInsert;
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

PLease help us to create the test class for the above code.


Thanks,
Best Answer chosen by Dev2India
NK@BITNK@BIT
Try this one....

@isTest
public class createSplitCommissionOnOpportunityXTest{

    public static testMethod void createSplitCommissionOnOpportunityXTest(){
       
        Opportunity o=new Opportunity();
        o.Name='Test Opportunity';
        o.CloseDate=System.Today();
        o.StageName='Closed Won';
       
        insert o;
       
        Split_Commissions__c s=new Split_Commissions__c();
        s.Opportunity_Name__c=o.id;
        insert s;
    }
}

All Answers

Sonam_SFDCSonam_SFDC
I understand that you are trying to create a split commision if an opportunity is closed Won- 

In the trigger you should create a new opportunity and set the stage to Closed Won 
Once created , give a name to the split commision(in the code) and query to see if it created.

This should test the working of the trigger - code sample available on the following link :
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_test.htm

I'd suggest you to start coding and you can post here f yu face any issues.
Dev2IndiaDev2India
I was trying to create Acount first then tried to associate the Opportunity , however not able to insert the Account without no reason. Getting error
Invalid constructor syntax, name=value pairs can only be used for SObjects

Someone please help to wirte the test class for the same.

Thanks and appreciate for the help.......
Dev2IndiaDev2India
I was trying to create Acount first then tried to associate the Opportunity , however not able to insert the Account without no reason. Getting error
Invalid constructor syntax, name=value pairs can only be used for SObjects

Someone please help to wirte the test class for the same.

Thanks and appreciate for the help.......
NK@BITNK@BIT
Try this one....

@isTest
public class createSplitCommissionOnOpportunityXTest{

    public static testMethod void createSplitCommissionOnOpportunityXTest(){
       
        Opportunity o=new Opportunity();
        o.Name='Test Opportunity';
        o.CloseDate=System.Today();
        o.StageName='Closed Won';
       
        insert o;
       
        Split_Commissions__c s=new Split_Commissions__c();
        s.Opportunity_Name__c=o.id;
        insert s;
    }
}
This was selected as the best answer
Dev2IndiaDev2India
I tried with same code you mentioned before but its covering just 36 % ....
NK@BITNK@BIT
In my org, 88% code coverage of same test class.. I don't know why it's not covering in your org..
Dev2IndiaDev2India
Yes, even I am getting 100 % in my Dev Org. But not in Sandbox. I am not sure if this is a refresh issues or something.

Thanks for the help.