function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JJamesJJames 

Trouble setting opportunity split in a trigger

Hi,
So I am trying to set up an opportunity split inside of a trigger (shown below).  I am having trouble getting the correct sales rep into the opportunity split.  I have an object in opportunity called territories which defines which user gets credit for what type of product, for example: territory__c.A_Sales_rep should get credit if the product (or quoteItem below) is of Sales_Division_c is A and territory__c.B_Sales_rep if it is a product of sales division B.  

So overall I just want to achieve the following: Get the sales division that the product is in, get the sales rep that owns that division, and apply an opportunity split.

Any help is appreciated, this has been driving me a little crazy!
trigger UpdateSplits on Opportunity(after update, after insert) 
{
    List<REVVY__MnQuoteItem__c> qItem = new List<REVVY__MnQuoteItem__c>();

    for( Opportunity acc : [ Select Id, (Select Id,Name,REVVY__Price__c FROM REVVY__QuoteItems__r) FROM Opportunity where Id IN: trigger.new ])
    {
        qItem.addALL( acc.REVVY__QuoteItems__r );
    }
    
    List<OpportunitySplit> oppSplitList = new List<OpportunitySplit>();
    
    for(REVVY__MnQuoteItem__c item : qItem)
    {
        //find sales division that gets credit for the quote item == item.Sales_Division__c
        
        oppSplitList.add(new OpportunitySplit(
            OpportunityId = [ Select Id FROM Opportunity where Id IN: trigger.new ]
            , UserId = ???? );
        
        //find rep that gets credit
    
    } 
}

 
Shashikant SharmaShashikant Sharma
Hi,

Could you please share the issue that you are getting ?

Thanks
​Shashikant
JJamesJJames
I am unsure how to pull the correct user which i want to use for the opportunity split from the custom field Territory__c