Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

Object Reference not set to instance of an object

$
0
0

jquery script is:

             

<script>
       $(document).ready(function () {
           $("#LeadForm").submit(function (event) {
               $.ajax({
                   type: "POST",
                   url: "http://localhost:9135/api/CRMLead",
                   data: $("#LeadForm").serialize(),
                   dataType: 'json',
                   success: function (response) {
                       alert(response);
                       $('#LeadForm').each(function () {
                           this.reset(); //Reset each field
                       });
                   },
                   error: function (request, textStatus, errorThrown) {
                       alert(request.responseText + " " +
                           textStatus + " " + errorThrown);
                   }
               });

               event.preventDefault();
        });
       });
    </script>

controller is:

                

 public class CRMLeadController : ApiController
    {
        private OrganizationService _orgService;

        [EnableCors(origins: "*", headers: "*", methods: "post")]
        public string Post([FromBody] FormDataCollection formValues)
        {
            string domain = HttpContext.Current.Request.Headers["Origin"].ToLower();
            string host = HttpContext.Current.Request.Url.Host.ToLower();
            //if (!domain.Contains("yourdomain.com") && !domain.Contains(host))
            //    return "fail!";

            CrmConnection connection = CrmConnection.Parse(
                ConfigurationManager.ConnectionStrings["CRMOL"].ConnectionString);

            using (_orgService = new OrganizationService(connection))
            {
                Entity lead = new Entity("lead");
                lead["firstname"] = formValues.Get("FirstName");
                lead["lastname"] = formValues.Get("LastName");

                _orgService.Create(lead);
            }
            return "success";
        }
    }
}

CRM Connection is:

<add name="CRMOL" connectionString="AuthType=Office365; Username=xxxx@xxxxxx.onmicrosoft.com; Password=xxxx; Url=https://xxxxx.crm.dynamics.com "/>

Thanks in advance


Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>