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
jrosser1.39050288155405E12jrosser1.39050288155405E12 

Writing a Class for Trigger

I am trying to figure out the right approach for writing a class for this trigger.  These objects are not really related.

TSG__c is a customer Object (Trigger object)
User to get the User.ID
Account to get the Account.ID
AccountTeamMember insert record
AccountShare insert record.

Please see trigger.

trigger AccountTeamADD on TSG__c (before update,before insert) {

//list to hold new account team members
     List<AccountTeamMember> acctMembers = new List<AccountTeamMember>();
    
     //list to hold new account sharing rules
     List<AccountShare> acctSharingRules = new List<AccountShare>();


//Hold SalesRep
Set <String> ss = New Set<String>();
//Hold Customer ID
Set <String> aa = New Set<String>();
// Hold User Info
List<User> TheUser=new LIST<User>();
//Hold Account Info
List<Account> TheAcct=new LIST<Account>();

//Loop Through all records in the Trigger.new Collection

For(TSG__c T: Trigger.new)
{
    ss.add(T.email_address__c);
    aa.add(T.P21_CompanyCustomer_ID__c);
}


//Get User Info
TheUser = [Select ID,email from User where email IN:ss];
//Get Account Info
TheAcct = [Select ID,P21_CompanyCustomer_ID__c from Account where P21_CompanyCustomer_ID__c IN:aa];

    System.debug('@@@@--'+TheUser.size());
   
//Check to Make sure a record was retuned  
if (!TheUser.isEmpty()) {

     //Loop Through the Trigger (New Records)

     For (TSG__c T: Trigger.new) {
    
             For (Account acct: TheAcct) {
            
             For (User email: TheUser) {
        
             if (t.email_address__c == email.email && T.P21_CompanyCustomer_ID__c ==acct.P21_CompanyCustomer_ID__c) { //Make sure we match on email_address__c
            
                 //Insert into Account Team Here;
                
                    AccountTeamMember ca = new AccountTeamMember();
                    ca.AccountId = acct.Id;
                    ca.TeamMemberRole = 'Technical Sales';
                    ca.UserId = email.Id;
                    acctMembers.add(ca);
                   
                //Insert into Account Share Here;
               
                AccountShare caSharingRule = new AccountShare();
                    caSharingRule.AccountId = acct.Id;
                    caSharingRule.OpportunityAccessLevel = 'Edit';
                    caSharingRule.CaseAccessLevel = 'Edit';
                    caSharingRule.AccountAccessLevel = 'Edit';
                    caSharingRule.UserOrGroupId = email.Id;
                    acctSharingRules.add(caSharingRule);
                

                
                             }

                        }

                    }

                }
                     upsert acctMembers;
                     upsert acctSharingRules;              
    
              
            }
        }
RadnipRadnip
So are you asking how to remove the code from the trigger so that you just have to call a class method?
jrosser1.39050288155405E12jrosser1.39050288155405E12
Radnip, no I am asking for help on a Test class so I can move it into production.

Thank you,
RadnipRadnip
So what is the code supposed to achieve? what tests do you need to do? Firstly I would remove the code from the trigger and create a class with it. It will make the code cleaner.
jrosser1.39050288155405E12jrosser1.39050288155405E12
We have a custom Object called TSG__c when a record is changed or updated it will look at the User and the Account and add that person to the account team / account share.

I have never created a class and called that class from the trigger, what would something like that look like?

Thank you,