You need to sign in to do that
Don't have an account?
Issue in event trigger
I have a custom date field "APN Last Activity Date" in Account. There is another custom picklist field in Activity called as "APN Activity Status" with values as planned,completed,cancelled and system close
My requirement is to populate APN Last ctivity Date field with the current date whenever the APN Activity Status is changed to "Completed with some other validations.
Below is the code:
trigger UpdateAccountYORDateNew on Event (before insert,before update) {
Set<Id> accIds = new Set<Id> ();
Set<Id> OppIds = new Set<Id> ();
List<Account> lstAcc,lstAcc1;
List<Opportunity> lstOpp;
if(lstOpp !=NULL){
For (Opportunity o :lstOpp )
{
OppIds.add(o.AccountId) ;
}
}
for(Event e: Trigger.new) {
if (e.WhatId != NULL) {
if(String.valueOf(e.WhatId).startsWith('001') && e.APN_Activity_Status__c == 'Completed') {
accIds.add(e.WhatId);
}
if((e.StartDateTime > System.today() && e.APN_Activity_Status__c== 'Completed')) {
e.addError('If APN Activity Status is "Completed", Cannot set the Start Date/Time to a Future Date.');
}
if ((e.APN_Activity_Status__c== 'Completed' && e.Description == NULL)){
e.addError('If APN Activity Status is "Completed", Description is Mandatory.');}
}
}
lstAcc = [Select APN_Last_Activity_Date__c from Account where ID IN: accIds];
lstAcc1 = [Select APN_Last_Activity_Date__c from Account where ID IN: OppIds];
for(Event e: Trigger.new) {
if(Trigger.isInsert) {
if(e.APN_Activity_Status__c == 'Completed') {
for(Account a: lstAcc) {
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
for(Account a: lstAcc1){
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
}
}
else if(Trigger.isUpdate) {
if(e.APN_Activity_Status__c == 'Completed' && Trigger.oldMap.get(e.Id).APN_Activity_Status__c != 'Completed') {
for(Account a: lstAcc) {
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
for(Account a: lstAcc1){
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
}
}
}
update lstAcc;
update lstAcc1;
}
The APN Date field is geting populated when I create or update a new events from accounts. The problem is the same date field doesnt populate when i create/update event from opportunity.
Please help me to fix this issue.
hi,
see this line in ur trigger
if(String.valueOf(e.WhatId).startsWith('001') && e.APN_Activity_Status__c == 'Completed') {
001 is for checking account so it will work only on account
so change ur logic
You are right. But I am not able to find how do i go forward. Instead I thought of creating a custom date APN_Event_Date__c field on opportunity to get populated when event status is completed. Even thats not working. Where am i doing wrong???
trigger UpdateAccountYORDateNew on Event (before insert,before update) {
Set<Id> accIds = new Set<Id> ();
Set<Id> OppIds = new Set<Id> ();
List<Account> lstAcc;
List<Opportunity> lstOpp;
for(Event e: Trigger.new) {
if (e.WhatId != NULL) {
if(String.valueOf(e.WhatId).startsWith('001') && e.APN_Activity_Status__c == 'Completed') {
accIds.add(e.WhatId);
OppIds.add(e.WhatId);
}
if((e.StartDateTime > System.today() && e.APN_Activity_Status__c== 'Completed')) {
e.addError('If APN Activity Status is "Completed", Cannot set the Start Date/Time to a Future Date.');
}
if ((e.APN_Activity_Status__c== 'Completed' && e.Description == NULL)){
e.addError('If APN Activity Status is "Completed", Description is Mandatory.');}
}
}
lstAcc = [Select APN_Last_Activity_Date__c from Account where ID IN: accIds];
lstOpp= [Select APN_Event_Date__c from Opportunity where ID IN: OppIds];
for(Event e: Trigger.new) {
if(Trigger.isInsert) {
if(e.APN_Activity_Status__c == 'Completed') {
for(Account a: lstAcc) {
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
for(Opportunity o: lstOpp) {
if(o.Id == e.WhatId) {
o.APN_Event_Date__c = System.Today();
}
}
}
}
else if(Trigger.isUpdate) {
if(e.APN_Activity_Status__c == 'Completed' && Trigger.oldMap.get(e.Id).APN_Activity_Status__c != 'Completed') {
for(Account a: lstAcc) {
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
for(Opportunity o: lstOpp) {
if(o.Id == e.WhatId) {
o.APN_Event_Date__c = System.Today();
}
}
}
}
}
update lstAcc;
update lstOpp;
}
if(String.valueOf(e.WhatId).startsWith('001') && e.APN_Activity_Status__c == 'Completed') {
accIds.add(e.WhatId);
flag =0;
}
if(String.valueOf(e.WhatId).startsWith('006') && e.APN_Activity_Status__c == 'Completed') {
opp.add(e.WhatId);
flag=1;
}
if((e.StartDateTime > System.today() && e.APN_Activity_Status__c== 'Completed')) {
e.addError('If APN Activity Status is "Completed", Cannot set the Start Date/Time to a Future Date.');
}
if ((e.APN_Activity_Status__c== 'Completed' && e.Description == NULL)){
e.addError('If APN Activity Status is "Completed", Description is Mandatory.');}
}
}
if(flag==0)
lstAcc = [Select APN_Last_Activity_Date__c from Account where ID IN: accIds];
for(Event e: Trigger.new) {
if(Trigger.isInsert) {
if(e.APN_Activity_Status__c == 'Completed') {
for(Account a: lstAcc) {
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
// here u have apply again
//if(flag==1)
for(oppertunity a: opp ){
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
}
}
Hi Sushant,
Sorry for the confusion. The field "APN_Last_Activity_Date__c" is in Accounts and not in Opportunity which is not getting populated when creating events from Opportunity.
Below is the code written so far. Pls let me know my mistake and modify as you wish. Still not able to fix this issue.
trigger UpdateAccountYORDateNew on Event (before insert,before update) {
Set<Id> accIds = new Set<Id> ();
Set<Id> OppIds = new Set<Id> ();
List<Account> lstAcc;
Integer flag;
for(Event e: Trigger.new) {
if (e.WhatId != NULL) {
if(String.valueOf(e.WhatId).startsWith('001') && e.APN_Activity_Status__c == 'Completed') {
accIds.add(e.WhatId);
flag = 0;
}
if(String.valueOf(e.WhatId).startsWith('006') && e.APN_Activity_Status__c == 'Completed') {
OppIds.add(e.WhatId);
flag = 1;}
System.debug('This is the value of flag: '+ flag);
if((e.StartDateTime > System.today() && e.APN_Activity_Status__c== 'Completed')) {
e.addError('If APN Activity Status is "Completed", Cannot set the Start Date/Time to a Future Date.');
}
if ((e.APN_Activity_Status__c== 'Completed' && e.Description == NULL)){
e.addError('If APN Activity Status is "Completed", Description is Mandatory.');}
}
}
if (flag== 0){
lstAcc = [Select APN_Last_Activity_Date__c from Account where ID IN: accIds];}
else if (flag== 1){
lstAcc = [Select APN_Last_Activity_Date__c from Account where ID IN: OppIds];}
for(Event e: Trigger.new) {
if(Trigger.isInsert) {
if(e.APN_Activity_Status__c == 'Completed') {
for(Account a: lstAcc) {
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
}
}
else if(Trigger.isUpdate) {
if(e.APN_Activity_Status__c == 'Completed' && Trigger.oldMap.get(e.Id).APN_Activity_Status__c != 'Completed') {
for(Account a: lstAcc) {
if(a.Id == e.WhatId) {
a.APN_Last_Activity_Date__c = System.Today();
}
}
}
}
}
if ((lstAcc != null) || (flag ==0)||(flag==1)) {
update lstAcc;
}
}