-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
3Questions
-
2Replies
Creating a new record on deletion
Hi All,
I'm successfully passed my first trigger and have even created a few that work (with test cases) without bugging all of you, but now I'm stuck again :(.
I have a trigger that works successfully in my sandbox (an after delete trigger that creates a record in a custom object to track the history of opportunity splits... don't know why we can't just track history, but there you go ;).
Any thoughts would be greatly appreciated :).
Thanks,
Jodi
I'm successfully passed my first trigger and have even created a few that work (with test cases) without bugging all of you, but now I'm stuck again :(.
I have a trigger that works successfully in my sandbox (an after delete trigger that creates a record in a custom object to track the history of opportunity splits... don't know why we can't just track history, but there you go ;).
trigger CreateSplitAuditRecordDelete on OpportunitySplit (after delete) { if(!myStaticClass.flag) { List<Opportunity_Split_History__c> listDOSH = new List<Opportunity_Split_History__c>(); for(OpportunitySplit Dos : trigger.old) { listDOSH.add(new Opportunity_Split_History__c(Name='Deleted '+ trigger.oldmap.get(Dos.ID).split, Action__c = 'Deleted', CurrencyIsoCode = trigger.oldmap.get(Dos.ID).CurrencyIsoCode, Modified_By__c =Dos.LastModifiedById, Modified_Date__c =Dos.LastModifiedDate, Opportunity__c =trigger.oldmap.get(Dos.ID).OpportunityId, Opp_Split_ID__c =trigger.oldmap.get(Dos.ID).ID, Split_Percentage_New__c =0, Split_Percentage_Old__c =trigger.oldmap.get(Dos.ID).SplitPercentage, Split_Team_Member_New__c =null, Split_Team_Member_Old__c =trigger.oldmap.get(Dos.ID).SplitOwnerId, Split__c =trigger.oldmap.get(Dos.ID).Split, Team_nnCMRR_New__c =0, Team_nnCMRR_Old__c =trigger.oldmap.get(Dos.ID).Team_nnCMRR__c)); } if(listDOSH.size() > 0) { insert listDOSH; }} myStaticClass.flag = true ; }This works perfectly..., but I cannot get the test case to work. For some reason, my trigger isn't firing. I delete my opportunity split, I can see that it has been deleted, but the new record that should be created in the opportunity split history is not showing up.
@isTest private class Test_OpportunitySplitDelete { static testMethod void TestOpportunitySplitDelete() { //Insert users with sales profile //If failing tests, be sure that the sales profile is still there Profile mySalesProfile = [SELECT Id FROM Profile WHERE Name='Starmind Sales User']; User myUser = new User(Alias = 'standt', Email='standarduser@starmind.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = mySalesProfile.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@starmind.com', isActive=True); insert myUser; User mySalesUser1 = new User(Alias = 'sales1', Email='salesuser1@starmind.com', EmailEncodingKey='UTF-8', LastName='Primary', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = mySalesProfile.Id, TimeZoneSidKey='America/Los_Angeles', UserName='salesuser1@starmind.com', isActive=True); insert mySalesUser1; User mySalesUser2 = new User(Alias = 'sales2', Email='salesuser2@starmind.com', EmailEncodingKey='UTF-8', LastName='Secondary', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = mySalesProfile.Id, TimeZoneSidKey='America/Los_Angeles', UserName='salesuser2@starmind.com', isActive=True); Insert mySalesUser2; // The following code runs as user 'myUser' System.runAs(myUser) { //Create test Account,Opportunity,Opportunity Split Account myAccount = new Account(name='TestAccount', currencyisocode='CHF', Region__c='CH'); insert myAccount; Opportunity myOpportunity = new Opportunity(name='TestOpportunity', AccountID=myAccount.Id, CloseDate=date.today(), StageName='5 Contract Sent', CurrencyIsoCode=myAccount.CurrencyIsoCode, OwnerId=myuser.id); insert myOpportunity; //Selecting the opportunity split type, if more than one is active, next line will need to change OpportunitySplitType ost = [Select ID from OpportunitySplitType where isactive = true]; //Defining variable for original split that was created with the opportunity OpportunitySplit osOwner = [SELECT CreatedById,CreatedDate,CurrencyIsoCode,Id,IsDeleted,LastModifiedById,LastModifiedDate, OpportunityId,Split,SplitAmount,SplitNote,SplitOwnerId,SplitPercentage,SplitTypeId, SystemModstamp,Team_nnCMRR__c FROM OpportunitySplit WHERE OpportunityId = :myOpportunity.id and SplitOwnerID = :myUser.Id and SplitTypeID = :ost.id]; system.debug('owner split, 100%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osOwner.id]); //Adding new opportunity split -- to be deleted later OpportunitySplit osDelete = new OpportunitySplit(SplitTypeId=ost.ID, OpportunityID=myOpportunity.ID, SplitOwnerID=mySalesUser1.id, SplitPercentage=60); insert osDelete; system.debug('delete split, created at 60%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osDelete.id]); OpportunitySplit osAdd = new OpportunitySplit(SplitTypeId=ost.ID, OpportunityID=myOpportunity.ID, SplitOwnerID=mySalesUser2.id, SplitPercentage=20); insert osAdd; system.debug('add split, created at 20%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osAdd.id]); //Check to see that new opp split was created system.debug('updated owner split, to 20%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osOwner.id]); system.debug('delete split, stays 60%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osDelete.id]); //Delete newly created record to test the deletion trigger delete [SELECT Id FROM OpportunitySplit WHERE ID = :osDelete.id]; //check new percentages; did the new opp split get deleted This works system.debug('original split, to 80%'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osOwner.id]); system.debug('does the record show as deleted?'+ [SELECT Id,isDeleted,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osDelete.id all rows]); system.debug('should have no records'+ [SELECT Id,Split,SplitPercentage FROM OpportunitySplit WHERE ID = :osDelete.id]); OpportunitySplit Deleted = [SELECT Id, IsDeleted FROM opportunitysplit WHERE Id = :osDelete.Id ALL ROWS]; System.assertEquals(deleted.IsDeleted, true); //Validate deletion recorded in the history table This doesn't work system.debug('should show 1 record with delete action' + [SELECT Action__c,Id,IsDeleted,Split__c,Split_Percentage_New__c,Split_Percentage_Old__c, Split_Team_Member_New__c FROM Opportunity_Split_History__c where Opp_Split_ID__c = :osDelete.id]); Opportunity_Split_History__c OSH1 = [SELECT Action__c,CreatedById,CreatedDate,Id,IsDeleted, LastModifiedById,LastModifiedDate,Modified_By__c,Modified_Date__c, Name,Opportunity__c,Opp_Split_ID__c,OwnerId,Split_Percentage_New__c, Split_Percentage_Old__c,Split_Team_Member_New__c, Split_Team_Member_Old__c,Split__c,SystemModstamp,Team_nnCMRR_New__c, Team_nnCMRR_Old__c, Opportunity_Split_History__c.CurrencyIsoCode FROM Opportunity_Split_History__c WHERE Action__c = 'Deleted' /*and Opp_Split_ID__c = :osDelete.Id*/]; /* System.assertEquals('Deleted', OSH1.Action__c, 'Expected Action = Deleted'); System.assertEquals('CHF', OSH1.CurrencyIsoCode, 'Expected CurrencyIsoCode =CHF'); System.assertEquals(myuser.id, OSH1.Modified_By__c, 'Expected Modified by = MyUser'); System.assertEquals(osDelete.LastModifiedDate, (OSH1.Modified_Date__c), 'Expected Modified date = Today'); System.assertEquals(myOpportunity.id, OSH1.Opportunity__c, 'Expected Opportunity = TestOpportunity'); System.assertEquals(0, OSH1.Split_Percentage_New__c , 'Expected Split_Percentage_New__c to = null'); System.assertEquals(0, OSH1.Split_Percentage_Old__c, 'Expected Split_Percentage_Old__c =0'); System.assertEquals(mySalesUser1.Id, OSH1.Split_Team_Member_Old__c, 'Expected Split_Team_Member_Old__c = primary'); System.assertEquals(null, OSH1.Split_Team_Member_New__c, 'Expected Split_Team_Member_New__c =null'); System.assertEquals(0, OSH1.Team_nnCMRR_New__c, 'Expected Team_nnCMRR_New__c = 0'); System.assertEquals(0, OSH1.Team_nnCMRR_Old__c, 'Expected Team_nnCMRR_Old__c = null');
Any thoughts would be greatly appreciated :).
Thanks,
Jodi
- Jodi Spitler
- June 07, 2016
- Like
- 0
Creating an audit trail for opp split records
Hello out there :),
As I've discovered that there is no OOTB history for the opportunity split records, I've tried to create my own with a custom object and the code below. Unfortunately, it is not catching the delete action, nor is it catching the insert when it is a part of the update of adding a new team member with a new percentage split. Any ideas? My thoughts have been centering around using trigger.old for delete and the fact that there is some automation already around the opportunity split record that is happening behind the scenes. Any help would be greatly appreciated :)
Thanks,
Jodi
trigger CreateSplitAuditRecord on OpportunitySplit (after delete, after insert, after update)
{
if(!myStaticClass.flag)
{
List<Opportunity_Split_History__c> listOSH = new List<Opportunity_Split_History__c>();
for(OpportunitySplit os : trigger.new)
{
if(Trigger.isDelete)
{
listOSH.add(new Opportunity_Split_History__c(Name='Deleted '+ os.split,
Action__c = 'Deleted',
CurrencyIsoCode = os.CurrencyIsoCode,
Modified_By__c =os.LastModifiedById,
Modified_Date__c =os.LastModifiedDate,
Opportunity__c =os.OpportunityId,
Opp_Split_ID__c =os.ID,
/*Split_Note_New__c =null,
Split_Note_Old__c =trigger.oldmap.get(os.ID).SplitNote,*/
Split_Percentage_New__c =0,
Split_Percentage_Old__c =trigger.oldmap.get(os.ID).SplitPercentage,
Split_Team_Member_New__c =null,
Split_Team_Member_Old__c =trigger.oldmap.get(os.ID).SplitOwnerId,
Split__c =trigger.oldmap.get(os.ID).Split,
Team_nnCMRR_New__c =null,
Team_nnCMRR_Old__c =trigger.oldmap.get(os.ID).Team_nnCMRR__c));
}
{
if(Trigger.isInsert)
{
listOSH.add(new Opportunity_Split_History__c(Name='Created '+ os.split,
Action__c = 'Created',
CurrencyIsoCode = os.CurrencyIsoCode,
Modified_By__c =os.LastModifiedById,
Modified_Date__c =os.LastModifiedDate,
Opportunity__c =os.OpportunityId,
Opp_Split_ID__c =os.ID,
/*Split_Note_New__c =os.SplitNote,
Split_Note_Old__c =null,*/
Split_Percentage_New__c =os.SplitPercentage,
Split_Percentage_Old__c =0,
Split_Team_Member_Old__c =null,
Split_Team_Member_New__c =os.SplitOwnerId,
Split__c =os.Split,
Team_nnCMRR_New__c =os.Team_nnCMRR__c,
Team_nnCMRR_Old__c = null));
}
if(Trigger.isUpdate)
{
listOSH.add(new Opportunity_Split_History__c(Name='Upserted '+ os.split,
Action__c = 'Upserted',
CurrencyIsoCode = os.CurrencyIsoCode,
Modified_By__c =os.LastModifiedById,
Modified_Date__c =os.LastModifiedDate,
Opportunity__c =os.OpportunityId,
Opp_Split_ID__c =os.ID,
/*Split_Note_New__c =os.SplitNote,
Split_Note_Old__c =Trigger.oldMap.get(os.Id).Split,*/
Split_Percentage_New__c =os.SplitPercentage,
Split_Percentage_Old__c =Trigger.oldMap.get(os.Id).SplitPercentage,
Split_Team_Member_New__c =os.SplitOwnerId,
Split_Team_Member_Old__c =Trigger.oldMap.get(os.Id).SplitOwnerId,
Split__c =os.Split,
Team_nnCMRR_New__c =os.Team_nnCMRR__c,
Team_nnCMRR_Old__c =Trigger.oldMap.get(os.Id).Team_nnCMRR__c));
}
}
if(listOSH.size() > 0)
{
insert listOSH;
}}
myStaticClass.flag = true ;
}}
As I've discovered that there is no OOTB history for the opportunity split records, I've tried to create my own with a custom object and the code below. Unfortunately, it is not catching the delete action, nor is it catching the insert when it is a part of the update of adding a new team member with a new percentage split. Any ideas? My thoughts have been centering around using trigger.old for delete and the fact that there is some automation already around the opportunity split record that is happening behind the scenes. Any help would be greatly appreciated :)
Thanks,
Jodi
trigger CreateSplitAuditRecord on OpportunitySplit (after delete, after insert, after update)
{
if(!myStaticClass.flag)
{
List<Opportunity_Split_History__c> listOSH = new List<Opportunity_Split_History__c>();
for(OpportunitySplit os : trigger.new)
{
if(Trigger.isDelete)
{
listOSH.add(new Opportunity_Split_History__c(Name='Deleted '+ os.split,
Action__c = 'Deleted',
CurrencyIsoCode = os.CurrencyIsoCode,
Modified_By__c =os.LastModifiedById,
Modified_Date__c =os.LastModifiedDate,
Opportunity__c =os.OpportunityId,
Opp_Split_ID__c =os.ID,
/*Split_Note_New__c =null,
Split_Note_Old__c =trigger.oldmap.get(os.ID).SplitNote,*/
Split_Percentage_New__c =0,
Split_Percentage_Old__c =trigger.oldmap.get(os.ID).SplitPercentage,
Split_Team_Member_New__c =null,
Split_Team_Member_Old__c =trigger.oldmap.get(os.ID).SplitOwnerId,
Split__c =trigger.oldmap.get(os.ID).Split,
Team_nnCMRR_New__c =null,
Team_nnCMRR_Old__c =trigger.oldmap.get(os.ID).Team_nnCMRR__c));
}
{
if(Trigger.isInsert)
{
listOSH.add(new Opportunity_Split_History__c(Name='Created '+ os.split,
Action__c = 'Created',
CurrencyIsoCode = os.CurrencyIsoCode,
Modified_By__c =os.LastModifiedById,
Modified_Date__c =os.LastModifiedDate,
Opportunity__c =os.OpportunityId,
Opp_Split_ID__c =os.ID,
/*Split_Note_New__c =os.SplitNote,
Split_Note_Old__c =null,*/
Split_Percentage_New__c =os.SplitPercentage,
Split_Percentage_Old__c =0,
Split_Team_Member_Old__c =null,
Split_Team_Member_New__c =os.SplitOwnerId,
Split__c =os.Split,
Team_nnCMRR_New__c =os.Team_nnCMRR__c,
Team_nnCMRR_Old__c = null));
}
if(Trigger.isUpdate)
{
listOSH.add(new Opportunity_Split_History__c(Name='Upserted '+ os.split,
Action__c = 'Upserted',
CurrencyIsoCode = os.CurrencyIsoCode,
Modified_By__c =os.LastModifiedById,
Modified_Date__c =os.LastModifiedDate,
Opportunity__c =os.OpportunityId,
Opp_Split_ID__c =os.ID,
/*Split_Note_New__c =os.SplitNote,
Split_Note_Old__c =Trigger.oldMap.get(os.Id).Split,*/
Split_Percentage_New__c =os.SplitPercentage,
Split_Percentage_Old__c =Trigger.oldMap.get(os.Id).SplitPercentage,
Split_Team_Member_New__c =os.SplitOwnerId,
Split_Team_Member_Old__c =Trigger.oldMap.get(os.Id).SplitOwnerId,
Split__c =os.Split,
Team_nnCMRR_New__c =os.Team_nnCMRR__c,
Team_nnCMRR_Old__c =Trigger.oldMap.get(os.Id).Team_nnCMRR__c));
}
}
if(listOSH.size() > 0)
{
insert listOSH;
}}
myStaticClass.flag = true ;
}}
- Jodi Spitler
- May 19, 2016
- Like
- 0
My very first Trigger: updating a child record from a value on a parent record. Could anyone help me optimize?
Hi All,
I'm very proud of my first trigger :), but I'm sure that it could be improved considerably. Could anyone assist? It works, but I will still need another trigger on the Opportunity Product (as it only works on update and not on insert). ...and I'm sure I missed out on some basics as this is my first shot.
What the code does: Updates a field (Duration) on the opportunity products based on a field on the opportunity line item (phase) and 3 fields on the opportunity (dur_nnCMRR_1, _2,or _3)
trigger UpdateDurations on Opportunity (after update)
//removed after insert as opps don’t have any product line when they are first created//
{for(Opportunity opp: Trigger.new)
{Opportunity oldOpp = Trigger.oldMap.get(Opp.Id);
if(oldOpp.Dur_2_nnCMRR__c != Opp.Dur_2_nnCMRR__c )
{ List<OpportunityLineItem> children = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Evaluation'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: children)
{if(LI.Duration__c != Opp.Dur_2_nnCMRR__c)
{LI.Duration__c = Opp.Dur_2_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}
if(oldOpp.Dur_1_nnCMRR__c != Opp.Dur_1_nnCMRR__c )
{ List<OpportunityLineItem> Install = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Installation'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: Install)
{if(LI.Duration__c != Opp.Dur_1_nnCMRR__c)
{LI.Duration__c = Opp.Dur_1_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}
if(oldOpp.Dur_3_nnCMRR__c != Opp.Dur_3_nnCMRR__c )
{ List<OpportunityLineItem> Use = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Usage'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: Use)
{if(LI.Duration__c != Opp.Dur_3_nnCMRR__c)
{LI.Duration__c = Opp.Dur_3_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}}}
I'm very proud of my first trigger :), but I'm sure that it could be improved considerably. Could anyone assist? It works, but I will still need another trigger on the Opportunity Product (as it only works on update and not on insert). ...and I'm sure I missed out on some basics as this is my first shot.
What the code does: Updates a field (Duration) on the opportunity products based on a field on the opportunity line item (phase) and 3 fields on the opportunity (dur_nnCMRR_1, _2,or _3)
trigger UpdateDurations on Opportunity (after update)
//removed after insert as opps don’t have any product line when they are first created//
{for(Opportunity opp: Trigger.new)
{Opportunity oldOpp = Trigger.oldMap.get(Opp.Id);
if(oldOpp.Dur_2_nnCMRR__c != Opp.Dur_2_nnCMRR__c )
{ List<OpportunityLineItem> children = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Evaluation'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: children)
{if(LI.Duration__c != Opp.Dur_2_nnCMRR__c)
{LI.Duration__c = Opp.Dur_2_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}
if(oldOpp.Dur_1_nnCMRR__c != Opp.Dur_1_nnCMRR__c )
{ List<OpportunityLineItem> Install = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Installation'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: Install)
{if(LI.Duration__c != Opp.Dur_1_nnCMRR__c)
{LI.Duration__c = Opp.Dur_1_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}
if(oldOpp.Dur_3_nnCMRR__c != Opp.Dur_3_nnCMRR__c )
{ List<OpportunityLineItem> Use = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Usage'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: Use)
{if(LI.Duration__c != Opp.Dur_3_nnCMRR__c)
{LI.Duration__c = Opp.Dur_3_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}}}
- Jodi Spitler
- March 15, 2016
- Like
- 0
My very first Trigger: updating a child record from a value on a parent record. Could anyone help me optimize?
Hi All,
I'm very proud of my first trigger :), but I'm sure that it could be improved considerably. Could anyone assist? It works, but I will still need another trigger on the Opportunity Product (as it only works on update and not on insert). ...and I'm sure I missed out on some basics as this is my first shot.
What the code does: Updates a field (Duration) on the opportunity products based on a field on the opportunity line item (phase) and 3 fields on the opportunity (dur_nnCMRR_1, _2,or _3)
trigger UpdateDurations on Opportunity (after update)
//removed after insert as opps don’t have any product line when they are first created//
{for(Opportunity opp: Trigger.new)
{Opportunity oldOpp = Trigger.oldMap.get(Opp.Id);
if(oldOpp.Dur_2_nnCMRR__c != Opp.Dur_2_nnCMRR__c )
{ List<OpportunityLineItem> children = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Evaluation'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: children)
{if(LI.Duration__c != Opp.Dur_2_nnCMRR__c)
{LI.Duration__c = Opp.Dur_2_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}
if(oldOpp.Dur_1_nnCMRR__c != Opp.Dur_1_nnCMRR__c )
{ List<OpportunityLineItem> Install = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Installation'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: Install)
{if(LI.Duration__c != Opp.Dur_1_nnCMRR__c)
{LI.Duration__c = Opp.Dur_1_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}
if(oldOpp.Dur_3_nnCMRR__c != Opp.Dur_3_nnCMRR__c )
{ List<OpportunityLineItem> Use = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Usage'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: Use)
{if(LI.Duration__c != Opp.Dur_3_nnCMRR__c)
{LI.Duration__c = Opp.Dur_3_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}}}
I'm very proud of my first trigger :), but I'm sure that it could be improved considerably. Could anyone assist? It works, but I will still need another trigger on the Opportunity Product (as it only works on update and not on insert). ...and I'm sure I missed out on some basics as this is my first shot.
What the code does: Updates a field (Duration) on the opportunity products based on a field on the opportunity line item (phase) and 3 fields on the opportunity (dur_nnCMRR_1, _2,or _3)
trigger UpdateDurations on Opportunity (after update)
//removed after insert as opps don’t have any product line when they are first created//
{for(Opportunity opp: Trigger.new)
{Opportunity oldOpp = Trigger.oldMap.get(Opp.Id);
if(oldOpp.Dur_2_nnCMRR__c != Opp.Dur_2_nnCMRR__c )
{ List<OpportunityLineItem> children = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Evaluation'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: children)
{if(LI.Duration__c != Opp.Dur_2_nnCMRR__c)
{LI.Duration__c = Opp.Dur_2_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}
if(oldOpp.Dur_1_nnCMRR__c != Opp.Dur_1_nnCMRR__c )
{ List<OpportunityLineItem> Install = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Installation'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: Install)
{if(LI.Duration__c != Opp.Dur_1_nnCMRR__c)
{LI.Duration__c = Opp.Dur_1_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}
if(oldOpp.Dur_3_nnCMRR__c != Opp.Dur_3_nnCMRR__c )
{ List<OpportunityLineItem> Use = [ SELECT Id, OpportunityId, Duration__c,Phase__c from
OpportunityLineItem where OpportunityId = :Opp.Id AND Phase__c='Usage'];
List<OpportunityLineItem> newids = new List<OpportunityLineItem>();
for(OpportunityLineItem LI: Use)
{if(LI.Duration__c != Opp.Dur_3_nnCMRR__c)
{LI.Duration__c = Opp.Dur_3_nnCMRR__c;newids.add(LI);}}
if (newids.isEmpty()== false)
{update newids;}}}}
- Jodi Spitler
- March 15, 2016
- Like
- 0
Test Class unable to insert opportunity
In my test class I need to create an opportunity to test against.
//Create Opportunity
// set up opp and Verify that the results are as expected.
RecordType SigSales = [select Id from RecordType where Name = 'Sigma Sales' and SobjectType = 'Opportunity'];
String opportunityName = 'My TEST Opportunity';
Opportunity opp1 = new Opportunity(
//AccountId=testAccount.Id,
Name=opportunityName,
StageName='Idea',
CloseDate=Date.today(),
RecordType=SigSales
);
insert opp1;
When I run the test I get the following error:
11:12:43.788 (12788478666)|VF_PAGE_MESSAGE|You can't create an Opportunity Split for a user unless they're part of the opportunity team.
11:12:43.788 (12788578660)|EXCEPTION_THROWN|[77]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, You can't create an Opportunity Split for a user unless they're part of the opportunity team.: [SplitOwnerId]
11:12:43.789 (12789451592)|HEAP_ALLOCATE|[77]|Bytes:196
11:12:43.797 (12797690786)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, You can't create an Opportunity Split for a user unless they're part of the opportunity team.: [SplitOwnerId]
Class.TestAddSAFromAccounttoOpp.myUnitTest: line 77, column 1
11:12:43.797 (12797713236)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, You can't create an Opportunity Split for a user unless they're part of the opportunity team.: [SplitOwnerId]
I am not attempting to create any splits. Not sure what is going on here.
Thanks!
Gaither
//Create Opportunity
// set up opp and Verify that the results are as expected.
RecordType SigSales = [select Id from RecordType where Name = 'Sigma Sales' and SobjectType = 'Opportunity'];
String opportunityName = 'My TEST Opportunity';
Opportunity opp1 = new Opportunity(
//AccountId=testAccount.Id,
Name=opportunityName,
StageName='Idea',
CloseDate=Date.today(),
RecordType=SigSales
);
insert opp1;
When I run the test I get the following error:
11:12:43.788 (12788478666)|VF_PAGE_MESSAGE|You can't create an Opportunity Split for a user unless they're part of the opportunity team.
11:12:43.788 (12788578660)|EXCEPTION_THROWN|[77]|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, You can't create an Opportunity Split for a user unless they're part of the opportunity team.: [SplitOwnerId]
11:12:43.789 (12789451592)|HEAP_ALLOCATE|[77]|Bytes:196
11:12:43.797 (12797690786)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, You can't create an Opportunity Split for a user unless they're part of the opportunity team.: [SplitOwnerId]
Class.TestAddSAFromAccounttoOpp.myUnitTest: line 77, column 1
11:12:43.797 (12797713236)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, You can't create an Opportunity Split for a user unless they're part of the opportunity team.: [SplitOwnerId]
I am not attempting to create any splits. Not sure what is going on here.
Thanks!
Gaither
- Gaither Simmons
- November 10, 2014
- Like
- 0