Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. @RestResource(urlmapping='/leadconvert/*')
  2. Global class Leadconvert {
  3. @HttpPost
  4. global static string post(string firstname,string lastname,string email,string company) {
  5. List<Lead> lead= [select firstname,lastname,email,company,isconverted from Lead where Firstname=:firstname AND Lastname=:lastname And Email =: email And isconverted=false];
  6. List<User> use = [select id from User where Username =: email];
  7. try {
  8. if(use.size()>0){
  9. return 'Given email is already used. please provide another email';
  10. }
  11. if(lead.size()>0 && use.size()==0){
  12. Database.LeadConvert lc = new database.LeadConvert();
  13. lc.setLeadId(lead[0].id);
  14. LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
  15. lc.setConvertedStatus(convertStatus.MasterLabel);
  16. Database.LeadConvertResult lcr = Database.convertLead(lc);
  17.  
  18. Account a=[select id,name,phone from Account where id=:lcr.getAccountId()];
  19. Contact cc=[select id,firstname,Lastname,email from Contact where id=:lcr.getContactId()];
  20. ID ProfileID = [Select id from Profile where id = '00e28000000OBjT'].id;
  21.  
  22. User u=new User();
  23. u.email=cc.email;
  24. u.contactid = lcr.getContactId();
  25. u.profileid = profileID;
  26. u.UserName=email;
  27. u.Alias=cc.firstname;
  28. u.CommunityNickName=lastname;
  29. u.TimeZoneSidKey='America/New_York';
  30. u.LocaleSidKey='en_US';
  31. u.EmailEncodingKey='ISO-8859-1';
  32. u.LanguageLocaleKey='en_US';
  33. u.FirstName = cc.firstname;
  34. u.LastName = cc.Lastname;
  35. insert u;
  36.  
  37. return u.id;
  38. }else {
  39. return 'Lead is not exit, please give valid values';
  40. }
  41. }catch(exception e){
  42. return e.getMessage();
  43. }
  44. }
  45. }
  46.  
  47. @isTest
  48. Private class Test_Leadconvert {
  49.  
  50. Static TestMethod void TestPostMethod1() {
  51. Account acc = new Account(Name = 'Exp');
  52. insert acc;
  53. Contact con = new Contact(Lastname = 'Lst Name', AccountId=acc.Id);
  54. insert con;
  55. ID ProfileID = [Select id from Profile where id = '00e28000000OBjT'].id;
  56.  
  57. User u=new User();
  58. u.email= 'variation1@gmail.com';
  59. u.contactid = con.Id;
  60. u.profileid = profileID;
  61. u.UserName= 'dummy@gmail.com';
  62. u.Alias= 'KL';
  63. u.CommunityNickName= 'perk55';
  64. u.FirstName = 'Test Name';
  65. u.LastName = 'KL';
  66. u.TimeZoneSidKey= 'America/New_York';
  67. u.LocaleSidKey= 'en_US';
  68. u.EmailEncodingKey= 'ISO-8859-1';
  69. u.LanguageLocaleKey= 'en_US';
  70. insert u;
  71. System.runAs(u) {
  72. RestRequest req = new RestRequest();
  73. RestResponse res = new RestResponse();
  74. req.requestURI = '/services/apexrest/leadconvert ';
  75. req.httpMethod = 'POST';
  76. RestContext.request = req;
  77. RestContext.response= res;
  78. String name = Leadconvert.post('Name1','lname','dummy@gmail.com','bizs');
  79. System.assertEquals('Given email is already used. please provide another email',name);
  80. }
  81. }
  82.  
  83. Static TestMethod void TestPostMethod2(){
  84.  
  85. Lead leed = new Lead(Firstname='Name2',Lastname='Example2',Email='myorg2@gmail.com',Company='My Org2',Status='Open - Not Contacted');
  86. insert leed;
  87. database.leadConvert lc = new database.leadConvert();
  88. lc.setLeadId(leed.id);
  89. leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
  90. lc.setConvertedStatus(convertStatus.MasterLabel);
  91. Database.LeadConvertResult lcr = Database.convertLead(lc);
  92.  
  93. Account a = [select id,name,phone from Account where id=:lcr.getAccountId()];
  94. Contact con = [select id,firstname,Lastname,email from Contact where id=:lcr.getContactId()];
  95.  
  96. User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
  97. ID ProfileID = [Select id from Profile where id = '00e28000000OBjT'].id;
  98. Id userId;
  99. System.runAs (thisUser) {
  100. User u = new User();
  101. u.email= 'variation1@gmail.com';
  102. u.contactid = con.Id;
  103. u.profileid = profileID;
  104. u.UserName= 'myorg2@gmail.com';
  105. u.Alias= 'KL';
  106. u.CommunityNickName= 'perk55';
  107. u.FirstName = 'Test Name';
  108. u.LastName = 'KL';
  109. u.TimeZoneSidKey= 'America/New_York';
  110. u.LocaleSidKey= 'en_US';
  111. u.EmailEncodingKey= 'ISO-8859-1';
  112. u.LanguageLocaleKey= 'en_US';
  113. insert u;
  114. userId = u.id;
  115. }
  116. RestRequest req = new RestRequest();
  117. RestResponse res = new RestResponse();
  118. req.requestURI = '/services/apexrest/leadconvert ';
  119. req.httpMethod = 'POST';
  120. RestContext.request = req;
  121. RestContext.response= res;
  122. String name = Leadconvert.post('Name2','Example2','dummy2@gmail.com','My Org2');
  123. System.assertEquals(userId,name); //error is coming here
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement