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
Abhishek KhoslaAbhishek Khosla 

trigger help - Stuck and not able to understand what to do

Hi All,

I have a custom object User Region which has a field called RSM Number and RSM__c which is the owner of that region. I want this field to be updated in Opportunity split whose Split owner is unigue and matches RSM__c . Opportunity split  is having a look up relationship with User region object. This is the tirgger I have written. But I am stuck after I have got All the USER region which have same Account region ID and RSM__C is = Split Owner ID. Because there is a ask that id Split Owner is hared between two User region then it should not update only Split owner which are not shared their RSM number should get updated through this trigger. 

Can you all please help me how to write this trigger 
Sample Code : 
Trigger RSMnumberUpdate on OpportunitySplit (before insert,before update){
if(Trigger.isbefore && (Trigger.isinsert || Trigger.isupdate)){
List<id> SplitownerID = new List<id> ();
List<String> OppAccountRegionID = new List<String>();
    For(OpportunitySplit Opsp : Trigger.new){
          SplitownerID.add(Opsp.Splitowner);
          OppAccountRegionID.add(Opsp.AccountRegionID);
}

List<ID> AllUserRegionsID = [SELECT id FROM UserRegion__c WHERE RSM__c IN : SplitownerID AND Parent_Region__c IN : OppAccountRegionID];
     
        

}
}
 
Ajay K DubediAjay K Dubedi
Hi Abhishek,
Try the following code it may helpful for you:

Trigger RSMnumberUpdate on OpportunitySplit (before insert,before update){
    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
    List<ID> SplitownerID = new List<ID> ();
    List<ID> OppAccountRegionID = new List<ID> ();
    For(OpportunitySplit Opsp : Trigger.new){
          SplitownerID.add(Opsp.Splitowner);
          OppAccountRegionID.add(Opsp.AccountRegionID);
    }
    List<ID> AllUserRegionsID=new List<ID>();
    AllUserRegionsID = [SELECT id FROM UserRegion__c WHERE RSM__c IN : SplitownerID AND Parent_Region__c IN : OppAccountRegionID];
}
}
    
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
 
Andrew GAndrew G
I've not done Opportunity Splits, but reading between the lines I would say that Opportunity Splits are records that are related to Opportunities and probably a many-to-one relation (i.e. one Opportunity has one or more Opportunity Splits)

Within the Opportunity Split, you have a field (RSM Number) that you want to update based on a trigger event.

So to clarify your question, is the concern:

1. that if an Opportunity is split between different regions then the field should not be updated ,

or 

2. that if the owner is managing(owning) more that one region, the field should not be updated?

Regards
Andrew
Abhishek KhoslaAbhishek Khosla
Hi Guys I wrote this one 

trigger RSMNumberUpdate on OpportunitySplit (before insert,before update) {
    if(Trigger.isbefore && (Trigger.isinsert || Trigger.isupdate )){
        Set<ID> SplitOwnerID = New Set<ID>();
        List<String> AccountRegionID1 = New List<String>();
        For(OpportunitySplit OppSt : Trigger.new ){
            SplitOwnerID.add(OppSt.SplitOwnerId);
            AccountRegionID1.add(OppSt.AccountRegionID__c);
        }
        List<User_Region__C> AllUserRegionID = [SELECT ID, RSM__c,RSM_Number__c FROM User_Region__c 
                                                WHERE RSM__c IN : SplitOwnerID AND Parent_Region__c IN : AccountRegionID1];
        System.debug(AllUserRegionID);
        For(ID OppSplitOwnerID : SplitOwnerID){
            For(User_Region__c Ur : AllUserRegionID){
                If(Ur.RSM__c == OppSplitOwnerID ){
                    Integer RSM_Count = 0;
                    RSM_Count = RSM_Count +1 ;
                    System.debug(RSM_Count);
                }
            }
        }
    }

}