Single Sign On (SSO) using Okta

Single sign-on (SSO) lets users access authorized network resources with a single login. You validate usernames and passwords against your corporate user database or other client app rather than Salesforce managing separate passwords for each resource.

Single sign-on (SSO) lets users access authorized network resources with a single login. You validate usernames and passwords against your corporate user database or other client app rather than Salesforce managing separate passwords for each resource. 

  • Session and User Authentication Service. 
  • Allows user to access multiple applications using single?set of login credentials. 
  • Secure and easy way to manage several accounts without compromising the integrities of the individual applications. 

Advantages of SSO: 

  • Reduced administrative costs 
  • Leverage existing investment 
  • Time savings 
  • Increased user adoption
  • Increased security 

SAML 

  • SAML (Security Assertion Mark-up Language) is an XML-based standard for exchanging authentication and authorization data between an?identity provider?(IdP) such as Okta, and a?service provider?(SP) such as Box, Salesforce, G Suite, Workday, etc. 

What is Okta? 

  • Okta?connects any person with any application on any device. 
  • Okta enables you to provide Single Sign On (SSO) access to cloud, on-premises, and mobile applications. 
  • You sign into Okta and you can then launch any of your web apps without having to re-enter your credentials. 

Why Okta? 

Okta provides a central portal of applications which lets users to access the applications in an easy way 

  • Provides secure integration 
  • Multitenant solution 
  • Time constraint because of its integrated cloud platform 

Advantages of Okta: 

  • Most reliable and customizable tool for SSO 
  • Can be used for all web and mobile apps
  • Cost effective 
  • More efficient 
  • Secure employee and customer experiences 

Using Okta SSO with Salesforce: 

The most important use case for SSO in salesforce is to overcome the license limitations for users. For instance, consider a situation wherein ‘n’ number of users need access for the org, but the org consists only limited number of licenses then we cannot give access to all the users. In this case, with the help of SSO and OKTA we can just provide the okta credential to the user and once they login, user will be created in salesforce and after the user logs out, they will be deactivated. By this way we can create n number of users in salesforce irrespective of the license limitations 

Prerequisites for integrating Okta with Salesforce: 

1.Need to create Okta account using the below link, 

https://www.okta.com/free-trial/# 

2.Need to install Okta Verify app either in Apple or Android or Windows platform. 

3.Sign into Okta account using the credentials received to the email id given while sign up. 

4.Click Your Org under the User 

           

 5.Click Admin tab in the right corner of the page to create custom apps.

6.This will redirect to Okta verify page.

7.Click Setup and Select your device type in which Okta verify app is installed. Click Next and Enter the Verification code received in the Okta Verify app. 

 

OKTA Setup[Need OKTA Admin access to create Custom App]  

Step 1:  Application ?Add applications ? Create New App 

                                            

 Step 2:  General Settings 

App Name: Enter the desired name 

                   

 Step 3:  SAML Settings 

  • Single sign on URL: https://community.cs63.force.com/samplecommunity/login [Need to replace this with the Salesforce Production Community login URL] 
  • Audience URL (SP Entity ID): https://saml.salesforce.com 

      

 Step 4:  Feedback        

     

Step 5:  Get URL details and download Certificate   

  • Click Application and search for created App and click the App name to view the detailed information. 
  • Click [Sign On] Tab and click the button [View Setup Instruction] 
  • Copy the Identity Provider Single Sign-On URL and Identity Provider Issuer URL [Provide to SF Team] 
  • Download the X.509 certificate [Provide to SF Team]. 

Salesforce: SAML Single Sign-On Setup 

 The following steps are written for Lightning Experience. 

 Step 1Setup -> Identity -> Single Sign-On Settings (Enable SAML, if not enabled already). 

 Step 2Setup -> Identity -> Single Sign-On Settings -> SAML Single Sign-On Settings -> New 

             

  • Name: Demo 
  • API Name: Demo 
  • Issuer: http://www.okta.com/exknxeot1st95AblV0h7 [OKTA team will provide the URL as shown in step 6] 
  • Entity ID: https://saml.salesforce.com 
  • Identity Provider Certificate: [Upload the certificate given by OKTA team. To download certificate follow Step 5] 
  • Request Signature Method: RSA-SHA256 
  • Assertion Decryption Certificate: Assertion not encrypted 
  • SAML Identity Type: Federation ID 
  • SAML Identity Location: Subject 
  • Service Provider Initiated Request Binding: HTTP Redirect 
  • Identity Provider Login [OKTA team will provide the URL. To get this URL follow step 5] 
  • Custom Logout URL: https://samplecommunity.okta.com/ 
  • User Provisioning Enabled: Enable 
  • User Provisioning Type: Custom SAML JIT with Apex handler 
  • SAML JIT Handler: JITHandler 
  • Execute Handler As: System Admin [Current User] 

