You need to sign in to do that
Don't have an account?
srinu namani 12
global class TerritoryAssignmentWebService {
public static boolean isRecursive = false;
@future(callout=true)
global static void invokeTerritoryAssignmentRules(Set<Id> accountIds, String sessionId, Map<Id,String> accountAssignedTerrMap){
isRecursive = true;
try{
system.debug('accountAssignedTerrMap Print>>>'+accountAssignedTerrMap);
system.debug('accountIds Print>>>'+accountIds);
HttpRequest req = new HttpRequest();
//Set HTTPRequest Method
req.setMethod('POST');
//Set HTTPRequest header properties
req.setHeader('content-type', 'text/xml');
req.setHeader('SOAPAction','""');
req.setEndpoint(Label.SFDC_Webservice_Endpoint);
String sobjectUpdateString = '';
FieldSalesProperties__c fsprops = FieldSalesProperties__c.getValues('IsRecursive');
if(fsprops!=null && fsprops.value__c=='False'){
if(accountIds!=null){
for(Id accId : accountIds){
sobjectUpdateString += '<urn:sObjects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Account">'
+ '<urn1:Id>' + accId + '</urn1:Id>'
+ '</urn:sObjects>';
}
}else if(accountAssignedTerrMap!=null){
system.debug('>>Inside else if'+accountAssignedTerrMap);
for(Id accId: accountAssignedTerrMap.keyset()){
sobjectUpdateString += '<urn:sObjects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Account">'
+ '<urn1:Id>' + accId + '</urn1:Id>'
+ '<urn1:Assigned_Territories__c>' + accountAssignedTerrMap.get(accId)+ '</urn1:Assigned_Territories__c>'
+ '</urn:sObjects>';
}
system.debug('fsprops ...'+fsprops);
sobjectUpdateString += '<urn:sObjects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="FieldSalesProperties__c">'
+ '<urn1:Id>' + fsprops.Id+ '</urn1:Id>'
+ '<urn1:Value__c>True</urn1:Value__c>'
+ '</urn:sObjects>';
}
}
//Preapre SOAP envelope
String body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com" '
+ 'xmlns:urn1="urn:sobject.enterprise.soap.sforce.com">'
+ '<soapenv:Header>'
+ '<urn:AssignmentRuleHeader>'
+ '<urn:useDefaultRule>true</urn:useDefaultRule>'
+ '</urn:AssignmentRuleHeader>'
+ '<urn:SessionHeader>'
+ '<urn:sessionId>' + sessionId + '</urn:sessionId>'
+ '</urn:SessionHeader>'
+ '</soapenv:Header>'
+ '<soapenv:Body>'
+ '<urn:update>'
+ sobjectUpdateString
+ '</urn:update>'
+ '</soapenv:Body>'
+ '</soapenv:Envelope>';
//Set the HTTPRequest body
system.debug('>>>> Value'+fsprops.value__c);
if(fsprops !=null && fsprops.value__c=='False'){
req.setBody(body);
Http http = new Http();
HTTPResponse res;
//Execute web service call here
system.debug('>>>>SobjString'+body );
System.debug('userinfo:'+Userinfo.getUserId());
System.debug('sessionId:'+UserInfo.getSessionId());
if (!Test.isRunningTest())
res = http.send(req);
}else{
fsprops.value__c = 'False';
update fsprops;
}
/** System.debug(res.toString());
System.debug('STATUS:'+body);
System.debug('STATUS:'+res.getStatus());
System.debug('STATUS_CODE:'+res.getStatusCode());
System.debug('STATUS_CODE:'+res.getBody());*/
}catch(Exception e){
ApplicationDebugLog.captureExceptionDetails(e);
}
}
}
How to write a test class when @Future annotation is used in the class.
global class TerritoryAssignmentWebService {
public static boolean isRecursive = false;
@future(callout=true)
global static void invokeTerritoryAssignmentRules(Set<Id> accountIds, String sessionId, Map<Id,String> accountAssignedTerrMap){
isRecursive = true;
try{
system.debug('accountAssignedTerrMap Print>>>'+accountAssignedTerrMap);
system.debug('accountIds Print>>>'+accountIds);
HttpRequest req = new HttpRequest();
//Set HTTPRequest Method
req.setMethod('POST');
//Set HTTPRequest header properties
req.setHeader('content-type', 'text/xml');
req.setHeader('SOAPAction','""');
req.setEndpoint(Label.SFDC_Webservice_Endpoint);
String sobjectUpdateString = '';
FieldSalesProperties__c fsprops = FieldSalesProperties__c.getValues('IsRecursive');
if(fsprops!=null && fsprops.value__c=='False'){
if(accountIds!=null){
for(Id accId : accountIds){
sobjectUpdateString += '<urn:sObjects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Account">'
+ '<urn1:Id>' + accId + '</urn1:Id>'
+ '</urn:sObjects>';
}
}else if(accountAssignedTerrMap!=null){
system.debug('>>Inside else if'+accountAssignedTerrMap);
for(Id accId: accountAssignedTerrMap.keyset()){
sobjectUpdateString += '<urn:sObjects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Account">'
+ '<urn1:Id>' + accId + '</urn1:Id>'
+ '<urn1:Assigned_Territories__c>' + accountAssignedTerrMap.get(accId)+ '</urn1:Assigned_Territories__c>'
+ '</urn:sObjects>';
}
system.debug('fsprops ...'+fsprops);
sobjectUpdateString += '<urn:sObjects xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="FieldSalesProperties__c">'
+ '<urn1:Id>' + fsprops.Id+ '</urn1:Id>'
+ '<urn1:Value__c>True</urn1:Value__c>'
+ '</urn:sObjects>';
}
}
//Preapre SOAP envelope
String body = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com" '
+ 'xmlns:urn1="urn:sobject.enterprise.soap.sforce.com">'
+ '<soapenv:Header>'
+ '<urn:AssignmentRuleHeader>'
+ '<urn:useDefaultRule>true</urn:useDefaultRule>'
+ '</urn:AssignmentRuleHeader>'
+ '<urn:SessionHeader>'
+ '<urn:sessionId>' + sessionId + '</urn:sessionId>'
+ '</urn:SessionHeader>'
+ '</soapenv:Header>'
+ '<soapenv:Body>'
+ '<urn:update>'
+ sobjectUpdateString
+ '</urn:update>'
+ '</soapenv:Body>'
+ '</soapenv:Envelope>';
//Set the HTTPRequest body
system.debug('>>>> Value'+fsprops.value__c);
if(fsprops !=null && fsprops.value__c=='False'){
req.setBody(body);
Http http = new Http();
HTTPResponse res;
//Execute web service call here
system.debug('>>>>SobjString'+body );
System.debug('userinfo:'+Userinfo.getUserId());
System.debug('sessionId:'+UserInfo.getSessionId());
if (!Test.isRunningTest())
res = http.send(req);
}else{
fsprops.value__c = 'False';
update fsprops;
}
/** System.debug(res.toString());
System.debug('STATUS:'+body);
System.debug('STATUS:'+res.getStatus());
System.debug('STATUS_CODE:'+res.getStatusCode());
System.debug('STATUS_CODE:'+res.getBody());*/
}catch(Exception e){
ApplicationDebugLog.captureExceptionDetails(e);
}
}
}
This command will trigger and finish all the future calls.
To recap you should do the following in your unit test: