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
raz rraz r 

can we write a trigger on AccountTeamMember ?Please help do we need to do any settings to write a trigger on AccountTeamMember

can we write a trigger on AccountTeamMember ?Please help do we need to do any settings to write  a trigger on AccountTeamMember
Niraj Kr SinghNiraj Kr Singh
Hi Raj,

It is not allowed to write trigger on AccountTeamMember.

If realy needed then explain your requirements, might be we will help you.

Thanks
Niraj
raz rraz r
I have a requiremnet like whenever I create/Update/Delete a AccountTeamMember record with team role :Manager ..There is a custom object which is having a MD relationship with Account, Ineed to update a Manager field in this custom object. Please help me
raz rraz r
Hi Niraj
Please help me on this requirement.
 
Niraj Kr SinghNiraj Kr Singh
Hi Raj,

Trigger is not possible, but you can achive your requirememt by writting a Batch apex class and schedule it for frequently like on every 15 minutes.
Like That:

System.schedule('Scheduled Job 1', '0 0 * * * ?', new YourBatchClass());
System.schedule('Scheduled Job 2', '0 15 * * * ?', new YourBatchClass());
System.schedule('Scheduled Job 3', '0 30 * * * ?', new YourBatchClass());
System.schedule('Scheduled Job 4', '0 45 * * * ?', new YourBatchClass());

Thanks
raz rraz r
Can you please elaborate the solution..
 
Niraj Kr SinghNiraj Kr Singh
Hi Raz,

This batch class will not run in exact real time,. It will execute on every 15 mins and take all those accounts which created or updated just before 15 mins back.

If you want you can execute for existing acc records for that you need to remove this where codintion (Optional) from query string:
AND LastModifiedDate >=: whenModified

I have some small doubt asking and as per my assumption writing a batch class:

Q: Is this a picklist/ multiselect picklist/text field, Because might be an account have more that one AccountTeamMember and each will associated with a Role.
Assumption:   
I am taking as latest AccountTeamMember and puting its role into Text field(Manager__c(Text));
Considering your custom object: TestObject__c, and fields:  Manager__c, Account__c
global class AccountTeamMemberUpdate_Batch implements Database.Batchable<SObject>, Database.Stateful{
	//Varibales
	String strQuery;
	
	/*
	I have a requiremnet like whenever I create/Update/Delete a AccountTeamMember record with team role :Manager ..
	There is a custom object which is having a MD relationship with Account, 
	I need to update a Manager field in this custom object. Please help me
	
	Q: Is this a picklist/ multiselect picklist/text field, Because might be an account have more that one AccountTeamMember and each will associated with a Role.
	
	I am taking as latest AccountTeamMember and puting its role into Text field(Manager__c(Text));
	Considering your custom object: TestObject__c, and fields:  Manager__c, Account__c
	*/
	global AccountTeamMemberUpdate_Batch(){
		Datetime whenModified = System.now();
		whenModified = whenModified.addMinutes(-15);
		
		strQuery = 'SELECT AccountId, TeamMemberRole, UserId, AccountAccessLevel, LastModifiedDate FROM AccountTeamMember';
		String strWhereCondition = ' WHERE TeamMemberRole = \'Account Manager\' AND LastModifiedDate >=: whenModified Order By DESC';
		strQuery = strQuery + strWhereCondition
	}
	
	global Database.Querylocator start(Database.Batchablecontext bc){
        system.debug('----- start of Batch Apex ----' + strQuery);         
        
        //Return record to execute method from DB.
        return Database.getQueryLocator(strQuery);
    }
    
    
    /*
    @Method Name   :  execute
    @Param         :  Database.Batchablecontext, List of AccountTeamMember.
    @Description   :  
    */
    
    global void execute(Database.Batchablecontext bc, List<SObject> lstSObject){
        List<AccountTeamMember> lstAccountTeamMember = new List<AccountTeamMember>();
        Map<Id, AccountTeamMember> mapAccIdVsAccMember = new Map<Id, AccountTeamMember>();
		//List having opportunity records
        if(!lstSObject.isEmpty()) {
            lstAccountTeamMember.addAll((List<AccountTeamMember>) lstSObject);
			
			for(AccountTeamMember objAccMem : lstAccountTeamMember) {
				if(!mapAccIdVsAccMember.containsKey(objAccMem.AccountId)) {
					mapAccIdVsAccMember.put(objAccMem.AccountId, objAccMem);
				}
			}
			
			if(!mapAccIdVsAccMember.isEmpty()) {
				List<TestObject__c> lstTestObject = [SELECT Id, Manager__c, Account__c FROM TestObject__c WHERE Account__c In: mapAccIdVsAccMember.keySet()];
				if(!lstTestObject.isEmpty()) {
					for(TestObject__c objTest : lstTestObject) {
						if(mapAccIdVsAccMember.containsKey(objTest.Account__c)) {
							objTest.Manager__c = mapAccIdVsAccMember.get(objTest.Account__c).TeamMemberRole;
						}
					}
					
					update lstTestObject;
				}
			}
		}
    }
	
	global void finish(Database.BatchableContext bc){
		
	}
}
And run this following script from developer console.
System.schedule('Scheduled Job 1', '0 0 * * * ?', new AccountTeamMemberUpdate_Batch());
System.schedule('Scheduled Job 2', '0 15 * * * ?', new AccountTeamMemberUpdate_Batch());
System.schedule('Scheduled Job 3', '0 30 * * * ?', new AccountTeamMemberUpdate_Batch());
System.schedule('Scheduled Job 4', '0 45 * * * ?', new AccountTeamMemberUpdate_Batch());

Note: Might be some update needed in code according to your requirements, and Try for that.

Plz let me know if solution works for you.

Thanks
Niraj

 
Niraj Kr SinghNiraj Kr Singh
Hi Raz,

Is this solved your problem??