+ Start a Discussion
Kumar Rao 1Kumar Rao 1 

Code not working for Update Lead

Dear Folks,

Can some one please get me the logic.

Requirement: The below Integration code is working fine for Insert but unable to get it work for Update.. Your help is highly appreciated and thanks in advance.

Integration Code:
ServicesPostcodeanywhereCoUk.ArrayForWayPoints wpoint = new ServicesPostcodeanywhereCoUk.ArrayForWayPoints();           
            ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap pcode = new ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap();
            ServicesPostcodeanywhereCoUk.DistancesAndDirections_Interactive_Distance_v1_00_ArrayOfResults result = pcode.DistancesAndDirections_Interactive_Distance_v1_00('XXXX-FKXX-RX91-NT72',weblead.Pick_Up_Post_Code__c,weblead.Destination_Postal_Code__c,wpoint,'Fastest');       

Here is the code might need to be updated:           
Integer pickupdropdis = result.DistancesAndDirections_Interactive_Distance_v1_00_Results[0].TotalDistance/1600;

Regards,
Kumar
pbattissonpbattisson
Hi Kumar

Could you provide some more information, the code you have given is not sufficient to allow people to help you.

What have you tried? Where is the code being called from?

Paul
Kumar Rao 1Kumar Rao 1
Hello,

When user fills starting postcode and destination postcode in leads object, a field called distance to be automatically, hence we are integrating salesforce with 3rd party tool to get milage/distance.

Above sample code is used to parse postcodes and the last line gets results. The lines is working for create a new lead but can't work for update.

Can you please through a light please?

Thanks,
Kumar
pbattissonpbattisson
Hi Kumar

You haven't provided enough information still. You say working for a new Lead, do you mean calling it from a trigger? Where is this code being used? 
Kumar Rao 1Kumar Rao 1
Hello, here you go. *Trigger;* Trigger DistanceCalc on Lead(after insert){ Lead currentRecord = Trigger.new[0]; DistanceCalc.webServiceCal(currentRecord.id); *Code:* * global class DistanceCalc { @future(callout=true) public static void webServiceCal(Id leadid){ Lead weblead = [select id,Base_Point__c, Pick_Up_Post_Code__c,Destination_Postal_Code__c,Distance_miles__c,Pick_Up_Street__c,Pick_Up_City__c,Pick_Up_County__c,Pick_up_Country__c from lead where id=:leadid]; system.debug('Basepoint Miles in Qury ========>>>> '+weblead.Base_Point__c); system.debug('Pickup Post code in Qury ========>>>> '+weblead.Pick_Up_Post_Code__c); system.debug('Destination PostCode in Qury ========>>>> '+weblead.Destination_Postal_Code__c); system.debug('Distance Miles in Qury ========>>>> '+weblead.Distance_miles__c); try{ Integer basedistance, milagedestobase; If(!test.isRunningTest()){ //Pickup to destination point ServicesPostcodeanywhereCoUk.ArrayForWayPoints wpoint = new ServicesPostcodeanywhereCoUk.ArrayForWayPoints(); ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap pcode = new ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap(); ServicesPostcodeanywhereCoUk.DistancesAndDirections_Interactive_Distance_v1_00_ArrayOfResults result = pcode.DistancesAndDirections_Interactive_Distance_v1_00('WW94-XXX-RX91-XXX',weblead.Pick_Up_Post_Code__c,weblead.Destination_Postal_Code__c,wpoint,'Fastest'); Integer pickupdropdis = result.DistancesAndDirections_Interactive_Distance_v1_00_Results[0].TotalDistance/1600; if(weblead.Base_Point__c!=null){ //Base to Pickup Point ServicesPostcodeanywhereCoUk.ArrayForWayPoints wpointforbase = new ServicesPostcodeanywhereCoUk.ArrayForWayPoints(); ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap pcodebase = new ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap(); ServicesPostcodeanywhereCoUk.DistancesAndDirections_Interactive_Distance_v1_00_ArrayOfResults resultbase = pcodebase.DistancesAndDirections_Interactive_Distance_v1_00('*WW94-XXX-RX91-XXX*',weblead.Base_Point__c,weblead.Pick_Up_Post_Code__c,wpointforbase,'Fastest');* * basedistance = resultbase.DistancesAndDirections_Interactive_Distance_v1_00_Results[0].TotalDistance/1600; //destination point to Basepoint ServicesPostcodeanywhereCoUk.ArrayForWayPoints wpointdestobase = new ServicesPostcodeanywhereCoUk.ArrayForWayPoints(); ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap pcodedestobase = new ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap(); ServicesPostcodeanywhereCoUk.DistancesAndDirections_Interactive_Distance_v1_00_ArrayOfResults resultdestobase = pcodedestobase.DistancesAndDirections_Interactive_Distance_v1_00('*WW94-XXX-RX91-XXX*',weblead.Destination_Postal_Code__c,weblead.Base_Point__c,wpointdestobase,'Fastest'); * * milagedestobase = resultdestobase.DistancesAndDirections_Interactive_Distance_v1_00_Results[0].TotalDistance/1600; }else{ basedistance=0; milagedestobase=0; } weblead.Distance_miles__c = basedistance+pickupdropdis+milagedestobase; system.debug('Total miles ========>>>>>>> '+weblead.Distance_miles__c); FullPostCode.PostcodeAnywhere_Soap fullpostcodeaddress = new FullPostCode.PostcodeAnywhere_Soap(); FullPostCode.CapturePlus_Interactive_Retrieve_v2_10_ArrayOfResults resultaddress = fullpostcodeaddress.CapturePlus_Interactive_Retrieve_v2_10('MJ32-PZ43-KM19-AU86','GBR|'+weblead.Pick_Up_Post_Code__c); FullPostCode.CapturePlus_Interactive_Retrieve_v2_10_Results pickupaddress = resultaddress.CapturePlus_Interactive_Retrieve_v2_10_Results[0]; FullPostCode.CapturePlus_Interactive_Retrieve_v2_10_ArrayOfResults Destiaddress = fullpostcodeaddress.CapturePlus_Interactive_Retrieve_v2_10('MJ32-PZ43-KM19-AU86','GBR|'+weblead.Destination_Postal_Code__c); FullPostCode.CapturePlus_Interactive_Retrieve_v2_10_Results Desti_add = Destiaddress.CapturePlus_Interactive_Retrieve_v2_10_Results[0]; weblead.Pick_Up_Street__c = weblead.Street = pickupaddress.Street; weblead.Pick_Up_City__c = weblead.City = pickupaddress.City; weblead.Pick_Up_County__c = weblead.state = pickupaddress.ProvinceName; weblead.Pick_up_Country__c = weblead.Country = pickupaddress.CountryName; weblead.PostalCode = weblead.Pick_Up_Post_Code__c; weblead.Destination_Street__c = Desti_add.Street; weblead.Destination_City__c = Desti_add.City; weblead.Destination_County__c = Desti_add.ProvinceName; weblead.Destination_Country__c = Desti_add.CountryName; update weblead; } }catch(system.asyncexception e){ system.debug(e); } } }*