Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RestResource(urlmapping='/leadconvert/*')
- Global class Leadconvert {
- @HttpPost
- global static string post(string firstname,string lastname,string email,string company) {
- List<Lead> lead= [select firstname,lastname,email,company,isconverted from Lead where Firstname=:firstname AND Lastname=:lastname And Email =: email And isconverted=false];
- List<User> use = [select id from User where Username =: email];
- try {
- if(use.size()>0){
- return 'Given email is already used. please provide another email';
- }
- if(lead.size()>0 && use.size()==0){
- Database.LeadConvert lc = new database.LeadConvert();
- lc.setLeadId(lead[0].id);
- LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
- lc.setConvertedStatus(convertStatus.MasterLabel);
- Database.LeadConvertResult lcr = Database.convertLead(lc);
- Account a=[select id,name,phone from Account where id=:lcr.getAccountId()];
- Contact cc=[select id,firstname,Lastname,email from Contact where id=:lcr.getContactId()];
- ID ProfileID = [Select id from Profile where id = '00e28000000OBjT'].id;
- User u=new User();
- u.email=cc.email;
- u.contactid = lcr.getContactId();
- u.profileid = profileID;
- u.UserName=email;
- u.Alias=cc.firstname;
- u.CommunityNickName=lastname;
- u.TimeZoneSidKey='America/New_York';
- u.LocaleSidKey='en_US';
- u.EmailEncodingKey='ISO-8859-1';
- u.LanguageLocaleKey='en_US';
- u.FirstName = cc.firstname;
- u.LastName = cc.Lastname;
- insert u;
- return u.id;
- }else {
- return 'Lead is not exit, please give valid values';
- }
- }catch(exception e){
- return e.getMessage();
- }
- }
- }
- @isTest
- Private class Test_Leadconvert {
- Static TestMethod void TestPostMethod1() {
- Account acc = new Account(Name = 'Exp');
- insert acc;
- Contact con = new Contact(Lastname = 'Lst Name', AccountId=acc.Id);
- insert con;
- ID ProfileID = [Select id from Profile where id = '00e28000000OBjT'].id;
- User u=new User();
- u.email= 'variation1@gmail.com';
- u.contactid = con.Id;
- u.profileid = profileID;
- u.UserName= 'dummy@gmail.com';
- u.Alias= 'KL';
- u.CommunityNickName= 'perk55';
- u.FirstName = 'Test Name';
- u.LastName = 'KL';
- u.TimeZoneSidKey= 'America/New_York';
- u.LocaleSidKey= 'en_US';
- u.EmailEncodingKey= 'ISO-8859-1';
- u.LanguageLocaleKey= 'en_US';
- insert u;
- System.runAs(u) {
- RestRequest req = new RestRequest();
- RestResponse res = new RestResponse();
- req.requestURI = '/services/apexrest/leadconvert ';
- req.httpMethod = 'POST';
- RestContext.request = req;
- RestContext.response= res;
- String name = Leadconvert.post('Name1','lname','dummy@gmail.com','bizs');
- System.assertEquals('Given email is already used. please provide another email',name);
- }
- }
- Static TestMethod void TestPostMethod2(){
- Lead leed = new Lead(Firstname='Name2',Lastname='Example2',Email='myorg2@gmail.com',Company='My Org2',Status='Open - Not Contacted');
- insert leed;
- database.leadConvert lc = new database.leadConvert();
- lc.setLeadId(leed.id);
- leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
- lc.setConvertedStatus(convertStatus.MasterLabel);
- Database.LeadConvertResult lcr = Database.convertLead(lc);
- Account a = [select id,name,phone from Account where id=:lcr.getAccountId()];
- Contact con = [select id,firstname,Lastname,email from Contact where id=:lcr.getContactId()];
- User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
- ID ProfileID = [Select id from Profile where id = '00e28000000OBjT'].id;
- Id userId;
- System.runAs (thisUser) {
- User u = new User();
- u.email= 'variation1@gmail.com';
- u.contactid = con.Id;
- u.profileid = profileID;
- u.UserName= 'myorg2@gmail.com';
- u.Alias= 'KL';
- u.CommunityNickName= 'perk55';
- u.FirstName = 'Test Name';
- u.LastName = 'KL';
- u.TimeZoneSidKey= 'America/New_York';
- u.LocaleSidKey= 'en_US';
- u.EmailEncodingKey= 'ISO-8859-1';
- u.LanguageLocaleKey= 'en_US';
- insert u;
- userId = u.id;
- }
- RestRequest req = new RestRequest();
- RestResponse res = new RestResponse();
- req.requestURI = '/services/apexrest/leadconvert ';
- req.httpMethod = 'POST';
- RestContext.request = req;
- RestContext.response= res;
- String name = Leadconvert.post('Name2','Example2','dummy2@gmail.com','My Org2');
- System.assertEquals(userId,name); //error is coming here
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement