Newer Version Available

This content describes an older version of this product. View Latest

Assign Records to Owners Using an Assignment Plan

The AssignRecords() Apex method assigns records to users automatically using rules specified in an assignment plan. For example, suppose that you use a web-to-lead generation form to generate inbound leads in Salesforce. You use the AssignRecords() method to assign those leads to sales reps as soon as the lead records are created, reducing the time for sales reps to contact leads.

Signature

1String, List<sObject> maps.API.AssignRecords(String, List<sObject>)
Where,
  • maps is the namespace that's available after you install Salesforce Maps.
  • API is the class that contains the global methods exposed to developers.
  • AssignRecords() is the method.

Sample Code

This code assigns accounts to reps based on an existing assignment plan. The AssignRecords() method returns a List<sObject> containing the records that were assigned to an assignment plan, the plan ID, and the records that weren't assigned to an assignment plan.

The AssignRecords() global method uses the last record cache to assign records. As a best practice, schedule assignment of records frequently in a batch to keep the cache up to date. For more information, see Salesforce Help: Schedule and Run Record Assignments.

When you invoke this global method with a trigger or process builder, use the following sample code as a guide. Not following the sample code can result in an infinite loop when your code runs.

Warning

Example

1// Get the record ID of the assignment plan.
2maps__AssignmentPlan__c plan = [SELECT Id FROM maps__AssignmentPlan__c];
3
4// Get a list of records you want to assign.
5// This query includes all fields mapped in the plan.
6List<Account> accs = [SELECT Id, BillingLatitude, BillingLongitude FROM Account];
7
8// Call the AssignRecords method with the plan ID and list of account records.
9maps.API.AssignRecords(plan.Id, accs);
10
11// Log the record assignment output in JSON format.
12Map<String, Object> response = maps.API.AssignRecords(plan.Id, accs);
13system.debug(JSON.serialize(response));

Invocable Example

1public class InvocableTest {
2    
3    @future(callout=true)
4    public static void testInvocableFuture(List<Id> accIds) {
5    // This query includes all fields mapped in the plan.
6        List<Account> accs = [SELECT Id, BillingLatitude, BillingLongitude, Description, maps__AssignmentRule__c FROM Account WHERE Id IN :accIds];
7
8        // Call the AssignRecords method with the plan ID and list of account records.
9        maps.API.AssignRecords('a0L54000007oHXqEAM', accs);
10
11        // Log the record assignment output in JSON format.
12        Map<String, Object> response = maps.API.AssignRecords(plan.Id, accs);
13        system.debug(JSON.serialize(response));
14    }
15
16    public with sharing class testInvocableQueueable implements Queueable, Database.AllowsCallouts {
17        List<Id> accIds;
18
19        public testInvocableQueueable(List<Id> accIds) {
20            this.accIds = accIds;
21        }
22
23        public void execute(QueueableContext qc) {
24            // This query includes all fields mapped in the plan.
25            List<Account> accs = [SELECT Id, BillingLatitude, BillingLongitude, Description, maps__AssignmentRule__c FROM Account WHERE Id IN :accIds];
26
27            // Call the AssignRecords method with the plan ID and list of account records.
28            maps.API.AssignRecords('a0L54000007oHXqEAM', accs);
29
30            // Log the record assignment output in JSON format.
31            Map<String, Object> response = maps.API.AssignRecords(plan.Id, accs);
32            system.debug(JSON.serialize(response));
33        }
34    }
35
36    @InvocableMethod(label='test invocable' description='test auto assign' callout=true)
37    public static void testInvocable(List<Id> accIds) {
38        
39        // CALLING A FUTURE METHOD WORKS.
40        //testInvocableFuture(accIds);
41        
42        // CALLING A QUEUEABLE METHOD WORKS.
43        //System.enqueueJob(new testInvocableQueueable(accIds));
44        
45        // THIS WON’T WORK AS AN INVOCABLE METHOD. A CALLOUT INSIDE OF A RECORD UPDATE OR INSERT RESULTS IN AN UNCOMMITTED WORK ERROR.
46        // INSTEAD, SEPARATE THE TRANSACTION INTO A FUTURE OR QUEUEABLE METHOD.
47        //List<Account> accs = [SELECT Id, BillingLatitude, BillingLongitude, Description, maps__AssignmentRule__c FROM Account WHERE Id IN :accIds];
48        //maps.API.AssignRecords('a0L54000007oHXqEAM', accs);
49    }
50}

