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
Nirmal9114Nirmal9114 

Trigger for Primary Contact. Please just guide me the way

I have this requirement. please jus guide me the way to do it.

Create a notification rule for the Account object that notifies the Primary contact of the Account when the Account is unaltered for a years time. 

Sumit Kumar Singh 9Sumit Kumar Singh 9

Hello Tanya,

I think you should write a batch and schedule it to run daily, weekly, montly whatever fits best for you.
In the start method of the batch, you can return the Accounts which have not been modified for last one year.
In the execute method you can get primary contact of the accounts and can send email to them.

Thanks,
Sumit Kumar Singh
Nirmal9114Nirmal9114
If i want to write a trigger. will that not work?
Sumit Kumar Singh 9Sumit Kumar Singh 9

Hello Tanya, 

Trigger is fired on some events (insert, update, delete etc). How would you fire the trigger without any event??
Please, elaborate your scenario in more detail.


Thanks, 
Sumit Kumar Singh
Nirmal9114Nirmal9114
Hi Sumit,

Yeah you are right! can you jus help me a little more in deep

we have to notify primary contact wwhen account is untouched for more than a year.
 
Sumit Kumar Singh 9Sumit Kumar Singh 9
Hi Tanya, 
Pls have a look at this -
global class AccPrimayContactNotifier implements Database.Batchable<sObject> {
   //Start method of the batch
   global Database.QueryLocator start(Database.BatchableContext BC){
      Date dt = Date.Today().addYears(-1); 
      String query = 'Select id, name from Account where LastModifiedDate <: dt';
      return Database.getQueryLocator(query);
   }
   // Execute method
   global void execute(Database.BatchableContext BC, List<Account> scope) {  
       List<Id> accountIds = new List<Account>();
       for(Account acc : scope){
           accountIds.add(acc.id);     
       }  
       //Getting the Primary Contact of the Accounts
       for(Contact con : [SELECT id, name FROM Contact where primary_Contact__c = true AND AccountId IN : accountIds]){
           Here, you can send the email, using templates OR plain text.     
       }           
   }

   global void finish(Database.BatchableContext BC){
   }
}

 
Nirmal9114Nirmal9114
Thanks Sumit i am working on it..Will let you know if i face any issue