0

Please tell me how to create a Roll-Up Summary Field in the user object ? I can't create this field because I need a master-detail relationship " you can't create this type of field on this object because it is not the master in a master-detail relationship."but I'm not a magician to create a relationship master-detail to the User object.

  • What exactly are you trying to roll up to the User? – David Reed May 29 '18 at 15:56
  • "List of My Orders" button to display a list of already made orders. Clicking this button should open a popup dialog with the list all orders made, as well as the amount of the current order. Running sum the made order must be stored in a custom field in the User object. – user57048 May 29 '18 at 16:02
2

There's a bunch of tools for this. We use this. I believe that there are alot of users here on Salesforce Stack Exchange that use this one.

The main idea is to create a field on the user that you want to store data in such as a number field or text field. Then you create an apex trigger so that every time a child record is created/updated, it goes to the parent record - the user in your case - and updates the field using an aggregate query.

Here's a sample of the one I use taken from their GitHub page.

trigger OppRollup on Opportunity (
    after insert, after update, after delete, after undelete
) {
      // modified objects whose parent records should be updated
     Opportunity[] objects = null;   

     if (Trigger.isDelete) {
         objects = Trigger.old;
     } else {
        /*
            Handle any filtering required, specially on Trigger.isUpdate event. If the rolled up fields
            are not changed, then please make sure you skip the rollup operation.
            We are not adding that for sake of similicity of this illustration.
        */ 
        objects = Trigger.new;
     }

     /*
      First step is to create a context for LREngine, by specifying parent and child objects and
      lookup relationship field name
     */
     LREngine.Context ctx = new LREngine.Context(
         Account.SobjectType, // parent object
         Opportunity.SobjectType,  // child object
         Schema.SObjectType.Opportunity.fields.AccountId // relationship field name
     );  

     /*
      Next, one can add multiple rollup fields on the above relationship. 
      Here specify 
       1. The field to aggregate in child object
       2. The field to which aggregated value will be saved in master/parent object
       3. The aggregate operation to be done i.e. SUM, AVG, COUNT, MIN/MAX
     */
     ctx.add(
            new LREngine.RollupSummaryField(
                Schema.SObjectType.Account.fields.AnnualRevenue,
                Schema.SObjectType.Opportunity.fields.Amount,
                LREngine.RollupOperation.Sum 
            )
     ); 

     ctx.add(
            new LREngine.RollupSummaryField(
                Schema.SObjectType.Account.fields.SLAExpirationDate__c,
                Schema.SObjectType.Opportunity.fields.CloseDate,
                LREngine.RollupOperation.Max
            )
     );                                       

     /* 
      Calling rollup method returns in memory master objects with aggregated values in them. 
      Please note these master records are not persisted back, so that client gets a chance 
      to post process them after rollup
      */ 
     Sobject[] masters = LREngine.rollUp(ctx, objects);    

     // Persiste the changes in master
     update masters;
}

The second one uses a user interface for configuration and then automatically creates the trigger and uploads it to your org. For information on using it please review the link I posted above.

  • Schema.SObjectType.Opportunity.fields.AccountId == Opportunity.AccountId. There's no real reason to be that formal in most code. – sfdcfox May 29 '18 at 16:10
  • Not my code, taken from the GitHub page of the app. But good info none the less – gNerb May 29 '18 at 16:12
0

It is just a platform limitation and It is not possible to use user object as master object. You may do the following

  1. create a lookup relationship on that object
  2. make that field required on the related object and
  3. Use trigger to update your roll-up value on user object.

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.