+ Start a Discussion
andersalandersal 

Problem with test trigger

Hi!

 

I have problems with testing the following trigger when I tries do deploy it to the server from eclipse. And in Eclipse it is an yellow triangle next to the trigger which says it's only on local machine, not on the server. Is there anybody who can help me. The error I got when I run the test is that from the if clause is never tested... I wonder what's wrong.

 

TRIGGER:

 

trigger CreateAssetOnDoneItem on OpportunityLineItem (after insert,after update) {
  List<OpportunityLineItem> list_oli = new List<OpportunityLineItem>();
  //Setup the array to hold the ids to Iterate through
   Set<Id> pbeIds = new Set<Id>();
  //Iterate through the Line Items
    for (OpportunityLineItem oli : Trigger.new)
    {   
       // Create individual post
       pbeIds.add(oli.PricebookEntryId);
       pbeIds.add(oli.opportunityId);
    }
     //Setup APEX MAP Arrays to get the required data from the Pricebook and Opportunity
    Map<Id, PricebookEntry> entries = new Map<Id, PricebookEntry>([select Product2.ProductCode,Product2.Name from PricebookEntry where id in :pbeIds]);
    Map<Id, Opportunity> Opp = new Map<Id, Opportunity>([select Account.Name, Account.ID from Opportunity where id in :pbeIds]);
    

      Asset[] ast = new Asset[]{};
         Asset a = new Asset();
         Integer teller = 0;
      for(OpportunityLineItem oli: trigger.new){
               
        if(oli.Status__c == 'Godkjent hos leverandør'  && oli.Converted_to_Asset__c == false)
         {
          a = new Asset();
          a.AccountId = opp.get(oli.OpportunityId).AccountId;
          //a.Product2Id = opp.get(oli.Pricebookentryid).PricebookEntry.Product2Id;
          a.Product2Id = oli.PricebookEntry.Product2ID;
          //a.Product2.Name= oli.PricebookEntry.Product2.Name;
          //a.Product2 = oli.PricebookEntry.Product2Id;
          a.Quantity = oli.Quantity;
          a.Opprinnelig_sum__c = a.Quantity;
          //a.Price =  ol.UnitPrice;
          a.PurchaseDate = oli.Opportunity.CloseDate;
          a.Status = 'Mangler provisjon';
          //a.Description = ol.Description;
          a.Provisjon__c = oli.Provisjon__c;
          System.debug(entries.get(oli.Pricebookentryid).Product2.Name);
          a.Name = entries.get(oli.Pricebookentryid).Product2.Name;
          ast.add(a);
          oli.Converted_to_Asset__c = true;
          //update oli;
          }  
    }
       
 
      insert ast;
     
    
}

 

 

TESTCLASS:

 

@isTest
private class testCreateAssetOnDoneItem {

    static testMethod void testCreateAssetOnDoneItem(){
        
        Account a = [select Id from Account limit 1];
        PricebookEntry pbID = [select Id from PricebookEntry limit 1];
        Opportunity o = new Opportunity();
        OpportunityLineItem ol = new OpportunityLineItem();
        
        o.AccountId = a.Id;
        o.Name = 'test';
        o.StageName = 'Presentere mulighet';
        o.CloseDate = date.today();
        insert o;
        
        ol.OpportunityId = o.Id;
        ol.Quantity = 1;
        ol.UnitPrice = 1.00;
        ol.PricebookEntryId = pbId.Id;
        ol.Converted_to_Asset__c = false;
        
        insert ol;
        test.start();
        ol.Status__c= 'Aksetptert salg';
        update o;
        test.stopTest();
        delete ol;
        delete o;
        
        
        
    }
    
    
}

SennahSennah

Andersal,

 

to which instance is your eclipse connected?

Production or Sandbox?

 

You should always code in your Sandbox.

You don't need code coverage in your Sandbox. Only when you are going to deploy to Production.

 

When eclipse shows this 'triangle' it will also tell you why it has not saved to the Server.

 

Once everything is saved to the server you should run your Apex Test in the Browser and understand the log output.

 

Add a lot of System.Debug(); to your code to print variables often and also before and after each if to better understand if you are going through the code.

 

Good Luck

//Hannes

andersalandersal

Hi!

 

I am connected to Production...  I can try to connect to Sandbox instead. But I know it should works, because I have wrote the trigger in the sandbox, and it works there.

 

Do you say that I should create a new project in Eclipse, and connect to Sandbox. And deploy it there and run tests in browser. Can I deploy from Sandbox to  Production?

Jeremy.NottinghJeremy.Nottingh

It's not enough to know that the Trigger works.

 

The other poster is talking about having Apex unit tests set up in a separate Apex class that will cover most of the lines of code. This is different from using the browser GUI to manually give it test cases. If you haven't written any unit tests, then you will not be able to put the Trigger into production, even if it's working in your sandbox.

 

For help on setting up unit tests, see Apex Developers Guide. If you run into a snag with writing your tests, come back and post your code, and we'll help!

 

Jeremy

andersalandersal

Thanks for the comments. I have managed to solve my problems now.