|
1 | | -# Calling REST API from Lightning Components without Named Credentials |
| 1 | +# Call REST API from Lightning Components without Named Credentials |
2 | 2 |
|
| 3 | +A simple [promise-based](https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_promises.htm) |
| 4 | +[service component](https://developer.salesforce.com/blogs/2018/08/implement-and-use-lightning-service-components.html) |
| 5 | +for working with Salesforce REST API |
| 6 | +directly from your component's JavaScript without you needing to |
| 7 | +write Apex or configure Named Credentials. Just install and use. |
| 8 | + |
| 9 | + |
| 10 | +# Getting Started |
| 11 | + |
| 12 | +1. Deploy this project to your org (you only need what's in `force-app` folder). |
| 13 | + |
| 14 | +2. Explore the `LC_Demo` component in the `force-demo` folder on usage. |
| 15 | + |
| 16 | +3. Try out a demo |
| 17 | + |
| 18 | + a. Assign yourself the **LC Demo** permission set. |
| 19 | + |
| 20 | + b. Navigate to the **LC Demo** tab. |
| 21 | + |
| 22 | + c. Play with the sample components to send different REST API requests. |
| 23 | + |
| 24 | + d. Marvel that you didn't have to write any Apex code or configure a Named Credential :) |
3 | 25 |
|
4 | 26 |
|
5 | 27 | ## Example Usage |
6 | 28 |
|
7 | | -See the `LC_Demo` component in the `force-demo` directory. |
| 29 | +Add the `<c:lc_api>` to your component and give it an `aura:id` for reference. |
| 30 | + |
| 31 | +```xml |
| 32 | +<!-- YourComponent.cmp --> |
| 33 | +<aura:component> |
| 34 | + <c:lc_api aura:id="restClient"/> |
| 35 | + ... |
| 36 | +</aura:component> |
| 37 | +``` |
| 38 | + |
| 39 | +Find the `<c:lc_api>` by its `aura:id` then call the `restRequest(..)` method passing in the `url`, `method`, `body`, and any `headers`. |
| 40 | + |
| 41 | +```js |
| 42 | +// YourComponentController.js |
| 43 | +({ |
| 44 | + createAccount: function( component, event, helper ) { |
| 45 | + |
| 46 | + component.find( 'restClient' ).restRequest({ |
| 47 | + 'url' : '/services/data/v43.0/sobjects/Account', |
| 48 | + 'method' : 'post', |
| 49 | + 'body' : JSON.stringify({ |
| 50 | + "Name" : "LC Demo Account" |
| 51 | + }) |
| 52 | + }).then( $A.getCallback( function( response ) { |
| 53 | + // handle response |
| 54 | + // { id: "001f400000YEZB8AAP", success: true, errors: [] } |
| 55 | + })).catch( $A.getCallback( function( err ) { |
| 56 | + // handle error |
| 57 | + })); |
| 58 | + |
| 59 | + } |
| 60 | +}) |
| 61 | +``` |
| 62 | + |
| 63 | +# Credits |
| 64 | + |
| 65 | +[Doug Ayers](https://douglascayers.com) develops and maintains the project. |
| 66 | + |
| 67 | +[Postmate](https://github.com/dollarshaveclub/postmate) |
| 68 | +for a secure, promise-based library for communicating between windows and iframes. |
| 69 | + |
| 70 | +[jsforce](https://jsforce.github.io/) |
| 71 | +for an elegant, promise-based library for working with Salesforce REST API. |
| 72 | + |
| 73 | + |
| 74 | +# Other Utilities |
| 75 | + |
| 76 | +You should check out [sfdc-lax](https://github.com/ruslan-kurchenko/sfdc-lax) by Ruslan Kurchenko, |
| 77 | +a promise-based service component that makes calling Apex actions or using Lightning Data Service a breeze. |
| 78 | + |
| 79 | + |
| 80 | +# License |
| 81 | + |
| 82 | +The source code is licensed under the [BSD 3-Clause License](LICENSE) |
0 commit comments