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
chowdary marellachowdary marella 

Trigger To create task based on dates.

Hi all.i had written a Trigger.but it is not meeting requirement.
Requirement:-
I have Policy Custom Object, Which is related to Account. There is are 6 "renewal due date"(Formula fields) field based on Expiration_Date__c ,Effective_Date__c,in Policy Object. Now i have to create a task on the Renewal Due Date. There could be many policies for an Account. Now we have to check that is there any policies with same Renewal Date related to one Account. We want to create only one task for the Policies with same Date.

(some accounts could have multiple policies with the same renewal dates.  We only need one set of tasks in this case.)

 

 

Thanks In Advance.


My code as follows:-

trigger Create_Task on Policy__c (After insert,After update) {
list<policy__c> p=new list<policy__c>();
list<task>tsk=new list<task>();
map<date,date> dat=new map<date,date>();
set<id> ids=new set<id>();
Boolean createtask=true;
for(policy__c p1:trigger.new){
ids.add(p1.Account__c);
}
p =[select id,name,Expiration_Date__c ,Effective_Date__c from policy__c where Account__c IN :ids];
system.debug('@@@@@@@@@@@@@@@@@@@@'+p);
for(policy__c p2:p){
dat.put(p2.Expiration_Date__c,p2.Effective_Date__c);
}
for(policy__c p3:trigger.new ){
if(dat.get(p3.Expiration_Date__c)!= null)
{
IF(dat.get(p3.Expiration_Date__c)==p3.Effective_Date__c )
   {
        createtask=False;
   }
}
 If(createtask != False)
 {
    Task t1=new Task();
    Task t2=new Task();
    Task t3=new Task();
    Task t4=new Task();
 
}

}
}