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
ECoronaECorona 

Add the opportunity stage on Event

Greetings!

 

I'm trying to get the opp stage name into the Event and freeze the value, what i'm trying to do is to get a report of all activities of the opp by stage.

 

Is that possible?

 

Thank you.

Vinita_SFDCVinita_SFDC

Hello,

 

Create a report on report type "Activities with opportunities" and apply a filter on Oppotunity stage. Now access this report on visualforce page by Salesforce Analytics API.

 

Refer: http://blogs.developerforce.com/developer-relations/2013/09/using-the-salesforce-analytics-api-on-a-visualforce-page.html?app_data=%7B%22pi%22%3A%225249fadf7c959d0b42000002%22%2C%22pt%22%3A%22wall%22%7D

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

So, you can create a text field in Event object, and store the opportunity stage value in the event where the realted to field (what id)  id is opportunityid.

 

And hide this field from event page layout. So, at the time of report generation, you can use that field value.

 

Hope this will help you...!

 

Please don't forget to give kudos by clicking on the Star icon and mark this as a solution, if this works out.

ECoronaECorona
Hello KamatchiDeviR!

I add the text field but i don't know how to save the stage i was looking for the stagename field but i don't get the opp field list
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

Now, I understood what you want. That was so simple, Opportunity Stage field should be refered by the Field API name as StageName

 

Example:

trigger UpdateStageVal on Task(before insert, before update){

     for(Task t: trigger.new){

          Opportunity opp = [select id,name,StageName from Opportunity where id =: t.whatID limit 1];

          t.StageTextField__c = opp.StageName;

          //your code continues here

}

}

 

Hope this will help you...!

 

Please don't forget to give kudos by clicking on the Star icon and mark this as a solution, if this works out.

 

ECoronaECorona
WOW!!!! thank you so much my friend :D
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Thank you.

 

Also, please mark this as a solution.

ECoronaECorona
Just one question, i'll not recieved any error message if the event / task is not related to an opp, correct???
ECoronaECorona
Hello Friend! well i was testing and i get this when the task / event is not related to an opp

execution of BeforeInsert caused by: System.QueryException: List has no rows
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi,

 

There should be a slight modification in your code. Try the following.

 

Example:

trigger UpdateStageVal on Task(before insert, before update){

     for(Task t: trigger.new){

          List<Opportunity> opp = [select id,name,StageName from Opportunity where id =: t.whatID];

          if(opp.size() > 0){

             for(Opportunity o : opp){

                    t.StageTextField__c = o.StageName;

                     //your code continues here

              }

           }

     }

}

 

While using an object query, it will throw this error if there is no value exists. So, for this scenario always you need to use the List and check the size of the list before you do an operation.

 

Hope this will help you...!

 

Please don't forget to give kudos by clicking on the Star icon and mark this as a solution, if this works out.

 

puneet-mishrapuneet-mishra

That was very nice post but i was wondering what will happen if Opportunity is inserted in bulk, at that time there may be a chance of hitting the salesforce limit of soql queries, I am not an expert I have just started my carrer but I have learnt one thing so far that never fire soql query inside for loop as that's a very bad practice.

 

I have written a code, have a look at it and let me know if this solves your problem 

trigger Opportunity_on_Event on Opportunity (after insert, before update) {
    List<Event> insertBulkEvent = new List<Event>();
    
    /* This will execute When Opportunity is Inserted*/
    if(trigger.isInsert) {
        for(Opportunity Opp : trigger.new) {
            Event newEvent = new Event();
            newEvent.Subject = Opp.Name;
            newEvent.StartDateTime = Opp.CloseDate; //Assigne Date accordingly, This is done for your comfort
            newEvent.EndDateTime = Opp.CloseDate;  //Assigne Date accordingly, This is done for your comfort
            newEvent.Opportunity_Stage_Name__c = Opp.StageName;
            newEvent.WhatId = Opp.Id;
            insertBulkEvent.add(newEvent);
        }
        insert insertBulkEvent;
    }
    
    /* Below will Execute when Opportunity is Updated */
    if(trigger.IsUpdate) {
        List<Event> EventList = [Select Id, WhatId From Event Where WhatId IN: trigger.new];
        for(Event eve : EventList) {
            /* If Opportunity related to any Event Updated */
            if(trigger.newMap.containsKey(eve.WhatId)) {
                /* If Opportunity Stage is Upadated */
                eve.Opportunity_Stage_Name__c = trigger.newMap.get(eve.WhatId).StageName;
                /*  Can also map other fields if they are upadtes in opportunity and 
                those fields also needed to upadated in related events  */
            }
        }
        update EventList;
    }
}