+ Start a Discussion
pru1pru1 

Running Territory Assignment Rules

Hi,
 
Is it possible to run all the territory assignment rules from an scontrol ?
If yes, how can this be achieved ?
 
Thank You
pru1pru1

Hi,

I need to run all territory assignment rules from an scontrol.

My code is as follows:

Code:
var accountResult = sforce.connection.query("SELECT Id, Name FROM Account LIMIT 1");
var accountRecords = accountResult.getArray("records");
var accountIs = accountRecords[0];                      
output = "<br>" + output + accountIs.Id + "  " + accountIs.Name + "<br>";                                         
var account = new sforce.SObject("Account");
account.id = accountIs.Id;
sforce.connection.assignmentRuleHeader = {}   
sforce.connection.assignmentRuleHeader.useDefaultRule = "true";    
updateResult = sforce.connection.update([account]);
if (updateResult[0].getBoolean("success")){
output = output + "Account with Id " + account.id + " updated<br>";
}          
else{
output = output + "Failed to update account " + updateResult[0];
}
state.output.innerHTML = output;

According to the AJAX toolkit developers guide the SOAP header option is used as follows: 

sforce.connection.assignmentRuleHeader = {}
sforce.connection.assignmentRuleHeader.useDefaultRule = "true";

This does not work, the rules do not run when the account is updated.

Can anyone please help. 

TomSnyderTomSnyder
The following code worked for me, not sure I see a difference from yours though, only difference is I'm only trying to refresh the rules for just the id passed in as an arg.

Code:
sforce.connection.sessionId="{!$Api.Session_ID}";

function refreshTerritoryRule(id) {
if ( id.substring(0,3)=='001' ) {
var acc=new sforce.SObject("Account");
acc.id=id;
sforce.connection.assignmentRuleHeader = {};
sforce.connection.assignmentRuleHeader.useDefaultRule = "true";
result=sforce.connection.update([acc]);
if (! result[0].getBoolean("success"))
alert('Unable to refresh Territory Rule...'+result[0]);
}
closeForm();
}
TomSnyderTomSnyder
We can now do this as of ver.15  via APEX. see Database.DMLOptions.