public HttpResponseMessage Register(SalesforceUpdateRequest model)
 {
     if (!ModelState.IsValid)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
     }
     try
     {
         SalesforceProxyService svc  = new SalesforceProxyService();
         ItemResponse <string>  resp = new ItemResponse <string>();
         //Ques the job in hangfire so the home page goes to loading page faster - needs hangfire switch TRUE in web.config
         BackgroundJob.Enqueue(() => svc.Register(model));
         return(Request.CreateResponse(HttpStatusCode.OK, new SuccessResponse()));
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
Ejemplo n.º 2
0
 public void Register(SalesforceUpdateRequest model)
 {
     _SalesforceService.Register(model);
 }
        public string Register(SalesforceUpdateRequest model)
        {
            //LOGIN TO SALESFORCE
            //Prevent using TLS 1.0 which is outdated
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            string        userName           = _configService.getConfigValusAsString("SalesforceUserName");
            string        password           = _configService.getConfigValusAsString("SalesforcePassword");
            SforceService SfdcBinding        = null;
            LoginResult   CurrentLoginResult = null;

            SfdcBinding = new SforceService();
            try
            {
                CurrentLoginResult = SfdcBinding.login(userName, password);
            }
            catch (System.Web.Services.Protocols.SoapException e)
            {  // This is likley to be caused by bad username or password
                SfdcBinding = null;
                throw (e);
            }
            catch (Exception e)
            {  // This is something else, probably comminication
                SfdcBinding = null;
                throw (e);
            }
            //Change the binding to the new endpoint
            SfdcBinding.Url = CurrentLoginResult.serverUrl;
            //Create a new session header object and set the session id to that returned by the login
            SfdcBinding.SessionHeaderValue           = new SessionHeader();
            SfdcBinding.SessionHeaderValue.sessionId = CurrentLoginResult.sessionId;

            //UPDATE -- to (spunkydrewster002's) salesforce Leads
            QueryResult queryResult   = null;
            string      emailToUpdate = model.Email;
            String      SOQL          = "select Id from Lead where Email = '" + emailToUpdate + "'";

            queryResult = SfdcBinding.query(SOQL);

            //If email exists, update with first/last name
            if (queryResult.size > 0)
            {
                //UPDATE -- to (spunkydrewster002's) salesforce Leads
                Lead   lead       = (Lead)queryResult.records[0];
                string Id         = lead.Id;
                Lead   updateLead = new Lead();
                updateLead.Id        = Id;
                updateLead.Email     = model.Email;
                updateLead.FirstName = model.FirstName;
                updateLead.LastName  = model.LastName;
                SaveResult[] saveResults = SfdcBinding.update(new sObject[] { updateLead });
                string       result      = "";
                if (saveResults[0].success)
                {
                    result = "The update of Lead ID " + saveResults[0].id + " was succesful";
                    return(result);
                }
                else
                {
                    result = "There was an error updating the Lead. The error returned was " + saveResults[0].errors[0].message;
                    return(result);
                }
            }
            //If email doesn't exist, insert email with first/last name
            else
            {
                //POST - to (spunkydrewster002's) salesforce Leads
                Lead sfdcLead = new Lead();
                sfdcLead.Email = model.Email;
                //Fields required for lead but wont be submitted on homepage, so placeholders are used.
                sfdcLead.FirstName = model.FirstName;
                sfdcLead.LastName  = model.LastName;
                string companyName = "Grader Registered User";
                sfdcLead.Company = companyName;
                SaveResult[] saveResults = SfdcBinding.create(new sObject[] { sfdcLead });
                if (saveResults[0].success)
                {
                    string resultId = "";
                    resultId = saveResults[0].id;
                    return(resultId);
                }
                else
                {
                    string result = "";
                    result = saveResults[0].errors[0].message;
                    return(result);
                }
            }
        }