Trigger Example

1// FOR INSERTS, AFTER WORKS BETTER THAN BEFORE BECAUSE THE ID DOESN’T EXIST IN THE BEFORE CHECK.
2trigger AccountTrigger on Account (after insert, after update) {
3    
4    List<Id> accIds = new List<Id>();
5
6    for (Account acc : Trigger.new) {
7        if (acc.maps__AssignmentRule__c == null) {
8            accIds.add(acc.Id);
9        }
10    }
11    
12    // CALLING A QUEUEABLE METHOD WORKS.
13    //if (!accIds.isEmpty()) {
14        //System.enqueueJob(new AccountTriggerHelper.testTriggerQueueable(accIds));
15    //}
16    
17    // THIS WON’T WORK AS A TRIGGER.
18    //List<Account> accs = [SELECT Id, BillingLatitude, BillingLongitude, Description, maps__AssignmentRule__c FROM Account WHERE Id IN :accIds];
19    //maps.API.AssignRecords('a0L54000007oHXqEAM', accs);
20    
21    // CALLING A FUTURE METHOD WON’T WORK.
22    //AccountTriggerHelper.testTriggerFuture(accIds);
23    
24}
1public class AccountTriggerHelper {
2    
3    @future(callout=true)
4    public static void testTriggerFuture(List<Id> accIds) {
5    //  This query includes all fields mapped in the plan.
6        List<Account> accs = [SELECT Id, BillingLatitude, BillingLongitude, Description, maps__AssignmentRule__c FROM Account WHERE Id IN :accIds];
7        // Call the AssignRecords method with the plan ID and list of account records.
8        maps.API.AssignRecords('a0L54000007oHXqEAM', accs);
9    }
10
11    public with sharing class testTriggerQueueable implements Queueable, Database.AllowsCallouts {
12        List<Id> accIds;
13
14        public testTriggerQueueable(List<Id> accIds) {
15            this.accIds = accIds;
16        }
17
18        public void execute(QueueableContext qc) {
19        // This query includes all fields mapped in the plan.
20            List<Account> accs = [SELECT Id, BillingLatitude, BillingLongitude, Description, maps__AssignmentRule__c FROM Account WHERE Id IN :accIds];
21            // Call the AssignRecords method with the plan ID and list of account records.
22            maps.API.AssignRecords('a0L54000007oHXqEAM', accs);
23
24        // Log the record assignment output in JSON format.
25        Map<String, Object> response = maps.API.AssignRecords(plan.Id, accs);
26        system.debug(JSON.serialize(response));
27        }
28    }
29
30}

Sample Response

This method returns an Apex Map<String, Object> object that contains the records assigned, the plan ID, and records not assigned. This JSON response illustrates the essential data returned.

1{
2    assigned: [
3        {
4            id="0011700001LTxzwAAD", // ID of record assigned.
5            rule="0021700001LTxzwAAD", // ID of rule used for assignment.
6            user="0031700001LTxzwAAD" // ID of user assigned to the matched rule.
7        },
8        {
9            id="0011700001LTxzwAAG",
10            rule="0021700001LTxzwAAD",
11            user="0031700001LTxzwAAD"
12        }
13    ],
14    plan: "a0L54000007oHXqEAM", // Assignment plan ID
15    unassigned: ["0011700001LTxzwAAE", "0011700001LTxzwAAF"] // List of record IDs not assigned.
16}