Exemple #1
0
        public ForceClient GetForceClient()
        {
            if (!SalesforceService.IsUserLoggedIn())
            {
                throw new InvalidOperationException("The user must be logged in to get a ForceClient.");
            }

            // Retrieve the access token and instance URL from the session object, which were set in the SalesforceOAuthRedirectHandler.
            this.AuthenticationClient.AccessToken = HttpContext.Current.Session["AccessToken"] as string;
            this.AuthenticationClient.InstanceUrl = HttpContext.Current.Session["InstanceUrl"] as string;

            return(new ForceClient(
                       this.AuthenticationClient.InstanceUrl,
                       this.AuthenticationClient.AccessToken,
                       this.AuthenticationClient.ApiVersion));
        }
        public async Task<ActionResult> LeadDetail(string id)
        {
            // Ensure the user is authenticated
            if (!SalesforceService.IsUserLoggedIn())
            {
                // Should include a state argument to the URI so that when the dance is done, we can redirect to the page that we were requesting
                string myUri = SalesforceOAuthRedirectHandler.AuthorizationUri.ToString() + "&state=" + this.Url.RequestContext.HttpContext.Request.RawUrl;
                return this.Redirect(myUri);
            }

            // Initalize the Force client
            SalesforceService service = new SalesforceService();
            ForceClient client = service.GetForceClient();

            Lead lead = await client.QueryByIdAsync<Lead>("Lead", id);

            return View(lead);
        }
        //
        // GET: /Lead/

        public async Task<ActionResult> LeadList()
        {
            // Ensure the user is authenticated
            if (!SalesforceService.IsUserLoggedIn())
            {
                // Should include a state argument to the URI so that when the dance is done, we can redirect to the page that we were requesting
                string myUri = SalesforceOAuthRedirectHandler.AuthorizationUri.ToString() + "&state=" + this.Url.RequestContext.HttpContext.Request.RawUrl;
                return this.Redirect(myUri);
            }
            // Initialize the Force client
            SalesforceService service = new SalesforceService();
            ForceClient client = service.GetForceClient();

            // Query just the properties we need to display the selection with SOQL 
            // Querying all properties would waste bandwidth and performance
            QueryResult<Lead> leads =
                await client.QueryAsync<Lead>("SELECT Id, FirstName, LastName,  City, State, Country From Lead");

            return View(leads.records);
        }
        public async Task<ActionResult> LeadEdit(Lead lead)
        {
            // TODO: Resolve Edit failures with ID
            // Ensure the user is authenticated
            if (!SalesforceService.IsUserLoggedIn())
            {
                // Should include a state argument to the URI so that when the dance is done, we can redirect to the page that we were requesting
                string myUri = SalesforceOAuthRedirectHandler.AuthorizationUri.ToString() + "&state=" + this.Url.RequestContext.HttpContext.Request.RawUrl;
                return this.Redirect(myUri);
            }
            // Initialize the Force client
            SalesforceService service = new SalesforceService();
            ForceClient client = service.GetForceClient();

            var success = await client.UpdateAsync("Lead", lead.Id, lead);
            if (success.errors == null)
            {
                return RedirectToAction("LeadList");
            }
            else
            {
                return View(lead);
            }
        }