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
Ali Husain 3Ali Husain 3 

OpportunityLineItem trigger

I need to write a trigger on the OpportunityLineItem. Part of the funtionality will involve me getting to the User object. Can someone help me if figuring out how to map these objects?

Here is what it would look like in SQL:

SELECT UserID, Name, Business__c
FROM AccountTeamMember atm
INNER JOIN Account a ON a.ID = atm.AccountID
INNER JOIN Opportunity o ON o.AccountID = a.ID
INNER JOIN OpportunityLineItem oli ON oli.OpportunityID = o.ID
ShashankShashank (Salesforce Developers) 
I could not understand your complete requuirement looking at the SQL, so would you be able to give a detailed explanation of what you are trying to achieve with the trigger so that I can provide you a sample trigger?
Ali Husain 3Ali Husain 3
Sure. I would like to have a trigger on the OpportunityLineItem that will fire if the Business__c = 'XYZ'. Business__c is a custom field on the User. So my thought was that I would need to somehow get to the User object from OpportunityLineItem.
ShashankShashank (Salesforce Developers) 
How is the user connected to the opportunity line item? Is he one of the account team members in the account that this opportunity belongs to? If so, how do we determine which account team member is he, as there can be more than 1 members?
Ali Husain 3Ali Husain 3
Yes, he is one of the account team members. Here is the exact wording of the requirement:

If the Business = "XYZ", the Account Owner listed on the Account should receive 100% split.
ShashankShashank (Salesforce Developers) 
One more question, please. Which account team member do we check? Do we check all account team members and verify if atleast one member has business='xyz'?
Ali Husain 3Ali Husain 3
Actually, there is a bit more to the requirement which may help clarify it a bit:

If the Business = "XYZ" and ABC Products are added to the Opportunity, the ABC Account Owner listed on the Account should receive 100% split. If there is no ABC Account Owner, no user should receive a split.

What i'm taking from that is we need to check all account team members and verify.
ShashankShashank (Salesforce Developers) 
Here's a sample trigger assuming the "ABC account Owner" that you mentioned is an account team member role. It may not be perfect, but it is a good skeleton to work with:
 
trigger updatePick on OpportunityLineItem (after insert) {
    set<Id> accIds = new set<Id>();
    map<Id,Id> userIds = new map<Id,Id>();
    map<id,account> accolimap = new map<id,account>();
    list<opportunitysplit> oslist = new list<opportunitysplit>();
    for(opportunitylineitem ol:trigger.new){
        accIds.add(ol.opportunity.accountId);
        accolimap.put(ol.Id,ol.opportunity.account);
    }
    list<account> acclist = new list<account>([select ownerId, (select userId, teammemberrole from accountteammembers) from account where Id in :accIds]);
    for(account a:acclist){
        for(accountteammember atm:a.accountteammembers){
            if(atm.teammemberrole=='ABC Account Owner'){
                userIds.put(a.Id,atm.userId);
            }
        }
    }
    map<id,user> usermap = new map<id,user>([select business__c from user where business__c='XYZ' and Id in :userIds.keyset()]);
    for(opportunitylineitem ol:trigger.new){
        if((userIds.get(accolimap.get(ol.Id).Id)!=null) && (usermap.get(userIds.get(accolimap.get(ol.Id).Id)).business__c == 'XYZ')){
            opportunitysplit os = new opportunitysplit();
            os.opportunityId = ol.opportunityId;
            os.splitpercentage = 100;
            os.splitownerId = userIds.get(accolimap.get(ol.Id).Id);
            oslist.add(os);
        }
    }
    if(oslist.size()>0){
        insert oslist;
    }
}