+ Start a Discussion
steve456steve456 

Basic Trigger Issue

I need to create an event whenever Opportunity field Probability is greater than 50%

 

I have written the code but it keeps firing on every update.I want it to fire on an insert or if there is any change in probability

 

My code is 

 

********************************

 

trigger AutoEventCreate on Opportunity (after insert , after update) {

List <Event> eventList = new List <Event> ();
Set<Id> oppIds = new Set<Id>();
for(Opportunity opp:Trigger.new){
if(opp.Id!=null &&(Trigger.isInsert || (Trigger.newMap.get(opp.id).Probability != Trigger.oldMap.get(opp.Id).Probability))){

oppIds.add(opp.Id);
}

}

for(Opportunity opp:Trigger.new){
if(opp.Probability !=null && opp.Probability>=25.00){

datetime myDate = datetime.now();



eventList.add (new Event(
Subject = 'Other',
WhatId = opp.Id,
OwnerId = opp.OwnerId,
StartDateTime = DateTime.Now(),
ActivityDateTime=null,
EndDateTime = DateTime.Now(),
DurationInMinutes = Integer.valueOf(Math.Floor( (myDate.getTime() - myDate.getTime()) / (1000.0*60.0)))

));

}
if(eventList.size() > 0)

insert eventList;

}

}

 

 

***************************

 

Can you please correct y code and let me know if any scenarios need to be added

 

Thanks in advance

Shiva Ramesh @ xcdhrShiva Ramesh @ xcdhr

Hi Steve

 

Probability % field Data type is Percent(3, 0). There is no decimal places. You can directly check the condition like this

 

if(opp.Probability !=null && opp.Probability>=50){
}

 

steve456steve456

My issue is not that .

Shiva Ramesh @ xcdhrShiva Ramesh @ xcdhr

Could you please explain your code. I thought that condition is not checking for greater than 50%, that's why its always creating event record.

sushant sussushant sus

i think you have to check in case of opportunity in update if probability is already 60.00 and some update it to 70.00 then it will again create event . this scenario you have check i tried to check .. have a look

 

trigger AutoEventCreate on Opportunity (after insert , after update) {

List <Event> eventList = new List <Event> ();
Set<Id> oppIds = new Set<Id>();
if(Trigger.isInsert) {
for(Opportunity opp:Trigger.new){
if(opp.Probability !=null && opp.Probability>=50.00){

datetime myDate = datetime.now();



eventList.add (new Event(
Subject = 'Other',
WhatId = opp.Id,
OwnerId = opp.OwnerId,
StartDateTime = DateTime.Now(),
ActivityDateTime=null,
EndDateTime = DateTime.Now(),
DurationInMinutes = Integer.valueOf(Math.Floor( (myDate.getTime() - myDate.getTime()) / (1000.0*60.0)))

));

}
if(eventList.size() > 0)

insert eventList;

}

}
If(Trigger.isUpdate)
{
for(Opportunity opp:Trigger.new){
if(opp.Probability !=null && opp.Probability>=50.00 )
{
Opportunity opp1=trigger.oldmap.get(opp.id);
if(opp1.probability<=50.00){
datetime myDate = datetime.now();



eventList.add (new Event(
Subject = 'Other',
WhatId = opp.Id,
OwnerId = opp.OwnerId,
StartDateTime = DateTime.Now(),
ActivityDateTime=null,
EndDateTime = DateTime.Now(),
DurationInMinutes = Integer.valueOf(Math.Floor( (myDate.getTime() - myDate.getTime()) / (1000.0*60.0)))

));

}
if(eventList.size() > 0)

insert eventList;

}

 

sushant

 



}
}

}

Discuss with meDiscuss with me

Try This 1.

 

It may Help You

 

trigger AutoEventCreate on Opportunity (after insert , after update) {

List <Event> eventList = new List <Event> ();


if(Trigger.isInsert || (Trigger.newMap.get(opp.id).Probability != Trigger.oldMap.get(opp.Id).Probability ){
            for(Opportunity opp:Trigger.new){
                     if(opp.Probability !=null && opp.Probability>=25.00){

                              datetime myDate = datetime.now();
                                eventList.add (new Event(
                                                                           Subject = 'Other',
                                                                            WhatId = opp.Id,
                                                                             OwnerId = opp.OwnerId,
                                                                              StartDateTime = DateTime.Now(),
                                                                           ActivityDateTime=null,
                                                                          EndDateTime = DateTime.Now(),
              DurationInMinutes = Integer.valueOf  (

                                                                         Math.Floor(

                                                                                           (myDate.getTime() - myDate.getTime()) / (1000.0*60.0)))
                                                                                           )

                                                                          );    // Formula In Bold Letter Always Return zero. Change It

              }
          if(eventList.size() > 0){
                     insert eventList;
          }

}

 }

 

Discuss with meDiscuss with me

Try this Toooo

 

trigger CreateEvent on Opportunity (after Insert,after Update) {
    list<Opportunity> listOpp = trigger.new;
    List <Event> eventList = new List <Event> ();
        
    for( Opportunity o : listOpp ){
        if(trigger.isinsert || trigger.newmap.get(o.id).Probability != trigger.Oldmap.get(o.id).Probability ){
             if(o.Probability >= 20.00){
                    eventList.add(new Event(Subject ='Other',StartDateTime=dateTime.now(),
                                        EndDateTime=dateTime.now()  ,ActivityDateTime =NULL, WhatId = o.Id ));
             }                           
        }
    }
    if(eventList.size() > 0){
          insert eventList;
    }
}

indyindy

Hi Steve,

 

I have just added if(oppIds.size() >0) to your code. Please let me know if it works.

 

trigger AutoEventCreate on Opportunity (after insert , after update) {
List <Event> eventList = new List <Event> ();
Set<Id> oppIds = new Set<Id>();
for(Opportunity opp:Trigger.new){
if(opp.Id!=null &&(Trigger.isInsert || (Trigger.newMap.get(opp.id).Probability != Trigger.oldMap.get(opp.Id).Probability))){

oppIds.add(opp.Id);
}

}

if(oppIds.size() >0)
{
for(Opportunity opp:Trigger.new){
if(opp.Probability !=null && opp.Probability>=25.00){

datetime myDate = datetime.now();

 

eventList.add (new Event(
Subject = 'Other',
WhatId = opp.Id,
OwnerId = opp.OwnerId,
StartDateTime = DateTime.Now(),
ActivityDateTime=null,
EndDateTime = DateTime.Now(),
DurationInMinutes = Integer.valueOf(Math.Floor( (myDate.getTime() - myDate.getTime()) / (1000.0*60.0)))

));

}
if(eventList.size() > 0)

insert eventList;

}
}
}

 

Thanks,

Indy.

steve456steve456

@sushant 

 

Your code is working fine but I have one scenario which is not working

 

 

When Probability is 60 event is created

 

When probability is updated to 70 event is created

 

But When probability is lowered to less than 70 event is NOT getting created

 

which should not be the case event should be created if the probability is greater than 25 and on change of probability