Advertisement
Guest User

Untitled

a guest
Feb 7th, 2017
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. public class BillingCalloutService {
  2. //Implement business and callout logic methods here
  3. @Future(callout=true)
  4. public static void callBillingService(List<Id> recordIds) {
  5. List<Project__c> projects = [Select Id, ProjectRef__c,Billable_Amount__c from Project__c Where Id = :recordIds];
  6.  
  7. List<Project__c> projectsToUpdate = new List<Project__c>();
  8.  
  9. String username = ServiceCredentials__c.getValues('BillingServiceCredential').Username__c;
  10. String password = ServiceCredentials__c.getValues('BillingServiceCredential').Password__c;
  11.  
  12. String auth = username + ':' + password;
  13. String encodedAuth = EncodingUtil.base64Encode(Blob.valueOf(auth));
  14.  
  15. for(Project__c p : projects) {
  16.  
  17. BillingServiceProxy.InvoicesPortSoap11 service = new BillingServiceProxy.InvoicesPortSoap11();
  18. service.inputHttpHeaders_x = new Map<String, String>();
  19. service.inputHttpHeaders_x.put('Authorization', 'Basic ' + encodedAuth);
  20.  
  21. BillingServiceProxy.project project = new BillingServiceProxy.project();
  22. project.username = username;
  23. project.password = password;
  24. project.projectRef = p.ProjectRef__c;
  25. project.billAmount = p.Billable_Amount__c;
  26.  
  27. if(service.billProject(project).equals('ok')){
  28. projectsToUpdate.add(new Project__c(ProjectRef__c = p.ProjectRef__c, Status__c = 'Billed'));
  29. }
  30. }
  31. shouldIRun.stopTrigger();
  32. upsert projectsToUpdate ProjectRef__c;
  33. }
  34. }
  35.  
  36. @IsTest
  37. private class BillingCalloutServiceTest {
  38.  
  39. @IsTest
  40. private static void testBillingCalloutService() {
  41. Account a = new Account(Name = 'Acme');
  42. insert a;
  43. Opportunity o = new Opportunity(Name='Test', StageName='Submitted Project', AccountId=a.Id, Amount=1000, CloseDate=Date.Today());
  44. insert o;
  45. Project__c p = new Project__c(Status__c='Running',Start_Date__c=Date.Today(),End_Date__c=Date.Today(),Billable_Amount__c=10000,ProjectRef__c='projectX',Opportunity__c=o.Id);
  46. insert p;
  47.  
  48. insert new ServiceCredentials__c(Name='BillingServiceCredential',Username__c='toto', Password__c='azerty');
  49.  
  50.  
  51. Test.setMock(HttpCalloutMock.class, new BillingCalloutServiceMock());
  52. Test.startTest();
  53. p.Status__c = 'Billable';
  54. update p;
  55. Test.stopTest();
  56. // runs callout and check results
  57. p = [select Status__c from Project__c where id =: p.id];
  58. System.assertEquals('Billed', p.Status__c);
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement