I have written a callout class as below.But when a account is created/updated am getting the exception like "You have uncommitted work pending. Please commit or rollback before calling out" at 36 line(response = httpProtocol.send(request); ).But am able to see the correct ouput as I have written try , catch but the client is getting exception mails even when the output is correct.
Please help guys, would be waiting for your reply!! Appreciate the help!!
global with sharing class AccTrigHandler
{
Public Static list<account> accToUpdate = new list<account>();
@future(callout=true)
public static void OnAfterInsertForAcc(set<Id> newAccs)
{
insertFunction(newAccs);
}
public static void insertFunction(set<Id> records)
{
for(Account acc : [select id,Other_Zip_Postal_Code__c,Mailing_Zip_Postal_Code__c from Account where id IN:records and (Mailing_Zip_Postal_Code__c!= null OR Other_Zip_Postal_Code__c != null)])
{
getCityAndStateName(acc,acc.Mailing_Zip_Postal_Code__c,acc.Other_Zip_Postal_Code__c);
}
}
public static void getCityAndStateName(Account acc,String mailingZipCode,String otherZipCode)
{
system.debug('______mailingZipCode______'+mailingZipCode);
system.debug('______otherZipCode______'+otherZipCode);
String urlvalue = 'xxx';
String urlvalue1 = 'xxx';
//boolean check =String.isEmpty(mailingZipCode);
if(mailingZipCode!=null && mailingZipCode!='')
{
urlValue += mailingZipCode+'&format=xml';
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
HttpResponse response;
request.setEndPoint(urlValue );
request.setMethod('GET');
if(!Test.isRunningTest())
{
response = httpProtocol.send(request); //getting error in this line
system.debug('________response___________'+response);
system.debug('________response xml___________'+response.getBody());
String xmlString = response.getBody();
if(xmlString.contains('geoCity') && xmlString.contains('geoState') )
{
Dom.Document docx = new Dom.Document();
docx.load(xmlString);
String city = docx.getrootelement().getChildElement('response', null).getChildElement('geoCity', null).gettext();
String state = docx.getrootelement().getChildElement('response', null).getChildElement('geoState', null).getText();
if(city!=null)
{
city = docx.getrootelement().getChildElement('response', null).getChildElement('geoCity', null).gettext();
integer leng = city.length();
string CaptCity = city.toLowerCase();
if(mailingZipCode!=null)
{
acc.Mailing_City__c = CaptCity.subString(0,1).toUpperCase()+CaptCity.subString(1,leng);
}
}
if(state!=null)
{
acc.Mailing_State__c = docx.getrootelement().getChildElement('response', null).getChildElement('geoState', null).getText();
}
system.debug('_______ acc.Mailing_City__c_______'+acc.Mailing_City__c );
system.debug('_______ acc.Mailing_State__c_______'+ acc.Mailing_State__c);
}
}
}
if(otherZipCode!=null && otherZipCode!='')
{
urlValue1 += otherZipCode+'&format=xml';
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
HttpResponse response;
request.setEndPoint(urlValue1);
request.setMethod('GET');
if(!Test.isRunningTest())
{
response = httpProtocol.send(request); //getting error in this line
system.debug('________response___________'+response);
system.debug('________response xml___________'+response.getBody());
String xmlString = response.getBody();
if(xmlString.contains('geoCity') && xmlString.contains('geoState') )
{
Dom.Document docx = new Dom.Document();
docx.load(xmlString);
String otherCity= docx.getrootelement().getChildElement('response', null).getChildElement('geoCity', null).gettext();
String OtherState= docx.getrootelement().getChildElement('response', null).getChildElement('geoState', null).getText();
if(otherCity!=null)
{
otherCity= docx.getrootelement().getChildElement('response', null).getChildElement('geoCity', null).gettext();
integer leng = otherCity.length();
string CaptCity = otherCity.toLowerCase();
acc.Other_City__c = CaptCity.subString(0,1).toUpperCase()+CaptCity.subString(1,leng);
}
if(OtherState!=null)
{
acc.Other_State__c = docx.getrootelement().getChildElement('response', null).getChildElement('geoState', null).getText();
}
system.debug('_______ acc.Other_City__c_______'+acc.Other_City__c);
system.debug('_______ acc.Other_State__c_______'+ acc.Other_State__c);
}
}
}
accToUpdate.add(acc);
try
{
IF(accToUpdate.size()>0)
{
system.debug('__________in if_________'+accToUpdate);
CallToZipTaxUtil.stopTrigger();
update accToUpdate;
}
}
catch(Exception e)
{
system.debug('_____________Exception__________'+e);
}
}
}