You need to sign in to do that
Don't have an account?

Apex Tes fails on Trigger fires a Custom Object from OppotunityLineItem with RecordType Dsfinition
Hello,
I wrote a trigger that fires a new custom object (AED_Campaign__c) when a new OpportunityLineItem is added to an Opportunity.
The trigger is working fine even when I've added definition of Record Types.
The problem is that when I try to test the trigger I receive some errors taht don'0t understand very well.
Code is the following:
trigger AddCampaign on OpportunityLineItem (after insert) { //Query for the Account record types List<RecordType> rtypes = [Select Name, Id From RecordType where sObjectType='AED_Campaign__c' and isActive=true]; //Create a map between the Record Type Name and Id for easy retrieval Map<String,String> CampaignsRecordTypes = new Map<String,String>{}; for(RecordType rt: rtypes) CampaignsRecordTypes.put(rt.Name,rt.Id); for (OpportunityLineItem oli : Trigger.new) { if (oli.Implementation_DirectTrack__c == 'Yes') { AED_campaign__c cam = new AED_campaign__c ( Name = oli.Magic_Id__c, Opportunity__c = oli.OpportunityId, Sales_Mode__c = oli.AED__Sales_Mode__c, Implementation_DirectTrack__c = oli.Implementation_DirectTrack__c, Country__c = oli.AED__Country__c, Product__c = oli.ProductTr__c, Channel__c = oli.Channel__c, RecordTypeId=CampaignsRecordTypes.get('DirectTrack') ); insert cam; } else if (oli.Implementation_DirectTrack__c == 'No') { AED_campaign__c cam = new AED_campaign__c ( Name = oli.Magic_Id__c, Opportunity__c = oli.OpportunityId, Sales_Mode__c = oli.AED__Sales_Mode__c, Implementation_DirectTrack__c = oli.Implementation_DirectTrack__c, Country__c = oli.AED__Country__c, Product__c = oli.ProductTr__c, Channel__c = oli.Channel__c, RecordTypeId=CampaignsRecordTypes.get('No_DirectTrack') ); insert cam; } else {} } }
Thetest I'm using is the following:
/** * This class tests the trigger named AddCampaign. */ @isTest private class AddCampaignTriggerTest { static testMethod void AddCampaignTest() { //Data Prep //Create Account, Opportunity, Product, etc. Account acct1 = new Account(name='test Account One1'); acct1.Type = 'Advertiser'; insert acct1; //Create Opportunity on Account Opportunity Oppty1 = new Opportunity(name='test Oppty One1'); Oppty1.AccountId = acct1.Id; Oppty1.StageName = 'Test'; Oppty1.CloseDate = Date.today(); Oppty1.AED__Campaign_Start_Date__c = Date.today(); Oppty1.AED__Campaign_Close_Date__c = Date.today()+1; Oppty1.AED__Invoice_Client_Name__c = acct1.Id; Oppty1.AED__Country__c = 'Spain'; Oppty1.Campaign_Name_Agreement__c = 'test Oppty One1' ; Oppty1.Implementation_DirectTrack__c = 'Yes'; Oppty1.Implementation_DT_Status__c = '1. Create Campaign'; insert Oppty1; //Create Opportunity on Account Opportunity Oppty2 = new Opportunity(name='test Oppty One2'); Oppty2.AccountId = acct1.Id; Oppty2.StageName = 'Test'; Oppty2.CloseDate = Date.today(); Oppty2.AED__Campaign_Start_Date__c = Date.today(); Oppty2.AED__Campaign_Close_Date__c = Date.today()+1; Oppty2.AED__Invoice_Client_Name__c = acct1.Id; Oppty2.AED__Country__c = 'Spain'; Oppty2.Campaign_Name_Agreement__c = 'test Oppty One2' ; Oppty2.Implementation_DirectTrack__c = 'No'; insert Oppty2; // Create Products Product2 testprod1 = new Product2 (name='test product one1'); testprod1.productcode = 'test pd code1one'; insert testprod1; Product2 testprod2 = new Product2 (name='test product two2'); testprod2.productcode = 'test pd code2two'; insert testprod2; // Ger Pricebook Pricebook2 testpb = [select id from Pricebook2 where IsStandard = true]; // Add to pricebook PricebookEntry testpbe1 = new PricebookEntry (); testpbe1.pricebook2id = testpb.id; testpbe1.product2id = testprod1.id; testpbe1.IsActive = True; testpbe1.UnitPrice = 250; testpbe1.UseStandardPrice = false; insert testpbe1; PricebookEntry testpbe2 = new PricebookEntry (); testpbe2.pricebook2id = testpb.id; testpbe2.product2id = testprod2.id; testpbe2.IsActive = True; testpbe2.UnitPrice = 250; testpbe2.UseStandardPrice = false; insert testpbe2; //And now you want execute the startTest method to set the context //of the following apex methods as separate from the previous data //preparation or DML statements. test.starttest(); // add the line item which should call the trigger // with this line item it should fail out quickly // As Auto Schedule is false OpportunityLineItem oli1 = new OpportunityLineItem(); oli1.Quantity = 1; oli1.TotalPrice = 1; oli1.PricebookEntryId = testpbe1.id; oli1.OpportunityId = oppty1.id; insert oli1; // add the line item which should call the trigger // Auto Schedule is true so it should build the schedule. OpportunityLineItem oli2 = new OpportunityLineItem(); oli2.Quantity = 1; oli2.TotalPrice = 1; oli2.PricebookEntryId = testpbe2.id; oli2.OpportunityId = oppty1.id; insert oli2; // add the line item which should call the trigger // Auto Schedule is true so it should build the schedule. // This uses a date on OpptyLineItem to try another code path OpportunityLineItem oli3 = new OpportunityLineItem(); oli3.Quantity = 1; oli3.TotalPrice = 1; oli3.ServiceDate = date.today(); oli3.PriceBookEntryId = testpbe2.id; oli3.OpportunityId = oppty1.id; insert oli3; test.stoptest(); } }
Message Error:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AddCampaign: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Record Type ID: id value not valid for the users profile: : [RecordTypeId] Trigger.AddCampaign: line 31, column 9: [] Class.AddCampaignTriggerTest.AddCampaignTest: line 73, column 5 External entry point
Someone can help me?
Thanks,
Stefano
Hi,
It looks like the User's profile who is running the test method is Not having access to 'DirectTrack' record type on AED_Campaign__c object.
Please assign the 'DirectTrack' record type to the profile from Record type Sction.
And re run the test method.
Thanks,
Kodisana
All Answers
Hi,
It looks like the User's profile who is running the test method is Not having access to 'DirectTrack' record type on AED_Campaign__c object.
Please assign the 'DirectTrack' record type to the profile from Record type Sction.
And re run the test method.
Thanks,
Kodisana
Hello,
I appreciated yor help!
Now the test went correctly.
Thanks a lot.
Stefano
Hi,
Can you please accept the solution?
Thanks,
Kodisana