+ Start a Discussion
ALAL 

Round robin assignment on opportunity object

I would like to create a round assignment on the opportunity object. So when an opportunity is created, an account name will be added to the opportunity (based off of a picklist of three account names). I see that there isn't an assignment rule option on the opportunity object, so I created an assignment rule on the case object for the account names. Right now I'm stuck on the soql statement [Select ID From Account Where Name in: oppList.id]. I receive a debug statement of Variable does not exist: id.  Is there a way for me to reference the assignment rule id so that it loops through the three account names?

trigger NewAcc on Opportunity (after insert) {
   
    List<Opportunity> oppList = new  List<Opportunity>();
    AssignmentRule ar = new AssignmentRule();
    ar = [SELECT ID FROM AssignmentRule where SobjectType = 'Case'and ID = '01Q360000001zcv' and Active = true limit 1];
      
  Case newCase = new Case(Status = 'new');
  Database.DMLOptions dmlOpts = new Database.DMLOptions();
        newCase.setOptions(dmlOpts);    

  Account acct = [SELECT Id From Account Where NAME in: oppList.id  ]

    for(Opportunity opp: Trigger.new){
          
        opp.AccountId = acct.Id;
        oppList.add(opp);
 
    }
insert newCase;
insert oppList;
}
Best Answer chosen by AL
HARSHIL U PARIKHHARSHIL U PARIKH
Hello Albert,

I would say you do not need a Trigger for that. In my opinion trigger should be a someones last option when all standard functionality fails.
But luckly in your case it is not failing. :) 

Follow the following process,

1) Create a field named "Opportunity Auto Number" on Opportunity object. This field will have data type as Auto Number. Display format is {0}. And make sure the checkbox named "Generate Auto Number for existing records" is NOT CHECKED FOR NOW.
2) Now create a formula field named "Name of Account" which has the following formula.
CASE( MOD( VALUE( Opportunity_Auto_Number__c ) ,3) + 1 , 
1, "Account #1", 
2, "Account #2", 
3, "Account #3", 
null)
now of course, instead of Account #1, and #2, and #3, write your actual name of the account inside the doble quotation.

3) Now let's say you create a 7 new opportunities. This is what you would see,

  Opportunity Record- 1 with field "Opportunity Auto Number"  = 1 and field "Name Of Account" = Account #1
  Opportunity Record- 2 with field "Opportunity Auto Number"  = 2 and field "Name Of Account" = Account #2
  Opportunity Record- 3 with field "Opportunity Auto Number"  = 3 and field "Name Of Account"" = Account #3
  Opportunity Record- 4 with field "Opportunity Auto Number"  = 4 and field "Name Of Account"" = Account #1
  Opportunity Record- 5 with field "Opportunity Auto Number"  = 5 and field "Name Of Account" = Account #2
  Opportunity Record- 6 with field "Opportunity Auto Number"  = 6 and field "Name Of Account" = Account #3
  Opportunity Record- 7 with field "Opportunity Auto Number"  = 7 and field "Name Of Account"" = Account #1

And so on and so on.... 

Now, this is done for the new records, but how about the past ones?
If above approach looks okay and reliable to you then remember that checkbox I said leave it unchecked?! We need to make that checked means go back to that Auto Number Generated field named "Opportunity Auto Number" and check the checkbox named "Generate Auto Number for existing records." Once done, this will auto populate "Name Of Account" field for all past existing records and it will look something like this:

User-added image

Hope this helps!

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Can you tell us your requirement in little depth?
So you want to have Account Name field automatically get populated on Opportunity record. Now, this account name would be a picklist field which would have three options to choose from.
What are the criteria which identifies that account name should be one of those picklist value?
Your trigger is in after insert and you are inserting opportunity record again so it will goes in infinite loop though.
Again, tell us the requirement and you might not need a trigger.
ALAL
Thank you Govind. It is a little counterintuitive, but the idea is that an opportunity comes in and then it is assigned to an account in a round robin order. I created a case assignment rule and specifieid just three of our accounts so I could test it out. So if an opportunity comes in then it's assigned to account #1, a new opportunity will be assigned to account #2, the third opportunity to account #3, and then the fourth opportunity will be assigned back to account #1 again. 

Would a before trigger work? Thank you for your help. 
HARSHIL U PARIKHHARSHIL U PARIKH
Hello Albert,

