In order for Territory Assignment rules to fire on Account creation and Update, I have got 2 Classes working (with Triggers), however I am struggling to create a Test class for these, any help would be greatly appreciated.
Class that calls the queueable callout:
public class RunTerritoryRulesCallout {
public static void RunTerritoryRulesCallout(List<Account> AccsToUpdate){
//run ETM assignment rules
Map<ID, Account> AcctMap = new Map<ID, Account>(AccsToUpdate);
Set<String> AccIDs = new Set<String>((List<String>) New List<ID>(AcctMap.keyset()));
if(!System.isQueueable()){
RunTerritoryRules Job = new RunTerritoryRules();
Job.accntIds = AccIDs;
System.debug('Queuing ETM assignments');
System.enqueueJob(job);
}
}
}
The callout class:
public class RunTerritoryRules implements Queueable, Database.AllowsCallouts {
public Set<String> accntIds = null;
private String sessionId = null;
public void execute(QueueableContext context) {
sessionId = Page.GetSessionId.getContent().toString();
List<String> lstAccString = new List<String>();
if(accntIds != null){
for(String accId:accntIds){
lstAccString.add(accountTag.replace('{ACCID}', accId));
}
}
requestTemplate = requestTemplate.replace('{ACCLISTS}', String.join(lstAccString, ' ')) ;
requestTemplate = requestTemplate.replace('{SESSID}', sessionId) ;
HttpRequest request = new HttpRequest();
request.setEndpoint(System.URL.getSalesforceBaseUrl().toExternalForm()+
'/services/Soap/u/41.0/'+UserInfo.getOrganizationId());
request.setMethod('POST');
request.setHeader('Content-Type', 'text/xml;charset=UTF-8');
request.setHeader('SOAPAction', '""');
request.setBody(requestTemplate);
String s = String.valueOf(new Http().send(request).getBodyDocument());
System.debug(s);
}
String accountTag = '<urn:sObjects> '+
'<urn1:type>Account</urn1:type> '+
'<urn1:Id>{ACCID}</urn1:Id> '+
'</urn:sObjects> ' ;
String requestTemplate = '<soapenv:Envelope '+
'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"'+
' xmlns:urn="urn:partner.soap.sforce.com"'+
' xmlns:urn1="urn:sobject.partner.soap.sforce.com">'+
'<soapenv:Header> '+
'<urn:AssignmentRuleHeader> '+
'<urn:useDefaultRule>true</urn:useDefaultRule> '+
'<urn:assignmentRuleId></urn:assignmentRuleId> '+
'</urn:AssignmentRuleHeader> '+
'<urn:SessionHeader> '+
'<urn:sessionId>{SESSID}</urn:sessionId> '+
'</urn:SessionHeader> '+
'</soapenv:Header> '+
'<soapenv:Body> '+
'<urn:update> '+
' {ACCLISTS}'+
'</urn:update> '+
'</soapenv:Body> '+
'</soapenv:Envelope>';}
RunTerritoryRules
implement an interface, and have a constructor or method that allows you to set an instance ofRunTerritoryRulesCallout
to use (a principle called dependency injection) so that you can write a test that says "hey, I know you'd normally run thingX. In this test though, run this other thing, ThingY that I have prep'd).