function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chris Leszka 5Chris Leszka 5 

Moving a trigger to Production

I created a trigger in my sandbox that creates an Asset for all renewable products in closedwon opportunity.  I'm not a developer in any way, so my process for building the trigger was to search the web for some code that was close to what I needed, and modify it to match my needs.  That said, the trigger works perfectly in the sandbox, but when i try to move it to production, it won't let me deploy it because it fails the test classes.  I don't have any test classes built specific to this trigger, and don't really know how to/where to even begin, so i'm hoping someone here can help me.

Here is my trigger code:
trigger CreateAssetonClosedWon on Opportunity (after update) {
map<id,map<id,asset>> assets = new map<id,map<id,asset>>();
  set<id> prodids = new set<id>();
  opportunitylineitem[] lines = [select id, totalprice, servicedate, service_end_date__c, quantity, pricebookentry.product2id, pricebookentry.product2.name, description, opportunityid from opportunitylineitem where opportunityid in :trigger.new and product2.business_group__c = 'TECH' and opportunity.StageName = 'Closed - Booked' and converted_to_asset__c = false];
  for(opportunitylineitem line:lines)
    prodids.add(line.pricebookentry.product2id);
  for(opportunity record:trigger.new)
    assets.put(record.accountid,new map<id,asset>());
  for(asset record:[select id, accountid, product2id, quantity, price, purchasedate, status, installdate, UsageEndDate, description, name from asset where accountid in :assets.keyset() and product2id in :prodids]) {
    assets.get(record.accountid).put(record.product2id,record);
    if(record.quantity==null)
      record.quantity=0;
    if(record.price==null)
      record.price=0;
  }
  for(opportunitylineitem line:lines) {
    opportunity lineopp = trigger.newmap.get(line.opportunityid);
    asset ast = assets.get(lineopp.accountid).get(line.pricebookentry.product2id);
    if(ast==null)
      ast = new asset(quantity=0,price=0);
    ast.accountid = lineopp.accountid;
    ast.product2id = line.pricebookentry.product2id;
    ast.quantity += line.quantity;
    ast.price += line.TotalPrice;
    ast.purchasedate = lineopp.closedate;
    ast.status = 'Purchased';
    ast.description = line.description;
    ast.installdate = line.servicedate;
    ast.usageenddate = line.service_end_date__c;
    ast.source_opportunity__c = lineopp.id;
    ast.name = line.pricebookentry.product2.name;
    assets.get(lineopp.accountid).put(line.pricebookentry.product2id,ast);
  }
  asset[] updates = new asset[0];
  for(id accountid:assets.keyset())
    updates.addall(assets.get(accountid).values());
  upsert updates;
  for(opportunitylineitem line:lines)
    line.converted_to_asset__c = true;
  update lines;
}

 
GauravGargGauravGarg

Hi Chris,

To cover above trigger, you need to create Test_data for following objects:
1. Account.
2. Product
3. PricebookEntry
4. Opportunity
5. OpportunityLineItem

Hope this will help you, let me know if you need more assistance on this.

Thanks,

Gaurav
Email: gauravgarg.nmims@gmail.com

Chris Leszka 5Chris Leszka 5
Thanks for outlining the specific areas that will need test data, Guarav! Is there an easy template for each of the test cases that i could use to create the test data - something where i just need to update object/field names? I literally have zero experience in writing code on my own, so creating test cases for the code found is way over my head. Thanks again!
GauravGargGauravGarg
Hi Chris,

Please find below sample create test data:
@isTest
public class PriceBookTest {
    static testmethod void addPricebookEntries() {
        Product2 prod = new Product2(Name = 'Test Product', 
            Family = 'Hardware',business_group__c ='TECH');
        insert prod;
        
        // Get standard price book ID.
        // This is available irrespective of the state of SeeAllData.
        Id pricebookId = Test.getStandardPricebookId();
        
        // 1. Insert a price book entry for the standard price book.
        // Standard price book entries require the standard price book ID we got earlier.
        PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        // Create a custom price book
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        // 2. Insert a price book entry with a custom price.
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
        // Next, perform some tests with your test price book entries.
		Account acc = new Account(name='Test Account');
		insert acc;
		
		Asset a = new Asset(accountid=acc.id,product2id=prod.id,quantity='10' ,price='1000', purchasedate=system.today(), status='New', installdate=System.today(), UsageEndDate=System.today().addMonths(4), description='Test Asset', name='New Test Asset')
		insert a;
		
		opportunity opp = new Opportunity();
		opp.name = 'Test New Opporunity';
		opp.closeDate = System.Today();
		opp.stageName = 'Closed - Booked';
		insert opp;
		
		OpportunityLineItem oli = new OpportunityLineItem();
		oli.totalprice = '100000';
		oli.servicedate = system.today();
		oli.service_end_date__c = system.today().addMonths(10);
		oli.quantity = 10;
		oli.pricebookentry = customPrice.id;
		oli.description = 'Test OLI Description';
		oliopportunityid = opp.id;
		converted_to_asset__c = false;
		
		insert oli;
		
    }
}


Let me know if you face issues in this. 
 

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com