I would say you do not need a Trigger for that. In my opinion trigger should be a someones last option when all standard functionality fails.
But luckly in your case it is not failing. :) 

Follow the following process,

1) Create a field named "Opportunity Auto Number" on Opportunity object. This field will have data type as Auto Number. Display format is {0}. And make sure the checkbox named "Generate Auto Number for existing records" is NOT CHECKED FOR NOW.
2) Now create a formula field named "Name of Account" which has the following formula.
CASE( MOD( VALUE( Opportunity_Auto_Number__c ) ,3) + 1 , 
1, "Account #1", 
2, "Account #2", 
3, "Account #3", 
null)
now of course, instead of Account #1, and #2, and #3, write your actual name of the account inside the doble quotation.

3) Now let's say you create a 7 new opportunities. This is what you would see,

  Opportunity Record- 1 with field "Opportunity Auto Number"  = 1 and field "Name Of Account" = Account #1
  Opportunity Record- 2 with field "Opportunity Auto Number"  = 2 and field "Name Of Account" = Account #2
  Opportunity Record- 3 with field "Opportunity Auto Number"  = 3 and field "Name Of Account"" = Account #3
  Opportunity Record- 4 with field "Opportunity Auto Number"  = 4 and field "Name Of Account"" = Account #1
  Opportunity Record- 5 with field "Opportunity Auto Number"  = 5 and field "Name Of Account" = Account #2
  Opportunity Record- 6 with field "Opportunity Auto Number"  = 6 and field "Name Of Account" = Account #3
  Opportunity Record- 7 with field "Opportunity Auto Number"  = 7 and field "Name Of Account"" = Account #1

And so on and so on.... 

Now, this is done for the new records, but how about the past ones?
If above approach looks okay and reliable to you then remember that checkbox I said leave it unchecked?! We need to make that checked means go back to that Auto Number Generated field named "Opportunity Auto Number" and check the checkbox named "Generate Auto Number for existing records." Once done, this will auto populate "Name Of Account" field for all past existing records and it will look something like this:

User-added image

Hope this helps!
This was selected as the best answer
HARSHIL U PARIKHHARSHIL U PARIKH
And Albert, if an above answer solves your question then please mark it as best answer since it will help other users who has a smililar issues for any object.

Thank You!
ALAL
Thank you Govind. This worked. 
Akhil KunaAkhil Kuna
Hi Govind, AL

I want to create a Round-robin on contact object. Unfortunatly there is no direct option to do so in salesforce. I have 6 users, I want to assign all the existing and all new contacts amoung those 6 agents. Please provide your thoughts.

Thanks in Advance  
ALAL
Hi Akhil, 

I'm not sure if this would work, but perhaps you could create a custom object (e.g. Custom Contact Object) and then link that to the standard Contact object by creating a lookup or master-detail custom field on the Custom Contact Object.

From there you could assign a queue for your six users. You could then create a workflow on the Custom Contact Object that would  run anytime a record is created and the criteria evaluates to true. I saw an article that may assist with the apex code that would be needed for the assignement rule. 

https://sfdcpanther.wordpress.com/2017/09/03/round-robin-assignment-using-trigger-and-workflow-rule/
 
Sibanee purohitSibanee purohit
Hi Govind,I have similar requirement.whenever a contact is created that needs to be assigned to users using round robin formula like we do for lead or case but here I need to create assignment rule on contact.How to do that?
Michael MarrMichael Marr
Hi Govind! This helps alot, now I have a question for you. How can I apply this method to number only a specific opportunity record type for the round robin? We have some record types that are not round robin but the current method applies it to all of them.
edralphedralph
Programmatic approaches to the round robin problem are fine when the scenario is relatively simple.  Soon though, what happens is the users ask if they can have weighted distribution, or multiple groups of round robin users, or more complicated criteria for record segmentation.  At that point it becomes harder and harder to maintain a home-grown solution.

I created a solution that is available on the AppExchange (yes this is a shamless self-plug) that automates lead assignment for any object in Salesforce (including Opportunities, Leads, Accounts, Cases and Contacts) and has features such as immediate alerts, weighted distribution, capping, out-of-office, proper segmentation of leads or cases and more.  I have many happy customers that have tried to do their own round robin solution, some spending months doing so.  They were delighted when all their problems were solved ten minutes after installing SuperRoundRobin.

It is upgraded regularly and admins regularly review it as a great app.  Read the reviews for yourself:
https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000FR4MkUAL

Thank you.