Skip to content

Instantly share code, notes, and snippets.

@emoran
Last active July 29, 2019 14:23
Show Gist options
  • Save emoran/9074450 to your computer and use it in GitHub Desktop.
Save emoran/9074450 to your computer and use it in GitHub Desktop.
Salesforce TestMethod for PriceBook and PriceBookEntry
/**
* This class contains unit tests for validating the behavior of Apex classes
* and triggers.
*
* Unit tests are class methods that verify whether a particular piece
* of code is working properly. Unit test methods take no arguments,
* commit no data to the database, and are flagged with the testMethod
* keyword in the method definition.
*
* All test methods in an organization are executed whenever Apex code is deployed
* to a production organization to confirm correctness, ensure code
* coverage, and prevent regressions. All Apex classes are
* required to have at least 75% code coverage in order to be deployed
* to a production organization. In addition, all triggers must have some code coverage.
*
* The @isTest class annotation indicates this class only contains test
* methods. Classes defined with the @isTest annotation do not count against
* the organization size limit for all Apex scripts.
*
* See the Apex Language Reference for more information about Testing and Code Coverage.
*/
@isTest (seeAllData=true)
private class test {
static testMethod void myUnitTest() {
// TO DO: implement unit test
Account cuenta = new Account();
cuenta.Name='Test1';
cuenta.Razon_social__c='abc';
cuenta.CurrencyIsoCode = 'MXN';
insert cuenta;
Account cuenta2 = new Account();
cuenta2.Name='Test1';
cuenta2.Razon_social__c='abc';
cuenta2.CurrencyIsoCode = 'MXN';
cuenta2.RecordTypeId='012G0000001GmaO';
insert cuenta2;
Opportunity oportunidadNueva = new Opportunity();
oportunidadNueva.Name = 'TEst';
oportunidadNueva.StageName='Contratada';
oportunidadNueva.CloseDate=Date.today();
oportunidadNueva.CurrencyIsoCode='MXN';
oportunidadNueva.AccountId = cuenta.Id;
insert oportunidadNueva;
Product2 producto =new Product2();
producto.Name='test';
producto.productCode='1234';
producto.isActive = true;
producto.Proveedor__c = cuenta2.Id;
producto.CurrencyIsoCode='MXN';
insert producto;
Pricebook2 standard = [Select Id, Name, IsActive From Pricebook2 where IsStandard = true LIMIT 1];
PriceBook2 pb2=new PriceBook2();
pb2.Name = 'test';
pb2.IsActive = true;
pb2.CurrencyIsoCode='MXN';
insert pb2;
PricebookEntry pbe = createPricebookEntry(standard,pb2,producto);
OpportunityLineItem item=new OpportunityLineItem();
item.OpportunityId = oportunidadNueva.Id;
item.PricebookEntryId = pbe.Id;
item.Quantity = 2;
item.TotalPrice = 10.0;
insert item;
}
public static PricebookEntry createPricebookEntry (Pricebook2 standard, Pricebook2 newPricebook, Product2 prod) {
System.debug('***** starting one');
PricebookEntry one = new PricebookEntry();
one.pricebook2Id = standard.id;
one.product2id = prod.id;
one.unitprice = 1249.0;
one.isactive = true;
insert one;
System.debug('***** one complete, ret next');
PricebookEntry ret = new PricebookEntry();
ret.pricebook2Id = newPricebook.id;
ret.product2id = prod.id;
ret.unitprice = 1250.0;
ret.isactive = true;
insert ret;
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment