private void convertLeadSample() { //Verify that we are already authenticated, if not //call the login function to do so if (!loggedIn) { if (!login()) return; } //This is the structure we need to fill in to effect a conversion apex.LeadConvert lc = new apex.LeadConvert(); string summary = "Converting lead to contact.\n"; //When converting a lead you have the option of creating a new contact, or //merging the lead with an existing contact. This sample will show both //cases. So the first thing to do, is get the lead to convert. apex.sObject lead = this.getUnconvertedLead(); if (lead == null) { Console.WriteLine("No lead was selected, conversion failed."); return; } //Set the lead id on the structure. lc.leadId = lead.Id.Substring(0, 15); //Now we need to determine if the user wants to merge the lead to an existing contact, //or have a new contact created. Console.Write("/nDo you want to merge this lead into an existing contact (Y/N)? "); string merge = Console.ReadLine(); apex.sObject contact = null; if (merge != null && merge.ToLower().Equals("y")) { contact = getContact(); if (contact == null) { summary += " Create new contact.\n"; } else { summary += " Merge with " + getFieldValue("FirstName", contact.Any) + " " + getFieldValue("LastName", contact.Any) + "\n"; lc.contactId = contact.Id.Substring(0, 15); //We have the option of resetting the contact status to that of the lead, //so we will query the user for that information. Console.Write("Do you want to overwrite the contact status with the lead status (Y/N)? "); string overWrite = Console.ReadLine(); if (overWrite != null && overWrite.ToLower().Equals("y")) lc.overwriteLeadSource = true; else lc.overwriteLeadSource = false; } } else summary += " Create new contact.\n"; //if a contact is chosen, we will use the accountid of the contact, this must //be specified. If the contact is not chosen, then the user needs to select //an account to place the new contact in. string accountId = null; if (contact != null) { accountId = getFieldValue("AccountId", contact.Any); lc.accountId = accountId.Substring(0, 15); } else //get an account chosen by the user { Console.Write("Do you want to create a new account for this lead (Y/N)? "); string newAccount = Console.ReadLine(); if (newAccount == null || !newAccount.ToLower().Equals("y")) accountId = this.getAccount().Id.Substring(0, 15); if (accountId != null) { lc.accountId = accountId; string accountName = getAccountName(accountId); if (accountName != null) summary += " New contact will be in account '" + accountName + "'.\n"; else summary += " New contact will be in account with an ID of '" + accountId + "'.\n"; } else { summary += " A new account will be created."; } } //We will now ask the user if they would like to create a new opportunity //based on the information on this lead Console.Write("Do you want to create an opportunity for this conversion (Y/N)? "); string opp = Console.ReadLine(); lc.doNotCreateOpportunity = true; if (opp != null && opp.ToLower().Equals("y")) { lc.doNotCreateOpportunity = false; Console.Write("Enter the name of the opportunity to create.. "); string oppName = Console.ReadLine(); if (oppName == null) { Console.WriteLine("No opportunity name given, NO opportunity will be created."); lc.doNotCreateOpportunity = true; summary += " No opportunity will be created.\n"; } else { lc.opportunityName = oppName; summary += " An opportunity named: " + oppName + " will be created.\n"; } } //The lead needs to have it's status updated to reflect the conversion operation, //so we will ask the user to select the status to use Console.Write("Select the lead status to use to update the converted lead. "); lc.convertedStatus = getLeadStatus(); summary += " The converted lead will be assigned a status of " + lc.convertedStatus + ".\n"; //Finally, we have the option of notifying the owner of the lead that it //has been converted. Console.Write("Would you like to have an email sent to the owner of the lead after the conversion (Y/N)?"); string sendMail = Console.ReadLine(); if (sendMail != null && sendMail.ToLower().Equals("y")) { lc.sendNotificationEmail = true; summary += " Email notification will be sent.\n"; } else { lc.sendNotificationEmail = false; summary += " Email notificatino will NOT be sent.\n"; } string cont; Console.Write("\n\nDEBUG VALUES\n"); Console.WriteLine("account id: " + lc.accountId); Console.WriteLine("contact id: " + lc.contactId); Console.WriteLine("converted status: " + lc.convertedStatus); Console.WriteLine("do not create opp: " + lc.doNotCreateOpportunity); Console.WriteLine("lead id: " + lc.leadId); Console.WriteLine("opp name: " + lc.opportunityName); Console.WriteLine("overwrite lead source: " + lc.overwriteLeadSource); Console.WriteLine("send email: " + lc.sendNotificationEmail); Console.Write(summary + "\n Enter 'Y' to convert the lead."); cont = Console.ReadLine(); if (cont != null && cont.ToLower().Equals("y")) { apex.LeadConvertResult[] lcr = binding.convertLead(new LeadConvert[] { lc }); //although we only converted a single lead, we will use a loop to process the //results since the return value is an array of LeadConvertResults for (int i = 0; i < lcr.Length; i++) { if (lcr[i].success) { Console.WriteLine("Conversion of lead " + getFieldValue("FirstName", lead.Any) + " " + getFieldValue("LastName", lead.Any) + " was successful."); if (contact.Id.Equals(lcr[i].contactId)) Console.WriteLine(" Contact with ID of '" + lcr[i].contactId + "' was merged."); else Console.WriteLine(" Contact with ID of '" + lcr[i].contactId + "' was created."); if (lcr[i].opportunityId != null) Console.WriteLine(" An opportunity with an ID of '" + lcr[i].opportunityId + "' was created."); else Console.WriteLine(" No opportunity was created."); Console.WriteLine(" The contact was create in the account with an ID of '" + lcr[i].accountId + "'."); } else { Console.WriteLine("One or more errors where encountered during the lead conversion process...\n"); for (int j = 0; j < lcr[i].errors.Length; j++) { Console.WriteLine((j + 1) + lcr[0].errors[j].message); } } } } else { Console.WriteLine("Lead conversion was aborted."); } }
/// <remarks/> public void convertLeadAsync(LeadConvert[] leadConverts) { this.convertLeadAsync(leadConverts, null); }
/// <remarks/> public void convertLeadAsync(LeadConvert[] leadConverts, object userState) { if ((this.convertLeadOperationCompleted == null)) { this.convertLeadOperationCompleted = new System.Threading.SendOrPostCallback(this.OnconvertLeadOperationCompleted); } this.InvokeAsync("convertLead", new object[] { leadConverts}, this.convertLeadOperationCompleted, userState); }
/// <remarks/> public System.IAsyncResult BeginconvertLead(LeadConvert[] leadConverts, System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("convertLead", new object[] { leadConverts}, callback, asyncState); }