I am pretty new to Apex and trying to learn as much as possible. Any help would be really appreciated. I have two classes.
Invokeable Apex Class: This class is just getting called by a Process (Builder) if lead object meets certain criteria and sending the lead record values like , ID, email, First Name, Last name to another class which is Future Callout class.
@future Apex class is calling a POST rest API (sending the lead data) to an external system and receiving an ID which updates the lead record.
Both the classes are working as expected. I am trying to create Apex Test Class for Both of them but facing issues as i never wrote apex Test class earlier.
Here are the classes
1- Invokable class
public class MakeCallout {
@InvocableMethod
public static void invokeleadcallout(list<Lead> Leads) {
//below is the Futurecallout Method
WS_Lead.Notification(Leads[0].id, Leads[0].Email, Leads[0].Firstname);
}
}
2- WS callout class
global class WS_Lead {
@future (callout=true)
// Receiving the Lead details from Invokable class ApexCallout.
WebService static void Notification(id lid, String name)
{
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
req.setEndpoint('https://google.com');
//Setting Method Content and Body
req.setMethod('POST');
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setBody(
'name='+EncodingUtil.urlEncode(name, 'UTF-8')
);
req.setTimeout(120000);
try {
res = http.send(req);
System.debug(res.getbody());
Dom.Document docx = new Dom.Document();
docx.load(res.getbody());
dom.XmlNode xroot = docx.getrootelement();
String U_Id = xroot.getAttributeValue('U_Id', null);
system.debug(U_Id );
Lead le = new Lead(id=lid);
le.ID__c = U_Id;
update le;
}
catch(System.CalloutException e) {
System.debug('Callout error: '+ e);
System.debug(res.toString());
res.getbody();
}
}
}