Step 4: 

  • Click Setup -> All Communities -> Select Your Community -> Workspace -> Administration 
  •  Login & registration tab ->Login page Setup -> In sign in option section, Select the Custom App which is created in Okta as the Single Sign on provider. 

            

 Validate: 

  • Copy Community login URL 
  • Sign out of Salesforce and OKTA 
  • Paste the URL in the address bar 
  • The URL will redirect to the OKTA login page 
  • After successful log in to OKTA, the user will be landed in the Salesforce Community. 

 For Community (License-Salesforce Platform) User, Account and Contact Creation SAML JIT Handler: 

  1. global class JITHandler implements Auth.SamlJitHandler {
  2. //JIT Handler Exception
  3. private class JitException extends Exception{}
  4. //Method to insert or update a user record
  5. @testVisible
  6. private void handleUser(boolean create, User user, Map<String, String> attributes,
  7. String federationIdentifier, boolean isStandard) {
  8. if(create && attributes.containsKey(‘User.Username’)) {
  9. user.Username = attributes.get(‘User.Username’);
  10. }
  11. if(create) {
  12. if(attributes.containsKey(‘User.FederationIdentifier’)) {
  13. user.FederationIdentifier = attributes.get(‘User.FederationIdentifier’);
  14. } else {
  15. user.FederationIdentifier = federationIdentifier;
  16. }
  17. }
  18. if(attributes.containsKey(‘User.Phone’)) {
  19. user.Phone = attributes.get(‘User.Phone’);
  20. }
  21. if(attributes.containsKey(‘User.Email’)) {
  22. user.Email = attributes.get(‘User.Email’);
  23. }
  24. if(attributes.containsKey(‘User.FirstName’)) {
  25. user.FirstName = attributes.get(‘User.FirstName’);
  26.   }
  27. if(attributes.containsKey(‘User.LastName’)) {
  28. user.LastName = attributes.get(‘User.LastName’);
  29. }
  30. if(attributes.containsKey(‘User.Title’)) {
  31. user.Title = attributes.get(‘User.Title’);
  32. }
  33. if(attributes.containsKey(‘User.CompanyName’)) {
  34. user.CompanyName = attributes.get(‘User.CompanyName’);
  35. }
  36. if(attributes.containsKey(‘User.AboutMe’)) {
  37. user.AboutMe = attributes.get(‘User.AboutMe’);
  38. }
  39. if(attributes.containsKey(‘User.Street’)) {
  40. user.Street = attributes.get(‘User.Street’);
  41. }
  42. if(attributes.containsKey(‘User.State’)) {
  43. user.State = attributes.get(‘User.State’);
  44. }
  45. if(attributes.containsKey(‘User.City’)) {
  46. user.City = attributes.get(‘User.City’);
  47. }
  48. if(attributes.containsKey(‘User.Zip’)) {
  49.   user.PostalCode = attributes.get(‘User.Zip’);
  50. }
  51. if(attributes.containsKey(‘User.Country’)) {
  52. user.Country = attributes.get(‘User.Country’);
  53. }
  54. if(attributes.containsKey(‘User.CallCenter’)) {
  55. user.CallCenterId = attributes.get(‘User.CallCenter’);
  56. }
  57. if(attributes.containsKey(‘User.Manager’)) {
  58. user.ManagerId = attributes.get(‘User.Manager’);
  59. }
  60. if(attributes.containsKey(‘User.MobilePhone’)) {
  61. user.MobilePhone = attributes.get(‘User.MobilePhone’);
  62. }
  63. if(attributes.containsKey(‘User.DelegatedApproverId’)) {
  64. user.DelegatedApproverId = attributes.get(‘User.DelegatedApproverId’);
  65. }
  66. if(attributes.containsKey(‘User.Department’)) {
  67. user.Department = attributes.get(‘User.Department’);
  68. }
  69. if(attributes.containsKey(‘User.Division’)) {
  70. user.Division = attributes.get(‘User.Division’);
  71. }
  72. if(attributes.containsKey(‘User.EmployeeNumber’)) {
  73. user.EmployeeNumber = attributes.get(‘User.EmployeeNumber’);
  74. }
  75. if(attributes.containsKey(‘User.Extension’)) {
  76. user.Extension = attributes.get(‘User.Extension’);
  77. }
  78. if(attributes.containsKey(‘User.Fax’)) {
  79. user.Fax = attributes.get(‘User.Fax’);
  80. }
  81. if(attributes.containsKey(‘User.CommunityNickname’)) {
  82. user.CommunityNickname = attributes.get(‘User.CommunityNickname’);
  83. }
  84. if(attributes.containsKey(‘User.ReceivesAdminInfoEmails’)) {
  85. String ReceivesAdminInfoEmailsVal = attributes.get(‘User.ReceivesAdminInfoEmails’);
  86. user.ReceivesAdminInfoEmails = ‘1’.equals(ReceivesAdminInfoEmailsVal) ||                            Boolean.valueOf(ReceivesAdminInfoEmailsVal);
  87. }
  88. if(attributes.containsKey(‘User.ReceivesInfoEmails’)) {
  89. String ReceivesInfoEmailsVal = attributes.get(‘User.ReceivesInfoEmails’);
  90. user.ReceivesInfoEmails = ‘1’.equals(ReceivesInfoEmailsVal) ||                                  Boolean.valueOf(ReceivesInfoEmailsVal);
  91. }
  92. List<Single_Sign_on__mdt>  singleSignProfileSettings = [SELECT id,Profile_Name__c,
  93. LocaleSidKey__c,TimeZoneSidKey__c,EmailEncodingKey__c,
  94. LanguageLocaleKey__c FROM Single_Sign_on__mdt
  95. WHERE Profile_Name__c = ‘Profile  Name’];
  96. List<Profile> profileIds = [SELECT Id, Name FROM Profile
  97. WHERE Name =: singleSignProfileSettings[0].Profile_Name__c];
  98. if(attributes.containsKey(‘User.LocaleSidKey’)) {
  99. user.LocaleSidKey = attributes.get(‘User.LocaleSidKey’);
  100. } else if(create) {
  101. user.LocaleSidKey = singleSignProfileSettings[0].LocaleSidKey__c;
  102. }
  103. if(attributes.containsKey(‘User.LanguageLocaleKey’)) {
  104. user.LanguageLocaleKey = attributes.get(‘User.LanguageLocaleKey’);
  105. } else if(create) {
  106. user.LanguageLocaleKey = singleSignProfileSettings[0].LanguageLocaleKey__c;
  107. }
  108. if( attributes.containsKey(‘User.Alias’) && attributes.get(‘User.Alias’) != ”
  109. && attributes.get(‘User.Alias’) != null ) {
  110. user.Alias = attributes.get(‘User.Alias’);
  111. } else if(create) {
  112. String alias = ”;
  113. if(user.FirstName == null) {
  114. alias = user.LastName;
  115. } else {
  116. alias = user.FirstName.charAt(0) + user.LastName;
  117. }
  118. if(alias.length() > 5) {
  119. alias = alias.substring(0, 5);
  120. }
  121. user.Alias = alias;
  122. }
  123. if(attributes.containsKey(‘User.TimeZoneSidKey’)) {
  124. user.TimeZoneSidKey = attributes.get(‘User.TimeZoneSidKey’);
  125. } else if(create) {
  126. user.TimeZoneSidKey = singleSignProfileSettings[0].TimeZoneSidKey__c;
  127. }
  128. if(attributes.containsKey(‘User.EmailEncodingKey’)) {
  129. user.EmailEncodingKey = attributes.get(‘User.EmailEncodingKey’);
  130. } else if(create) {
  131. user.EmailEncodingKey = singleSignProfileSettings[0].EmailEncodingKey__c;
  132. }
  133. /*
  134. * If you are updating Contact or Account object fields, you cannot update the following User fields at the same time.
  135. * If your identity provider sends these User fields as attributes along with Contact
  136. * or Account fields, you must modify the logic in this class to update either these
  137. * User fields or the Contact and Account fields. */
  138. if(attributes.containsKey(‘User.IsActive’)) {
  139.   String IsActiveVal = attributes.get(‘User.IsActive’);
  140. user.IsActive = ‘1’.equals(IsActiveVal) || Boolean.valueOf(IsActiveVal);
  141. }else if(create){
  142. user.IsActive = true;
  143. }else if (!create){
  144.      List<User> userList = [SELECT isActive,username,Profile.Name,FederationIdentifier
  145. FROM User
  146. WHERE (FederationIdentifier = :federationIdentifier OR
  147.            username =:federationIdentifier) AND isActive = false ];
  148. if(userList.size() > 0){
  149. user.ProfileId = profileIds[0].Id;
  150. user.IsActive = true;
  151. }
  152. }
  153. if(attributes.containsKey(‘User.ProfileId’)) {
  154. String userProfileId = attributes.get(‘User.ProfileId’);
  155. Profile profileId = [SELECT Id FROM Profile WHERE Id =: userProfileId];
  156. user.ProfileId = profileId.Id;
  157. } else if(create){
  158. user.ProfileId = profileIds[0].Id;
  159. }
  160. if(attributes.containsKey(‘User.UserRoleId’)) {
  161. String userRole = attributes.get(‘User.UserRoleId’);
  162. UserRole role = [SELECT Id FROM UserRole WHERE Id=:userRole];
  163. user.UserRoleId = role.Id;
  164. }
  165. if(create)
  166. {
  167. insert user;
  168. }else {
  169. update user;
  170. }
  171. }
  172. //Method to insert or update a Contact record
  173. @TestVisible
  174. private void handleContact(boolean create, String accountId, User user,
  175. Map<String, String> attributes) {
  176. Contact contactIns;
  177. boolean newContact = false;
  178. if(create) {
  179. if(attributes.containsKey(‘User.Contact’)) {
  180. String contact = attributes.get(‘User.Contact’);
  181. contactIns = [SELECT Id, AccountId FROM Contact WHERE Id=:contact];
  182. user.ContactId = contact;
  183. } else {
  184. contactIns = new Contact();
  185. newContact = true;
  186. }
  187. }
  188. else{
  189. List<Contact> contactList = [SELECT Id,User__c,Email,AccountId FROM Contact
  190. WHERE User__c=:user.Id ];
  191. if(contactList.size() > 0)
  192. contactIns = contactList[0];
  193. }
  194. List<User> userInfo = [SELECT id,FirstName,LastName,Email FROM User WHERE Id =: user.Id];
  195. if(attributes.containsKey(‘Contact.Email’)) {
  196. contactIns.Email = attributes.get(‘Contact.Email’);
  197. }else{
  198. //if contact Email id not in map attribute
  199. if(userInfo.size() > 0)
  200. contactIns.Email = userInfo[0].Email;
  201. }
  202. if(attributes.containsKey(‘Contact.FirstName’)) {
  203. contactIns.FirstName = attributes.get(‘Contact.FirstName’);
  204. }else{
  205. //if contact FirstName id not in map attribute
  206. if(userInfo.size() > 0)
  207. contactIns.FirstName = userInfo[0].FirstName;
  208. }
  209. if(attributes.containsKey(‘Contact.LastName’)) {
  210. contactIns.LastName = attributes.get(‘Contact.LastName’);
  211. }else{
  212. //if contact LastName id not in map attribute
  213. if(userInfo.size() > 0)
  214. contactIns.LastName =userInfo[0].LastName;
  215. }
  216. if(attributes.containsKey(‘Contact.Phone’)) {
  217. contactIns.Phone = attributes.get(‘Contact.Phone’);
  218. }
  219. if(attributes.containsKey(‘Contact.MailingStreet’)) {
  220. contactIns.MailingStreet = attributes.get(‘Contact.MailingStreet’);
  221. }
  222. if(attributes.containsKey(‘Contact.MailingCity’)) {
  223. contactIns.MailingCity = attributes.get(‘Contact.MailingCity’);
  224. }
  225. if(attributes.containsKey(‘Contact.MailingState’)) {
  226. contactIns.MailingState = attributes.get(‘Contact.MailingState’);
  227. }
  228. if(attributes.containsKey(‘Contact.MailingCountry’)) {
  229. contactIns.MailingCountry = attributes.get(‘Contact.MailingCountry’);
  230. }
  231. if(attributes.containsKey(‘Contact.MailingPostalCode’)) {
  232. contactIns.MailingPostalCode = attributes.get(‘Contact.MailingPostalCode’);
  233. }
  234. if(attributes.containsKey(‘Contact.OtherStreet’)) {
  235. contactIns.OtherStreet = attributes.get(‘Contact.OtherStreet’);
  236. }
  237. if(attributes.containsKey(‘Contact.OtherCity’)) {
  238. contactIns.OtherCity = attributes.get(‘Contact.OtherCity’);
  239. }
  240. if(attributes.containsKey(‘Contact.OtherState’)) {
  241. contactIns.OtherState = attributes.get(‘Contact.OtherState’);
  242. }
  243. if(attributes.containsKey(‘Contact.OtherCountry’)) {
  244. contactIns.OtherCountry = attributes.get(‘Contact.OtherCountry’);
  245. }
  246. if(attributes.containsKey(‘Contact.OtherPostalCode’)) {
  247. contactIns.OtherPostalCode = attributes.get(‘Contact.OtherPostalCode’);
  248. }
  249. if(attributes.containsKey(‘Contact.AssistantPhone’)) {
  250. contactIns.AssistantPhone = attributes.get(‘Contact.AssistantPhone’);
  251. }
  252. if(attributes.containsKey(‘Contact.Department’)) {
  253. contactIns.Department = attributes.get(‘Contact.Department’);
  254. }
  255. if(attributes.containsKey(‘Contact.Description’)) {
  256. contactIns.Description = attributes.get(‘Contact.Description’);
  257. }
  258. if(attributes.containsKey(‘Contact.Fax’)) {
  259. contactIns.Fax = attributes.get(‘Contact.Fax’);
  260. }
  261. if(attributes.containsKey(‘Contact.HomePhone’)) {
  262. contactIns.HomePhone = attributes.get(‘Contact.HomePhone’);
  263. }
  264. if(attributes.containsKey(‘Contact.MobilePhone’)) {
  265. contactIns.MobilePhone = attributes.get(‘Contact.MobilePhone’);
  266. }
  267. if(attributes.containsKey(‘Contact.OtherPhone’)) {
  268. contactIns.OtherPhone = attributes.get(‘Contact.OtherPhone’);
  269. }
  270.   if(attributes.containsKey(‘Contact.Title’)) {
  271. contactIns.Title = attributes.get(‘Contact.Title’);
  272. }
  273. if(attributes.containsKey(‘Contact.Salutation’)) {
  274. contactIns.Salutation = attributes.get(‘Contact.Salutation’);
  275. }
  276. if(attributes.containsKey(‘Contact.LeadSource’)) {
  277. contactIns.LeadSource = attributes.get(‘Contact.LeadSource’);
  278. }
  279. if(attributes.containsKey(‘Contact.DoNotCall’)) {
  280.       String DoNotCallVal = attributes.get(‘Contact.DoNotCall’);
  281. contactIns.DoNotCall = ‘1’.equals(DoNotCallVal) || Boolean.valueOf(DoNotCallVal);
  282. }
  283. if(attributes.containsKey(‘Contact.HasOptedOutOfEmail’)) {
  284. String HasOptedOutOfEmailVal = attributes.get(‘Contact.HasOptedOutOfEmail’);
  285. contactIns.HasOptedOutOfEmail = ‘1’.equals(HasOptedOutOfEmailVal) ||                                Boolean.valueOf(HasOptedOutOfEmailVal);
  286. }
  287. if(attributes.containsKey(‘Contact.HasOptedOutOfFax’)) {
  288. String HasOptedOutOfFaxVal = attributes.get(‘Contact.HasOptedOutOfFax’);
  289. contactIns.HasOptedOutOfFax = ‘1’.equals(HasOptedOutOfFaxVal) ||                                    Boolean.valueOf(HasOptedOutOfFaxVal);
  290. }
  291. if(attributes.containsKey(‘Contact.Owner’)) {
  292. contactIns.OwnerId = attributes.get(‘Contact.Owner’);
  293. }
  294. if(attributes.containsKey(‘Contact.AssistantName’)) {
  295. contactIns.AssistantName = attributes.get(‘Contact.AssistantName’);
  296. }
  297. if(attributes.containsKey(‘Contact.Birthdate’)) {
  298. contactIns.Birthdate = Date.valueOf(attributes.get(‘Contact.Birthdate’));
  299. }
  300. contactIns.AccountId = accountId;
  301. contactIns.User__c = user.Id;
  302. if(newContact) {
  303. Database.DMLOptions dml = new Database.DMLOptions();
  304. dml.DuplicateRuleHeader.allowSave = true;
  305. dml.DuplicateRuleHeader.runAsCurrentUser = true;
  306. Database.SaveResult sr = Database.insert(contactIns, dml);
  307. } else {
  308. update(contactIns);
  309. }
  310. }
  311. //Method to insert or update a Account record
  312. @testVisible
  313. private String handleAccount(boolean create, User user, Map<String, String> attributes) {
  314. Account accIns;
  315. boolean newAccount = false;
  316. if(create) {
  317. if(attributes.containsKey(‘User.Account’)) {
  318. String account = attributes.get(‘User.Account’);
  319. accIns = [SELECT Id FROM Account WHERE Id=:account];
  320. } else {
  321. if(attributes.containsKey(‘User.Contact’)) {
  322. String contact = attributes.get(‘User.Contact’);
  323. Contact con = [SELECT Id, AccountId FROM Contact WHERE Id =: contact];
  324. String account = con.AccountId;
  325. accIns = [SELECT Id FROM Account WHERE Id=:account];
  326. } else {
  327. accIns  = new Account();
  328. newAccount = true;
  329. }
  330. }
  331. } else {
  332. if(attributes.containsKey(‘User.Account’)) {
  333. String account = attributes.get(‘User.Account’);
  334. accIns = [SELECT Id FROM Account WHERE Id=:account];
  335. }
  336. else {
  337. if(attributes.containsKey(‘User.Contact’)) {
  338. String contact = attributes.get(‘User.Contact’);
  339. Contact con = [SELECT Id, AccountId FROM Contact WHERE Id=:contact];
  340. String account = con.AccountId;
  341. accIns = [SELECT Id FROM Account WHERE Id=:account];
  342. } else{
  343. List<Contact> con = [SELECT Id,User__c,Email, AccountId FROM Contact
  344. WHERE  User__c =: user.Id];
  345. If(con.Size() > 0){
  346.      String account = con[0].AccountId;
  347. accIns = [SELECT Id,Name FROM Account WHERE Id=:account];
  348. }
  349. }
  350. }
  351. }
  352. List<User> userInfo = [SELECT id,FirstName,LastName,Email FROM User WHERE Id =: user.Id];
  353. if(attributes.containsKey(‘Account.Name’)) {
  354. accIns.Name = attributes.get(‘Account.Name’);
  355. }else{
  356. if(userInfo.size() > 0)
  357. {
  358. if(userInfo[0].FirstName != null)
  359. {
  360. accIns.Name = userInfo[0].FirstName +’ ‘+ userInfo[0].LastName;
  361. }
  362. }
  363.     else
  364. accIns.Name = user.Email;
  365. }
  366. if(attributes.containsKey(‘Account.AccountNumber’)) {
  367. accIns.AccountNumber = attributes.get(‘Account.AccountNumber’);
  368. }
  369. if(attributes.containsKey(‘Account.Owner’)) {
  370. accIns.OwnerId = attributes.get(‘Account.Owner’);
  371. }
  372. if(attributes.containsKey(‘Account.BillingStreet’)) {
  373. accIns.BillingStreet = attributes.get(‘Account.BillingStreet’);
  374. }
  375. if(attributes.containsKey(‘Account.BillingCity’)) {
  376. accIns.BillingCity = attributes.get(‘Account.BillingCity’);
  377. }
  378. if(attributes.containsKey(‘Account.BillingState’)) {
  379. accIns.BillingState = attributes.get(‘Account.BillingState’);
  380. }
  381. if(attributes.containsKey(‘Account.BillingCountry’)) {
  382. accIns.BillingCountry = attributes.get(‘Account.BillingCountry’);
  383. }
  384. if(attributes.containsKey(‘Account.BillingPostalCode’)) {
  385. accIns.BillingPostalCode = attributes.get(‘Account.BillingPostalCode’);
  386. }
  387. if(attributes.containsKey(‘Account.AnnualRevenue’)) {
  388. accIns.AnnualRevenue = Integer.valueOf(attributes.get(‘Account.AnnualRevenue’));
  389. }
  390. if(attributes.containsKey(‘Account.Description’)) {
  391. accIns.Description = attributes.get(‘Account.Description’);
  392. }
  393. if(attributes.containsKey(‘Account.Fax’)) {
  394. accIns.Fax = attributes.get(‘Account.Fax’);
  395. }
  396. if(attributes.containsKey(‘Account.NumberOfEmployees’)) {
  397. accIns.NumberOfEmployees = Integer.valueOf(attributes.get(‘Account.NumberOfEmployees’));
  398. }
  399. if(attributes.containsKey(‘Account.Phone’)) {
  400. accIns.Phone = attributes.get(‘Account.Phone’);
  401. }
  402. if(attributes.containsKey(‘Account.ShippingStreet’)) {
  403. accIns.ShippingStreet = attributes.get(‘Account.ShippingStreet’);
  404. }
  405. if(attributes.containsKey(‘Account.ShippingCity’)) {
  406. accIns.ShippingCity = attributes.get(‘Account.ShippingCity’);
  407. }
  408. if(attributes.containsKey(‘Account.ShippingState’)) {
  409. accIns.ShippingState = attributes.get(‘Account.ShippingState’);
  410. }
  411. if(attributes.containsKey(‘Account.ShippingCountry’)) {
  412. accIns.ShippingCountry = attributes.get(‘Account.ShippingCountry’);
  413. }
  414. if(attributes.containsKey(‘Account.ShippingPostalCode’)) {
  415. accIns.ShippingPostalCode = attributes.get(‘Account.ShippingPostalCode’);
  416. }
  417. if(attributes.containsKey(‘Account.Sic’)) {
  418. accIns.Sic = attributes.get(‘Account.Sic’);
  419. }
  420. if(attributes.containsKey(‘Account.TickerSymbol’)) {
  421. accIns.TickerSymbol = attributes.get(‘Account.TickerSymbol’);
  422. }
  423. if(attributes.containsKey(‘Account.Website’)) {
  424. accIns.Website = attributes.get(‘Account.Website’);
  425. }
  426. if(attributes.containsKey(‘Account.Industry’)) {
  427. accIns.Industry = attributes.get(‘Account.Industry’);
  428. }
  429. if(attributes.containsKey(‘Account.Ownership’)) {
  430. accIns.Ownership = attributes.get(‘Account.Ownership’);
  431. }
  432. if(attributes.containsKey(‘Account.Rating’)) {
  433. accIns.Rating = attributes.get(‘Account.Rating’);
  434. }
  435. if(newAccount) {
  436. Database.DMLOptions dml = new Database.DMLOptions();
  437. dml.DuplicateRuleHeader.allowSave = true;
  438. dml.DuplicateRuleHeader.runAsCurrentUser = true;
  439. Database.SaveResult sr = Database.insert(accIns, dml);
  440. } else {
  441. update(accIns);
  442. }
  443. return accIns.Id;
  444. }
  445. //This method is called if the user has logged in before with SAML single sign-on and then logs in again
  446. private void handleJit(boolean create, User user, Id samlSsoProviderId, Id communityId,
  447. Id portalId, String federationIdentifier, Map<String, String> attributes, String assertion) {
  448. if(communityId != null || portalId != null) {
  449. handleUser(create, user, attributes, federationIdentifier, false);
  450.      String account = handleAccount(create, user, attributes);
  451. handleContact(create, account, user, attributes);
  452. } else {
  453. handleUser(create, user, attributes, federationIdentifier, true);
  454. }
  455. }
  456. /*Returns a User object using the specified Federation ID.
  457. * The User object corresponds to the user information and may be a new user that hasn’t t been inserted in the database
  458. * or may represent an existingser record in the database. */
  459. global User createUser(Id samlSsoProviderId, Id communityId, Id portalId,
  460. String federationIdentifier,Map<String, String> attributes, String assertion) {
  461. User userIns = new User();
  462. handleJit(true, userIns, samlSsoProviderId, communityId, portalId, federationIdentifier,
  463. attributes, assertion);
  464. return userIns;
  465. }
  466. /*Updates the specified user’s information.
  467. * This method is called if the user has logged in before with SAML single sign-on and then logs in again*/
  468. global void updateUser(Id userId, Id samlSsoProviderId, Id communityId, Id portalId,
  469. String federationIdentifier,
  470. Map<String, String> attributes, String assertion) {
  471. User user = [SELECT Id, FirstName, ContactId FROM User WHERE Id =:userId];
  472. handleJit(false, user, samlSsoProviderId, communityId, portalId,federationIdentifier,
  473. attributes, assertion);
  474. }
  475. }

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.