0

Trigger on Opportunity to update User object

I need to make a trigger on the opportunity object that Adds +1 to a custom filed on the user object every time an opportunity that the user owns has the "Date qualified" field changed from null to having a date.

What I am trying to do is get a count on the user object of how many opps are qualified MTD. Then I can run a report that will show how many reps that have zero opps along with the reps that have opps.

I tired making a workflow but the field update can't cross to the user object. And I am not sure were to start on this code. Any help would be great.

1

I would do this with a matrix report

  • Rows: Opportunity Owner
  • Columns: Date qualified (grouped by month)
  • Cell: Record count
  • Report filter: Date_qualified__c not equal null and date_qualified in Current Calendar year

If you store the count on User, you have to clear it out every month in order to get MTD

| improve this answer | |
0

based on your description a basic code snippet will be something like this to update custom field on user object on invoking Opportunity trigger:

trigger MyCoolTrigger on Opportunity (after insert) {

    List<User> userFieldUpdate = new List<User>();

    for(User u : [Select custom_field__c From User Where Id =: UserInfo.getUserId()]){
        u.custom_field__c = ''//your value;
        userFieldUpdate.add(u);
    }

    if(!userFieldUpdate.isEmpty()){
        update userFieldUpdate;
    }

}

This code is just to give a idea of how to approach your task. Specific to your task you will need to fetch the user ID of the opportunity owner using a SOQL query and then pass it in place of 'UserInfo.getUserId()' in the above SOQL query.

| improve this answer | |
0

Like @crop1645 suggested, I would also go the report route. You could do something like:

  1. create a new report type (Users with or without opportunities)
  2. create a formula checkbox that is set to true when it meets your criteria
  3. create a report with criteria that matches users you'd want to appear on the report
  4. summarize the report by user and sum the field created in 2 above

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.