0

I want to create a component in an external website that will fetch some data from the user that he enters and store in an salesforce object. Is it possible to do this. I have used the below code but it is not working :

CreateContactRecord.cmp

<aura:component controller="CreateContactRecord" 
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" 
                access="global" >

    <!-- Include Static Resource-->
    <ltng:require styles="/resource/bootstrap/css/bootstrap.min.css" 
                  scripts="/resource/bootstrap/js/jquery.js,/resource/bootstrap/js/bootstrap.min.js"/>

    <!-- Define Attribute-->
    <aura:attribute name="contact" type="Contact" default="{'sobjectType': 'Contact',
                         'FirstName': '',
                         'LastName': '',
                         'Email': '', 
                         'Birthdate': ''
                       }"/>
    <div class="container-fluid">
        <h3>Please Enter The Candidate Information</h3>
        <div class="form-group">
            <label>First Name</label>
            <ui:inputText class="form-control" value="{!v.contact.FirstName}"/>
        </div>
        <div class="form-group">
            <label>Last Name</label>
            <ui:inputText class="form-control" value="{!v.contact.LastName}"/>
        </div>
        <div class="form-group">
            <label>Email Address</label>
            <ui:inputText class="form-control" value="{!v.contact.Email}"/>
        </div>
        <div class="form-group">
            <label>Date Of Birth</label>
            <ui:inputText class="form-control" value="{!v.contact.Birthdate}"/>
        </div>
    </div>
    <div class="col-md-4 text-center">
        <ui:button class="btn btn-default" press="{!c.create}">Create</ui:button>
  </div>
</aura:component>

ManageContact.app

<aura:application access="GLOBAL" extends="ltng:outApp" implements="ltng:allowGuestAccess">
    <aura:dependency resource="CreateContactRecord"/>
</aura:application>

CreateContactRecord.apxc

public with sharing class CreateContactRecord {


    @AuraEnabled
    public static void createRecord (Contact contact){

        try{
            System.debug('CreateContactRecord::createRecord::contact'+contact);

            if(contact != null){
                insert contact;
            }

        } catch (Exception ex){

        }

    }    
}

and this is the code I've written in the website

<div id="lightning"></div>
<script src="https://atosauto-dev-ed.my.salesforce.com/lightning/lightning.out.js"></script>
<script>
     $Lightning.use("c:ManageContact", function() {
        $Lightning.createComponent("c:CreateContactRecord", {
             objectName: "Contact"
            },
        },
       'https://atosauto-dev-ed.my.salesforce.com/',

      );
</script>

I tried doing it in a Visualforce Page

<apex:page showHeader="false" sidebar="false">

 <!-- This loads the JavaScript file needed to use Lightning Components for Visualforce-->
    <apex:includeLightning />

    <!-- div tag act as locator while calling Lightning.createComponent -->
    <div id="lightning" />

    <script>

        $Lightning.use("c:ManageContact", function() {
          $Lightning.createComponent("c:CreateContactRecord",
          "",
          "lightning",
          function(cmp) {
            // do some stuff
          });
        });

    </script>
</apex:page>

and for this it is showing the below error :

An internal server error has occurred Error ID: 1851586357-376265 (989141424)

  • Can you provide more details on what is not working? Does it display an error? Does if not push data to the object? Does it make your computer explode? – Lukas Lunow Oct 23 '19 at 10:30
  • Actually it is not displaying anything. It's just blank – Ankur Nigam Oct 23 '19 at 10:54
  • @LukasLunow when I used Visualforce Page it gives the below error An internal server error has occurred Error ID: 1851586357-376265 (989141424) – Ankur Nigam Oct 23 '19 at 16:46
  • Any logs in the browser console? – David Reed Oct 23 '19 at 17:01
  • Access to XMLHttpRequest at 'atosauto-dev-ed.my.salesforce.com/c/…' from origin 'lightningknow-developer-edition.ap5.force.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. But I have whitelisted this in CORS settings – Ankur Nigam Oct 23 '19 at 17:08
0

Make the below change in your app for <aura:dependency/> tag

<aura:application access="GLOBAL" extends="ltng:outApp" implements="ltng:allowGuestAccess">
       <aura:dependency resource="markup://c:CreateContactRecord"/>
</aura:application>

In your visual force page make the below changes for the parameters instead of "" change it to {}

    $Lightning.use("c:ManageContact", function() {
      $Lightning.createComponent("c:CreateContactRecord",
      {},
      "lightning",
      function(cmp) {
        // do some stuff
      });
    });

</script>

try above changes and let me know!!

| improve this answer | |
  • I recieved this error: An internal server error has occurred Error ID: 1851586357-378069 (989141424) – Ankur Nigam Oct 23 '19 at 17:33
  • Does the site guest user has access to contact and account object ? – Sudarshan Oct 23 '19 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.