You need to sign in to do that
Don't have an account?
Create custom object records under Opportunity with data from 2 diff custom objects under Opp
I have two custom objects under the Opportunity object and I'd like to make a trigger that creates a third set of custom object records.
Custom object one is 'RepSplit__c': contains a Sales Rep (user lookup id) and Percentage Split (percentage)
Custom object two is 'OppPayment__c': contains a Date and amount of money received (currency) and amount of money initially expected (currency)
Third custom object titled 'RepRevShare__c' -- Opportunity, Percentage, Expected Amount, Actual Amount, Date, Amount of Money Received * Percentage (formula),
I want to have RepRevShare records be the combination of the other two objects, so if I have RepSplit1,2,and 3...and OppPayments A,B... then I want to trigger creation of six records:
RepRevShare1A, 1B, 2A, 2B, 3A, and 3B
All this to have matrix reporting where my Reps are on the Y axis and Months are on the X axis, with money in the middle!
So here goes with my little programming knowledge...
My thinking was I need the following actions:
before insert,update,delete of a RepSplit or an OppPayment -- clear/delete all RepRevShare records under the related Opportunity
after insert,update,delete pf a RepSplit or an OppPayment -- create all new RepRevShare records
Is this possible? I've been working my way through coding bits and pieces but my guess is there is a far easier way to code this.
Is this thinking correct?
for CustomObject 1 & 2 before update, before insert, before delete:
for CustomObject 1 & 2 after update, after insert, after delete:
I know coupled 'for' statements is messy...
Hi Gator ,
trigger createnewRecordsUnderOpp for RepSplit__c(before insert)
{
Set<Id> obJopp = new Setr<Id>();
for(RepSplit__c opp : Trigger.New)
{
obJopp.add(opp.opportunityId);
}
List<RepRevShare__c> objRepRevShare = new List<RepRevShare__c>();
List<RepSplit__c> objRepsplit = [select id,Name form RepSplit__c where opportunityId in : obJopp];
List<OppPayment__c> objOppPayment = [select id,Name form RepSplit__c where opportunityId in : obJopp];
for(RepSplit__c objRepSp : objRepsplit)
{for(OppPayment__c objOppPay : objOppPayment)
{RepRevShare__c objRepRevShare = new RepRevShare__c();
objRepRevSharel.Name = objRepSp.Name + objOppPay.Name ;
objRepRevShare.add(objRepRevSharel);}}}
Above code is the small sniipet of code for befor creation on RepSplit Object same type of trigger also created on OppPayment__c for upadte and insert.
Please try from your end and let me know if any further doubt.
Thanks! looks like the only current problem is on
line: {RepRevShare__c objRepRevShare = new RepRevShare__c();
error message: "Duplicate variable: objRepRevShare (attempt to re-create the variable with type:RepRevShare__c)"
Hi gator,
Please rename objRepRevShare to objRepRevShre means
RepRevShare__c objRepRevShre = new RepRevShare__c();
and try from your end and post it wheteher it works.
deleted
Got it! Thanks again! Except... still running into a problem when I try to delete a RepSplit to trigger --
Error:
'There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger createnewRecordsUnderOpp caused an unexpected exception, contact your administrator: createnewRecordsUnderOpp: execution of AfterDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.createnewRecordsUnderOpp: line 5, column 1".'
See full code below:
--------------
trigger createnewRecordsUnderOpp on RepSplit__c (after insert, after update, before delete) {
Set<Id> obJopp = new Set<Id>();
for(RepSplit__c opp : Trigger.New)
{
obJopp.add(opp.Opportunity__c);
}
List<RepRevShare__c> objRepRevShare = new List<RepRevShare__c>();
List<RepSplit__c> objRepsplit = [select id,Name,Opportunity__c, SplitPercent__c, Rep__c from RepSplit__c where Opportunity__c in : obJopp];
List<OppPayment__c> objOppPayment = [select id, Name, Opportunity__c, Opp_Product__c, Date__c, Amount__c, Booked__c from OppPayment__c where Opportunity__c in : obJopp];
for(RepSplit__c objRepSp : objRepsplit){
for(OppPayment__c objOppPay : objOppPayment){
RepRevShare__c objRepRevSharel = new RepRevShare__c();
objRepRevSharel.Name = objRepSp.Name + objOppPay.Name ;
objRepRevSharel.Opportunity__c = objRepSp.Opportunity__c ;
objRepRevSharel.Percent__c = objRepSp.SplitPercent__c ;
objRepRevSharel.Rep__c = objRepSp.Rep__c ;
objRepRevSharel.Booked__c = objOppPay.Booked__c ;
objRepRevSharel.Amount__c = objOppPay.Amount__c ;
objRepRevSharel.Date__c = objOppPay.Date__c ;
objRepRevShare.add(objRepRevSharel);
}}
insert(objRepRevShare);
}
Also, one other element I need is to first locate the Opportunityid and delete all the current RepRevShare records before running this and creating new records. Again, truly appreciate the help!