Account Assignment Rules in Salesforce.com via webservice
To run the Territory rule assignment
We need to use webservice call out
To do this, we must import the partner wsdl and generate helper apex classes.We need to use webservice call out
http://developer.force.com/cookbook/recipe/calling-salesforce-web-services-using-apex
And then after insert or update of Account, pass the all accounts in fireTerritoryManagementViaSoap method.
global class TerritoryMangRule
{
webService static void fireTerritoryManagementViaSoap(List<Account> lstAccountToUpdate)
{
partnerSoapSforceCom.Soap obj = new partnerSoapSforceCom.Soap();
partnerSoapSforceCom.LoginResult loginResult = obj.login('org@username.com', 'Password');
obj.SessionHeader = new partnerSoapSforceCom.SessionHeader_element();
obj.endpoint_x =loginResult.ServerUrl;
obj.Sessionheader.sessionid = loginResult.sessionid;
List<sobjectPartnerSoapSforceCom.sObject_x> lst = new List<sobjectPartnerSoapSforceCom.sObject_x>();
for(Account ac:lstAccountToUpdate)
{
sobjectPartnerSoapSforceCom.sObject_x tmpObj = new sobjectPartnerSoapSforceCom.sObject_x();
tmpObj.type_x = 'Account';
tmpObj.Id = ac.Id;
lst.add(tmpObj);
}
partnerSoapSforceCom.AssignmentRuleHeader_element obj1 = new partnerSoapSforceCom.AssignmentRuleHeader_element();
obj1.useDefaultRule = true;
obj.AssignmentRuleHeader = obj1;
partnerSoapSforceCom.SaveResult[] lst1 =obj.update_x(lst);
}
}