You need to sign in to do that
Don't have an account?
sudhIr Narayan
Not able to deploy 100% code coverage to production
Hi,
Created a custom object in fullcopy with name Temp_Assets__c have created few formula fields on this object created a trigger on this object to insert data into this object also wrote a test class on this trigger which is 100% code coverage
Problem am facing is am not able to deploy this to production i get error
Created a custom object in fullcopy with name Temp_Assets__c have created few formula fields on this object created a trigger on this object to insert data into this object also wrote a test class on this trigger which is 100% code coverage
Problem am facing is am not able to deploy this to production i get error
trigger Temp_Asset2_Opp on Temp_Assets__c (after insert ) { List<OpportunityLineItem> OppLineItems = new List<OpportunityLineItem>(); List<Temp_Assets__c> CurrentAsset = [Select Id, Name, PriceBook_Name__c, Asset_Id__c, Serial_Number__c, AccountId__c, Product__c, Service_Start_Date__c, Service_End_Date__c ,Install_Date__c ,Reseller__c,Distributor__c, Incumbent_Reseller__c,Education__c,Expiry_Date__c,Existing_Opportunity__c, New_Opportunity__c,Expiry_Term__c,Bundle_Support__c, X5_Year_SKU_Code__c, X3_Year_SKU_Code__c, X1_Year_SKU_Code__c, Monthly_SKU_Code__c, X5_year_SKU__c ,X3_year_SKU__c, X1_year_SKU__c, Total_in_Months__c, Support_Only_5_Year_SKU__c, Support_Only_3_Year_SKU__c, Support_Only_1_Year_SKU__c, Support_Only_Monthly_SKU__c, DaysRemaining_Months__c, Monthly_Quote__c, Total_Monthly_Quote_term__c From Temp_Assets__c Where CreatedById = :userinfo.getUserId()]; List<Temp_Assets__c> CurrentAsset1 = [Select Id, Name, PriceBook_Name__c, Opp_Close_Date__c, Asset_Id__c, Serial_Number__c, AccountId__c, Product__c, Service_Start_Date__c, Service_End_Date__c ,Install_Date__c ,Reseller__c,Distributor__c , Incumbent_Reseller__c,Education__c,Expiry_Date__c,Existing_Opportunity__c, New_Opportunity__c,Expiry_Term__c,Bundle_Support__c, X5_Year_SKU_Code__c, X3_Year_SKU_Code__c, X1_Year_SKU_Code__c, Monthly_SKU_Code__c, X5_year_SKU__c ,X3_year_SKU__c, X1_year_SKU__c, Total_in_Months__c, Support_Only_5_Year_SKU__c, Support_Only_3_Year_SKU__c, Support_Only_1_Year_SKU__c, Support_Only_Monthly_SKU__c, DaysRemaining_Months__c From Temp_Assets__c Where CreatedById = :userinfo.getUserId() Limit 1]; //Pricebook2 prBook1= [select id from Pricebook2 where Name=: 'NAM Price Book']; RecordType renewal_RT = [Select id, Name, sObjectType from RecordType where sObjectType= 'Opportunity' and Name = 'Renewal Opportunity' ]; set<id> existingOppId= new Set<id>(); List<Opportunity> opport = new List<Opportunity>(); set<id> newOppId = new set<id>(); //String variable to store the name of the pricebook for this Opportunity String priceBookName; for (Temp_Assets__c temp: currentAsset1) { existingOppId.add(temp.Existing_Opportunity__c); priceBookName = temp.priceBook_Name__c; } Map<Id, String> productNameToId = new Map<Id, String>(); //Get the Pricebook2 Id Pricebook2 prBook1= [select id, Name from Pricebook2 where Name =: priceBookName]; System.debug('=== pricebook is: '+ prbook1); List<Opportunity> OpptUp = [select Id from Opportunity where id IN:existingOppId ]; for ( Temp_Assets__c TA : currentAsset1) { // If New Opportunity is Created if ( TA.Existing_Opportunity__c == NULL && TA.New_Opportunity__c != NULL ) { // Insert Opportunity with all mandatory fields Opportunity Opp = new Opportunity(); Opp.Name = TA.New_Opportunity__c; Opp.Type = 'Existing Customer'; Opp.AccountId = TA.AccountId__c; //Opp.CloseDate = TA.Expiry_Date__c; // default Sysdate + 30 opp.closeDate = TA.Opp_close_Date__c; opp.Support_Expiry_Date__c = TA.Expiry_Date__c; //new addition Opp.Government_Contract__c = 'None'; Opp.Renewal_Opportunity__c = 'Yes'; Opp.StageName = 'Renewal'; //Opp.Lost_Reason__c = 'Other'; // not required Opp.Primary_Competitor__c = 'No Competitor'; Opp.ForecastCategoryName = 'Pipeline'; Opp.LeadSource = 'Renewal'; Opp.Primary_Reseller__c = TA.Reseller__c; Opp.Primary_Distributor__c = TA.Distributor__c; Opp.Renewal_Incumbant_Reseller__c = TA.Incumbent_Reseller__c; Opp.Renewal_K_12__c = TA.Education__c; Opp.Opportunity_Driver__c ='Renewals'; //Assigning the new Opportunity to the Renewal Record Type opp.recordtypeId = renewal_RT.id; opport.add(opp); } else if ( TA.Existing_Opportunity__c != NULL && TA.New_Opportunity__c == NULL ) { // List<Opportunity> OpptUp = [select Id from Opportunity where id = :TA.Existing_Opportunity__c ]; for ( Opportunity OppUpd : OpptUp ) { OppUpd.Primary_Reseller__c = TA.Reseller__c; OppUpd.Primary_Distributor__c = TA.Distributor__c; OppUpd.Renewal_Incumbant_Reseller__c = TA.Incumbent_Reseller__c; OppUpd.Renewal_K_12__c = TA.Education__c; OppUpd.Support_Expiry_Date__c = TA.Expiry_Date__c; //new addition //change the RecordType of this existing Opportunity to Renewal Opportunity Record Type OppUpd.recordtypeId = renewal_RT.id; OppUpd.Opportunity_Driver__c ='Renewals'; Opport.add(OppUpd); } } } if (Opport.size() > 0) { upsert Opport; } // for (Opportunity Opport1: Opport) { for ( Temp_Assets__c TA : CurrentAsset ) { for (Opportunity Opport1: Opport) { //Grab the Service End Date Value of this Asset Date currentServiceDate = TA.Service_End_Date__c; /************New condition added below*********************************************/ //if Monthly Quote is not selected by the user then it will calculate all the SKU's // if (TA.Monthly_Quote__c == false) { //Check if the Renewals need 5 years renewals if (TA.X5_year_SKU__c > 0 && (TA.Support_Only_5_Year_SKU__c!=null || TA.X5_Year_SKU_Code__c!=null)) { //Insert into Opportunity Lines OpportunityLineItem OppL_5yr = new OpportunityLineItem(); OppL_5yr.OpportunityId = Opport1.Id; //check if the Renew is for Bundle Support or Support only if (TA.Bundle_Support__c == true && TA.X5_Year_SKU_Code__c!= null) { System.debug('#### Checkpoint #1'); //Retrieve the priceBookEntryId for Bundle support 5 years Renewals SKU Code OppL_5yr.PricebookEntryId = currentPriceBook(TA.X5_Year_SKU_Code__c ); System.debug('#### Checkpoint #2'); //OppL_5yr.PricebookEntryId = [Select id from PriceBookEntry where product2.Name =:TA.X5_Year_SKU_Code__c AND priceBook2Id=:prBook.id].Id; } else if (TA.Bundle_Support__c != true && TA.Support_Only_5_Year_SKU__c!= null) { //Retrieve the priceBookEntryId for CO or SO support 5 years Renewals SKU Code System.debug('#### Checkpoint #1.1'); OppL_5yr.PricebookEntryId = currentPriceBook(TA.Support_Only_5_Year_SKU__c); //OppL_5yr.PricebookEntryId = [Select id from PriceBookEntry where product2.Name =:TA.Support_Only_5_Year_SKU__c AND priceBook2Id=:prBook.id].Id; System.debug('#### Checkpoint #2.2'); } //OppL_5yr.PricebookEntryId = prBookEntry.Id; OppL_5yr.Quantity = TA.X5_year_SKU__c; OppL_5yr.Start_Date__c= currentServiceDate.addDays(1); OppL_5yr.End_Date__c = currentServiceDate.addYears(Integer.valueOf(5* TA.X5_year_SKU__c)); OppL_5yr.Asset__c = TA.Asset_Id__c; currentServiceDate = currentServiceDate.addYears(Integer.valueOf(5* TA.X5_year_SKU__c)); //Adding to the list OppLineItems.add(OppL_5yr); //Insert OppL_5yr; } if (TA.X3_year_SKU__c > 0 && (TA.Support_Only_3_Year_SKU__c!=null || TA.X3_Year_SKU_Code__c!=null)) { //Insert into Opportunity Lines OpportunityLineItem OppL_3yr = new OpportunityLineItem(); OppL_3yr.OpportunityId = Opport1.Id; //check if the Renew is for Bundle Support or Support only if (TA.Bundle_Support__c == true && TA.X3_Year_SKU_Code__c!= null) { //Retrieve the priceBookEntryId for Bundle support 3 years Renewals SKU Code OppL_3yr.PricebookEntryId = currentPriceBook(TA.X3_Year_SKU_Code__c ); //OppL_3yr.PricebookEntryId = [Select id from PriceBookEntry where product2.Name =:TA.X3_Year_SKU_Code__c AND priceBook2Id=:prBook.id].Id; } else if (TA.Bundle_Support__c != true && TA.Support_Only_3_Year_SKU__c!= null) { //Retrieve the priceBookEntryId for CO or SO support 3 years Renewals SKU Code OppL_3yr.PricebookEntryId = currentPriceBook(TA.Support_Only_3_Year_SKU__c ); //OppL_3yr.PricebookEntryId = [Select id from PriceBookEntry where product2.Name =:TA.Support_Only_3_Year_SKU__c AND priceBook2Id=:prBook.id].Id; } //OppL_3yr.PricebookEntryId = prBookEntry.Id; OppL_3yr.Quantity=TA.X3_year_SKU__c; OppL_3yr.Start_Date__c= currentServiceDate.addDays(1); OppL_3yr.End_Date__c = currentServiceDate.addYears(Integer.valueOf(3* TA.X3_year_SKU__c)); OppL_3yr.Asset__c = TA.Asset_Id__c; currentServiceDate = currentServiceDate.addYears(Integer.valueOf(3* TA.X3_year_SKU__c)); OppLineItems.add(OppL_3yr); //Insert OppL_3yr; } if (TA.Monthly_Quote__c == false) { if (TA.X1_year_SKU__c > 0 && (TA.Support_Only_1_Year_SKU__c!=null || TA.X1_Year_SKU_Code__c!=null)) { //Insert into Opportunity Lines OpportunityLineItem OppL_1yr = new OpportunityLineItem(); OppL_1yr.OpportunityId = Opport1.Id; //check if the Renew is for Bundle Support or Support only if (TA.Bundle_Support__c == true && TA.X1_Year_SKU_Code__c!= null) { //Retrieve the priceBookEntryId for Bundle support 1 year Renewals SKU Code OppL_1yr.PricebookEntryId = currentPriceBook(TA.X1_Year_SKU_Code__c ); //OppL_1yr.PricebookEntryId = [Select id from PriceBookEntry where product2.Name =:TA.X1_Year_SKU_Code__c AND priceBook2Id=:prBook.id].Id; } else if (TA.Bundle_Support__c != true && TA.Support_Only_1_Year_SKU__c!= null) { //Retrieve the priceBookEntryId for CO or SO support 1 years Renewals SKU Code OppL_1yr.PricebookEntryId = currentPriceBook(TA.Support_Only_1_Year_SKU__c ); //OppL_1yr.PricebookEntryId = [Select id from PriceBookEntry where product2.Name =:TA.Support_Only_1_Year_SKU__c AND priceBook2Id=:prBook.id].Id; } //OppL_1yr.PricebookEntryId = prBookEntry.Id; OppL_1yr.Quantity=TA.X1_year_SKU__c; OppL_1yr.Start_Date__c= currentServiceDate.addDays(1); OppL_1yr.End_Date__c = currentServiceDate.addYears(Integer.valueOf(1* TA.X1_year_SKU__c)); OppL_1yr.Asset__c = TA.Asset_Id__c; currentServiceDate = currentServiceDate.addYears(Integer.valueOf(1* TA.X1_year_SKU__c)); OppLineItems.add(OppL_1yr); //Insert OppL_1yr; } if (TA.Total_in_Months__c > 0 && (TA.Support_Only_Monthly_SKU__c!=null || TA.Monthly_SKU_Code__c!=null)) { //Insert into Opportunity Lines OpportunityLineItem OppL_Months = new OpportunityLineItem(); OppL_Months.OpportunityId = Opport1.Id; //check if the Renew is for Bundle Support or Support only if (TA.Bundle_Support__c == true && TA.Monthly_SKU_Code__c!= null) { //Retrieve the priceBookEntryId for Bundle support Monthly Renewals SKU Code OppL_Months.PricebookEntryId = currentPriceBook(TA.Monthly_SKU_Code__c ); //OppL_Months.PricebookEntryId = [Select id from PriceBookEntry where product2.Name =:TA.Monthly_SKU_Code__c AND priceBook2Id=:prBook.id].Id; } else if (TA.Bundle_Support__c != true && TA.Support_Only_Monthly_SKU__c!= null) { //Retrieve the priceBookEntryId for CO or SO support Monthly Renewals SKU Code OppL_Months.PricebookEntryId = currentPriceBook(TA.Support_Only_Monthly_SKU__c); //OppL_Months.PricebookEntryId = [Select id from PriceBookEntry where product2.Name =:TA.Support_Only_Monthly_SKU__c AND priceBook2Id=:prBook.id].Id; } //OppL_Months.PricebookEntryId = prBookEntry.Id; OppL_Months.Quantity=TA.Total_in_Months__c; OppL_Months.Start_Date__c= currentServiceDate.addDays(1); OppL_Months.End_Date__c = currentServiceDate.addDays(Integer.valueOf(TA.DaysRemaining_Months__c)); OppL_Months.Asset__c = TA.Asset_Id__c; //currentServiceDate = currentServiceDate.addMonths(Integer.valueOf(1* TA.Total_in_Months__c)); OppLineItems.add(OppL_Months); //Insert OppL_Months; } } //end of this new condition for Monthly Quote //If Monthly Quote is true then only monthly SKU should be created else if ((TA.Monthly_Quote__c == true && TA.Total_Monthly_Quote_term__c > 0)){ if (TA.Total_Monthly_Quote_term__c > 0 && (TA.Support_Only_Monthly_SKU__c!=null || TA.Monthly_SKU_Code__c!=null)) { //Insert into Opportunity Lines OpportunityLineItem OppL_Months = new OpportunityLineItem(); OppL_Months.OpportunityId = Opport1.Id; //check if the Renew is for Bundle Support or Support only if (TA.Bundle_Support__c == true && TA.Monthly_SKU_Code__c!= null) { //Retrieve the priceBookEntryId for Bundle support Monthly Renewals SKU Code OppL_Months.PricebookEntryId = currentPriceBook(TA.Monthly_SKU_Code__c ); //OppL_Months.PricebookEntryId = [Select id from PriceBookEntry where product2.Name =:TA.Monthly_SKU_Code__c AND priceBook2Id=:prBook.id].Id; } else if (TA.Bundle_Support__c != true && TA.Support_Only_Monthly_SKU__c!= null) { //Retrieve the priceBookEntryId for CO or SO support Monthly Renewals SKU Code OppL_Months.PricebookEntryId = currentPriceBook(TA.Support_Only_Monthly_SKU__c); //OppL_Months.PricebookEntryId = [Select id from PriceBookEntry where product2.Name =:TA.Support_Only_Monthly_SKU__c AND priceBook2Id=:prBook.id].Id; } //OppL_Months.PricebookEntryId = prBookEntry.Id; OppL_Months.Quantity=TA.Total_Monthly_Quote_term__c ; OppL_Months.Start_Date__c= currentServiceDate.addDays(1); //OppL_Months.End_Date__c = currentServiceDate.addDays(Integer.valueOf(TA.DaysRemaining_Months__c)); OppL_Months.End_Date__c = TA.Expiry_Date__c; OppL_Months.Asset__c = TA.Asset_Id__c; //currentServiceDate = currentServiceDate.addMonths(Integer.valueOf(1* TA.Total_in_Months__c)); OppLineItems.add(OppL_Months); //Insert OppL_Months; } } } //end of new for loop } public String currentPriceBook(String SKU_Code) { List<product2> prId = [Select id from Product2 where Name =:SKU_Code]; System.debug('=== product id is : '+ prId[0].id); for (Product2 pr: prId) { ProductNameToId.put(pr.id, SKU_Code); } System.debug('#### map results in: ' + productNameToId); System.debug('#### map Keyset results in: ' + productNameToId.keyset()); //String prBookEntry= [Select id from PriceBookEntry where product2.Name =:SKU_Code AND priceBook2Id=:prBook1.id].Id; String prBookEntry = [Select id from PricebookEntry where Product2Id IN:prId AND product2.id =: ProductNameToId.keyset() AND priceBook2Id =: prBook1.id].Id; System.debug('#### PricebookEntry is: ' + prBookEntry); return prBookEntry; } System.debug('== Opp Line Items in the list are: ' + oppLineItems); if (OppLineItems.size()>0) { insert OppLineItems; } List<Temp_Assets__c> TempAsset = [SELECT Id From Temp_Assets__c Where CreatedById = :userinfo.getUserId()]; delete TempAsset ; }
@isTest(SeeAllData = true) private class Test_Temp_Asset2_Opp { public static testmethod void testopp() { Account acc = new Account (Name = 'Test Acc',RecordTypeId ='0123000000090Hd',Phone = '1234567809', website = 'test@merunetworks.com'/*type = 'Reseller',Program_Level__c = 'Authorized',Status__c = 'Approved',Preferred_Distributor__c = 'Catalyst'*/); insert acc; Account accDisti = new Account (Name = 'Test Disti',RecordTypeId ='0123000000090Hi',Phone = '1234567809',type = 'Distributor',Program_Level__c = 'Authorized',Status__c = 'Approved'); insert accDisti; Account accRes = new Account (Name = 'Test Reseller',RecordTypeId ='0123000000090Hi',Phone = '1234567809',type = 'Reseller',Program_Level__c = 'Authorized',Status__c = 'Approved'); insert accRes; Opportunity opp = new Opportunity (Name='Test Opp', closeDate=system.today(),recordtypeId = '0123000000094yH', accountId = acc.id,ForecastCategoryName = 'Pipeline',Government_Contract__c = 'None', StageName ='Renewal', Primary_Competitor__c = 'No Competitor', LeadSource='Renewal' ); insert opp; Contract c = new Contract(Name='Test Contract', AccountId=acc.id, status= 'Draft', Contract_Number__c='MERU-1234'); insert c; Pricebook2 standardPb= [select id, name, isActive from Pricebook2 where Name= 'NAM Price Book' limit 1]; Product2 prd = [Select id, name, isActive, cost_price__c, productCode from Product2 where isActive = true Limit 1]; System.assert (prd != null); //Product2 prd = [Select id, name from Product2 where isActive = true and ]); //Product2 prd = new Product2(Name='test Product', isActive = true, cost_Price__c =0.00, productCode='E1-MC1550-VE'); //insert prd; PricebookEntry pbe = [select id from PricebookEntry where product2Id =: prd.id AND pricebook2id =: standardPb.id]; //PricebookEntry pbe = new PricebookEntry (Product2ID=prd.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true,UseStandardPrice=false); // insert pbe; Asset ast = new Asset(Name='Test Asset', AccountId= acc.id, Last_Contract_Id__c= c.id, serialNumber='1234567890', product2Id= prd.id, Serial_Number_ID__c = '000011412MC152033', Service_End_Date_Max__c = Date.valueOf('2014-05-25')); insert ast; test.startTest(); Temp_Assets__c tast = new Temp_Assets__c( Name = 'Test Temp object', AccountId__c = acc.Id, Bundle_Support__c = FALSE, Distributor__c = accDisti.id, Monthly_Quote__c = FALSE, Existing_Opportunity__c = null, Expiry_Date__c = Date.valueOf('2023-12-15'), Opp_Close_Date__c = Date.today(), Install_Date__c = Date.valueOf('2013-12-15'), Last_Contract_Number__c = c.contract_Number__c, New_Opportunity__c = 'Test Opp', Product__c = prd.id, Product_lookup__c = prd.id, Reseller__c = accRes.Id, Serial_Number__c = ast.serial_Number_ID__c, Service_End_Date__c = ast.Service_End_Date_Max__c, Service_Start_Date__c = Date.today(), Incumbent_Reseller__c = 'Yes', Education__c = 'K-12', //PriceBook_Name__c ='NAM Price Book', PriceBook_Name__c =standardPb.name, Asset_Id__c = ast.id); insert tast; //Sudhir commented //System.debug('==> ' + tast.X1_Year_SKU_Code__c); //System.AssertEquals(tast.Monthly_Quote__c,false); test.stopTest(); } } }
Run Failures: Test_Temp_Asset2_Opp.testopp System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Temp_Asset2_Opp: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The changes you have made required the Discount Schedule to be recalculated. SFDC is unable to locate a Discount Schedule. Please contact your System Administrator.: [] Trigger.Temp_Asset2_Opp: line 277, column 1: [] Test_Temp_Asset2_Opp.testopp2 System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Temp_Asset2_Opp: execution of AfterInsert
Please suggest me how to migrate this to production
Thanks
Sudhir
In Setup's "Apex Test Execution" page there is an "Options" button that includes a "Disable Apex Parallel Testing" checkbox. Checking that will probably avoid the problem.
Please mark my answer as a best solution to your question to help others if it solves your problem.
This option has to be disabled in fullcopy or production?
Thanks
Sudhir
Look for that validation rule and modify your test class to manipulate data to pass it.