private EntityCollection getExisitingAccountDetails(Guid _accountId) { ColumnSet accountColumns = new ColumnSet("name", "fdx_goldmineaccountnumber", "fdx_gonogo", "address1_line1", "address1_line2", "address1_city", "fdx_stateprovinceid", "fdx_zippostalcodeid", "telephone1"); accountColumns.AddColumns("fdx_prospectgroup", "defaultpricelevelid", "fdx_prospectpriority", "fdx_prospectscore", "fdx_prospectpercentile", "fdx_ratesource", "fdx_pprrate", "fdx_subrate", "fdx_prospectradius", "fdx_prospectdatalastupdated", "fdx_prospectscoreblankmessage"); QueryExpression accountQuery = CRMQueryExpression.getQueryExpression("account", accountColumns, new CRMQueryExpression[] { new CRMQueryExpression("accountid", ConditionOperator.Equal, _accountId) }); EntityCollection accountCollection = service.RetrieveMultiple(accountQuery); return(accountCollection); }
private Entity getAccountMatchedLead(Entity _account, IOrganizationService _service) { Entity lead = new Entity(); QueryExpression queryExp = CRMQueryExpression.getQueryExpression("lead", new ColumnSet("leadid", "contactid", "parentcontactid", "accountid", "parentaccountid", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("accountid", ConditionOperator.Equal, _account.Id), new CRMQueryExpression("parentaccountid", ConditionOperator.Equal, _account.Id) }, LogicalOperator.Or); EntityCollection collection = _service.RetrieveMultiple(queryExp); if (collection.Entities.Count > 0) { lead = collection.Entities[0]; } return(lead); }
public LeadAssignment_Variables(Entity _leadEntity, IOrganizationService _service) { smartCrmSyncWebServiceUrl = DEV_ENVIRONMENT_URL; leadAssigned = true; acc_gmaccountno_exist = false; accountid = Guid.Empty; zip = Guid.Empty; zipTerritory = Guid.Empty; isGroupPractice = _leadEntity.Attributes.Contains("fdx_grppracactice") ? _leadEntity.GetAttributeValue <bool>("fdx_grppracactice") : false; if (_leadEntity.Attributes.Contains("fdx_zippostalcode")) { zip = ((EntityReference)_leadEntity.Attributes["fdx_zippostalcode"]).Id; Entity zipEntity = new Entity(); zipEntity = _service.Retrieve("fdx_zipcode", zip, new ColumnSet("fdx_zipcode", "fdx_timezone", "fdx_territory")); zipcodetext = zipEntity.Attributes.Contains("fdx_zipcode") ? zipEntity.Attributes["fdx_zipcode"].ToString() : ""; if (zipEntity.Contains("fdx_timezone")) { int timeZoneCode = Convert.ToInt32(zipEntity["fdx_timezone"]); QueryExpression tzDefinationQuery = CRMQueryExpression.getQueryExpression("timezonedefinition", new ColumnSet("standardname"), new CRMQueryExpression[] { new CRMQueryExpression("timezonecode", ConditionOperator.Equal, timeZoneCode) }); Entity tzDefination = _service.RetrieveMultiple(tzDefinationQuery).Entities[0]; DateTime timeUtc = DateTime.UtcNow; if (tzDefination.Attributes.Contains("standardname")) { TimeZoneInfo tzInfo = TimeZoneInfo.FindSystemTimeZoneById(tzDefination.Attributes["standardname"].ToString()); tzTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, tzInfo); } } if (zipEntity.Contains("fdx_territory")) { zipTerritory = ((EntityReference)zipEntity.Attributes["fdx_territory"]).Id; } } firstName = _leadEntity.Attributes.Contains("firstname") ? _leadEntity.Attributes["firstname"].ToString() : ""; lastName = _leadEntity.Attributes.Contains("lastname") ? _leadEntity.Attributes["lastname"].ToString() : ""; phone = _leadEntity.Attributes.Contains("telephone2") ? Regex.Replace(_leadEntity.Attributes["telephone2"].ToString(), @"[^0-9]+", "") : ""; telephone1 = _leadEntity.Attributes.Contains("telephone1") ? Regex.Replace(_leadEntity.Attributes["telephone1"].ToString(), @"[^0-9]+", "") : ""; email = _leadEntity.Attributes.Contains("emailaddress1") ? _leadEntity.Attributes["emailaddress1"].ToString() : ""; companyName = _leadEntity.Attributes.Contains("companyname") ? _leadEntity.Attributes["companyname"].ToString() : ""; title = _leadEntity.Attributes.Contains("fdx_jobtitlerole") ? CRMQueryExpression.GetOptionsSetTextForValue(_service, "lead", "fdx_jobtitlerole", ((OptionSetValue)_leadEntity.Attributes["fdx_jobtitlerole"]).Value) : ""; address1 = _leadEntity.Attributes.Contains("address1_line1") ? _leadEntity.Attributes["address1_line1"].ToString() : ""; address2 = _leadEntity.Attributes.Contains("address1_line2") ? _leadEntity.Attributes["address1_line2"].ToString() : ""; city = _leadEntity.Attributes.Contains("address1_city") ? _leadEntity.Attributes["address1_city"].ToString() : ""; state = _leadEntity.Attributes.Contains("fdx_stateprovince") ? (_service.Retrieve("fdx_state", ((EntityReference)_leadEntity.Attributes["fdx_stateprovince"]).Id, new ColumnSet("fdx_statecode"))).Attributes["fdx_statecode"].ToString() : ""; if ((((OptionSetValue)_leadEntity["leadsourcecode"]).Value == 2) || (((OptionSetValue)_leadEntity["leadsourcecode"]).Value == 3) || (((OptionSetValue)_leadEntity["leadsourcecode"]).Value == 5)) { if (_leadEntity.Attributes.Contains("parentaccountid")) { accountid = ((EntityReference)_leadEntity.Attributes["parentaccountid"]).Id; } } }
public void Execute(IServiceProvider serviceProvider) { int step = 0; string url = ""; string apiParm = ""; //Extract the tracing service for use in debugging sandboxed plug-ins.... step = 87; tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); //Obtain execution contest from the service provider.... step = 88; IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { step = 89; leadEntity = (Entity)context.InputParameters["Target"]; if (leadEntity.LogicalName != "lead") { return; } try { step = 90; IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); service = serviceFactory.CreateOrganizationService(context.UserId); //Format phone numbers to remove special characters.... step = 91; this.formatPhoneNumbers(); //Initialize lead variables.... step = 92; variables = new LeadAssignment_Variables(leadEntity, service); leadEntity["fdx_createdontime"] = variables.tzTime.Hour; //Set api parameters.... step = 93; apiParm = this.setApiParmFromLeadEntity(); //Set Connected lead, existing contact, existinng account and Lead owner based on criteria.... if (!variables.isGroupPractice) { EntityCollection entityCollection = new EntityCollection(); if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 1)).Entities.Count > 0) { tracingService.Trace("First name, last name and office phone on new lead matches First name, last name and office phone on existing lead"); step = 1; this.setConnectedLead(entityCollection.Entities[0]); step = 2; this.setExistingContact(entityCollection.Entities[0], "lead"); step = 3; this.setExistingAccount(entityCollection.Entities[0], "lead"); step = 4; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 2)).Entities.Count > 0) { tracingService.Trace("First name, last name and office phone on new lead matches First name, last name and office phone on existing contact"); step = 5; this.setExistingContact(entityCollection.Entities[0], "contact"); step = 6; this.setExistingAccount(entityCollection.Entities[0], "contact"); step = 7; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 3)).Entities.Count > 0) { tracingService.Trace("Office Phone on new lead matches office phone on existing lead"); step = 8; this.setConnectedLead(entityCollection.Entities[0]); step = 9; this.setExistingAccount(entityCollection.Entities[0], "lead"); step = 10; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 4)).Entities.Count > 0) { tracingService.Trace("Email on new lead matches email on existing lead"); step = 11; this.setConnectedLead(entityCollection.Entities[0]); step = 12; this.setExistingAccount(entityCollection.Entities[0], "lead"); step = 13; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 5)).Entities.Count > 0) { tracingService.Trace("Company name and zipcode on new lead matches company name and zipcode on an account"); step = 14; Entity matchedLead = new Entity(); matchedLead = this.getAccountMatchedLead(entityCollection.Entities[0]); step = 15; if (matchedLead.Id != Guid.Empty) { step = 16; this.setConnectedLead(matchedLead); } step = 17; this.setExistingAccount(entityCollection.Entities[0], "account"); step = 18; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 6)).Entities.Count > 0) { tracingService.Trace("Company name and zipcode on new lead matches company name and zipcode on a lead"); step = 19; this.setConnectedLead(entityCollection.Entities[0]); step = 20; this.setExistingAccount(entityCollection.Entities[0], "lead"); step = 21; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 7)).Entities.Count > 0) { tracingService.Trace("Email on new lead matches email on account"); Entity matchedLead = new Entity(); step = 22; matchedLead = this.getAccountMatchedLead(entityCollection.Entities[0]); step = 23; if (matchedLead.Id != Guid.Empty) { step = 24; this.setConnectedLead(matchedLead); } this.setExistingAccount(entityCollection.Entities[0], "account"); step = 25; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 8)).Entities.Count > 0) { tracingService.Trace("Email on new lead matches email on existing contact"); step = 26; this.setExistingAccount(entityCollection.Entities[0], "contact"); step = 27; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 9)).Entities.Count > 0) { tracingService.Trace("Lastname and phone on new lead matches lastname and any phone on contact"); step = 28; this.setExistingAccount(entityCollection.Entities[0], "contact"); step = 29; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 10)).Entities.Count > 0) { tracingService.Trace("Lastname and phone on new lead matches lastname and any phone on lead"); step = 30; this.setConnectedLead(entityCollection.Entities[0]); step = 31; this.setExistingAccount(entityCollection.Entities[0], "lead"); step = 32; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 11)).Entities.Count > 0) { tracingService.Trace("Office Phone on new lead matches any phone on a lead"); step = 33; this.setConnectedLead(entityCollection.Entities[0]); step = 34; this.setExistingAccount(entityCollection.Entities[0], "lead"); step = 35; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 12)).Entities.Count > 0) { tracingService.Trace("Mobile Phone on new lead matches any phone on a lead"); step = 36; this.setConnectedLead(entityCollection.Entities[0]); step = 37; this.setExistingAccount(entityCollection.Entities[0], "lead"); step = 38; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 13)).Entities.Count > 0) { tracingService.Trace("Office Phone on new lead matches any phone on account"); Entity matchedLead = new Entity(); step = 39; matchedLead = this.getAccountMatchedLead(entityCollection.Entities[0]); step = 40; if (matchedLead.Id != Guid.Empty) { step = 41; this.setConnectedLead(matchedLead); } step = 42; this.setExistingAccount(entityCollection.Entities[0], "account"); step = 43; this.setLeadOwner(entityCollection.Entities[0]); //Code added as part of S893 by Meghana on Lead create for Incoming Phone call,web creation/import //wf name - fdx_Lead update Existing Account step = 44; //this.updateExistingAccount(matchedLead.Id); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 14)).Entities.Count > 0) { tracingService.Trace("Mobile Phone on new lead matches any phone on account"); Entity matchedLead = new Entity(); step = 45; matchedLead = this.getAccountMatchedLead(entityCollection.Entities[0]); step = 46; if (matchedLead.Id != Guid.Empty) { step = 47; this.setConnectedLead(matchedLead); } step = 48; this.setExistingAccount(entityCollection.Entities[0], "account"); step = 49; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 15)).Entities.Count > 0) { tracingService.Trace("Office Phone on new lead matches any phone on contact"); step = 50; this.setExistingAccount(entityCollection.Entities[0], "contact"); step = 51; this.setLeadOwner(entityCollection.Entities[0]); } else if ((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 16)).Entities.Count > 0) { tracingService.Trace("Mobile Phone on new lead matches any phone on contact"); step = 52; this.setExistingAccount(entityCollection.Entities[0], "contact"); step = 53; this.setLeadOwner(entityCollection.Entities[0]); } else if (((entityCollection = CRMQueryExpression.GetLeadEntityCollection(variables, service, 17)).Entities.Count > 0) && ((((OptionSetValue)leadEntity["leadsourcecode"]).Value == 1) || (((OptionSetValue)leadEntity["leadsourcecode"]).Value == 4))) { tracingService.Trace("Assiagn SAE based on zipcode"); step = 54; Entity user = entityCollection.Entities[0]; step = 55; leadEntity["ownerid"] = new EntityReference("systemuser", user.Id); } else if (((OptionSetValue)leadEntity["leadsourcecode"]).Value == 1 || ((OptionSetValue)leadEntity["leadsourcecode"]).Value == 4) { tracingService.Trace("Set lead Assigned to False and trigger next@bat only for Web Leads (new leads only and not cloned)"); variables.leadAssigned = false; step = 56; if (((OptionSetValue)leadEntity["leadsourcecode"]).Value == 1 && !leadEntity.Attributes.Contains("fdx_leadid")) { leadEntity["fdx_snb"] = true; } } } #region Set the address field as per the account if there is an existing account.... if (variables.accountid != Guid.Empty) { step = 57; EntityCollection accountCollection = this.getExisitingAccountDetails(variables.accountid); if (accountCollection.Entities.Count > 0) { Entity account = new Entity(); account = accountCollection.Entities[0]; step = 58; if (!account.Attributes.Contains("fdx_goldmineaccountnumber")) { step = 59; apiParm = this.setApiParmFromAccountEntity(account); url = variables.smartCrmSyncWebServiceUrl + "/lead/createlead?" + apiParm; } else { step = 60; variables.acc_gmaccountno_exist = true; leadEntity["fdx_goldmineaccountnumber"] = account.Attributes["fdx_goldmineaccountnumber"].ToString(); leadEntity["fdx_gonogo"] = account.Attributes["fdx_gonogo"]; step = 61; ProspectData prospectData = this.GetProspectDataFromAccount(account); step = 62; CopyLeadProspectDataToSharedVariable(context.SharedVariables, prospectData); } } } else { step = 63; url = variables.smartCrmSyncWebServiceUrl + "/lead/createlead?" + apiParm; } #endregion #region Call and update from API.... tracingService.Trace(url); Lead leadObj = new Lead(); if (!variables.acc_gmaccountno_exist) { step = 64; const string token = "8b6asd7-0775-4278-9bcb-c0d48f800112"; var uri = new Uri(url); step = 65; var request = WebRequest.Create(uri); request.Method = WebRequestMethods.Http.Post; request.ContentType = "application/json"; request.ContentLength = 0; request.Headers.Add("Authorization", token); step = 66; using (var getResponse = request.GetResponse()) { step = 67; DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Lead)); step = 68; leadObj = (Lead)serializer.ReadObject(getResponse.GetResponseStream()); tracingService.Trace("ProspectScoreBlankMessage :" + leadObj.prospectScoreBlankMessage); if (string.IsNullOrEmpty(leadObj.prospectScoreBlankMessage)) { leadObj.prospectScoreBlankMessage = "Valid Address"; } step = 69; EntityCollection priceLists = this.GetPriceListByName(leadObj.priceListName, service); step = 70; EntityCollection prospectGroups = this.GetProspectGroupByName(leadObj.prospectGroup, service); step = 71; ProspectData prospectData = this.GetProspectDataFromWebService(leadObj); prospectData.PriceListName = leadObj.priceListName; int lengthoferror = leadObj.prospectScoreBlankMessage.Length; tracingService.Trace("lengthoferror-" + lengthoferror); //if(lengthoferror >= 1500) //{ // string trimerror = leadObj.prospectScoreBlankMessage.Substring(0, 1400); // leadObj.prospectScoreBlankMessage = leadObj.prospectScoreBlankMessage.Substring(0, 1499); // tracingService.Trace("trimerrormessage :" + trimerror); //} step = 72; if (priceLists.Entities.Count == 1) { prospectData.PriceListId = priceLists.Entities[0].Id; } step = 73; if (prospectGroups.Entities.Count == 1) { prospectData.ProspectGroupId = prospectGroups.Entities[0].Id; } step = 74; leadEntity["fdx_goldmineaccountnumber"] = leadObj.goldMineId; if (leadObj.goNoGo) { leadEntity["fdx_gonogo"] = new OptionSetValue(756480000); } else { leadEntity["fdx_gonogo"] = new OptionSetValue(756480001); } step = 75; if (variables.accountid != Guid.Empty) { step = 76; IOrganizationService impersonatedService = serviceFactory.CreateOrganizationService(null); step = 77; Entity acc = new Entity("account") { Id = variables.accountid }; acc.Attributes["fdx_goldmineaccountnumber"] = leadObj.goldMineId; acc.Attributes["fdx_gonogo"] = leadObj.goNoGo ? new OptionSetValue(756480000) : new OptionSetValue(756480001); step = 78; this.UpdateProspectDataOnAccount(acc, prospectData); step = 79; impersonatedService.Update(acc); } step = 80; CopyLeadProspectDataToSharedVariable(context.SharedVariables, prospectData); step = 81; tracingService.Trace(GetProspectDataString(prospectData)); tracingService.Trace("Prospect Data Updated in AssignLead_Create"); } } #endregion #region Condition to assign Lead to Lead Review Team.... if (!variables.leadAssigned && !leadObj.goNoGo && ((((OptionSetValue)leadEntity["leadsourcecode"]).Value == 1) || (((OptionSetValue)leadEntity["leadsourcecode"]).Value == 4))) { step = 82; leadEntity["fdx_snb"] = false; QueryExpression teamQuery = CRMQueryExpression.getQueryExpression("team", new ColumnSet("name"), new CRMQueryExpression[] { new CRMQueryExpression("name", ConditionOperator.Equal, "Lead Review Team") }); step = 83; EntityCollection teamCollection = service.RetrieveMultiple(teamQuery); step = 84; if (teamCollection.Entities.Count > 0) { Entity team = teamCollection.Entities[0]; leadEntity["ownerid"] = new EntityReference("team", team.Id); } tracingService.Trace("Condition to assign Lead to Lead Review Team"); } #endregion #region Condition if Cloned Lead: override owner of cloned lead = originating lead's owner if (leadEntity.Attributes.Contains("fdx_leadid")) { step = 85; Entity OriginatingLead = service.Retrieve("lead", ((EntityReference)leadEntity.Attributes["fdx_leadid"]).Id, new ColumnSet("ownerid", "owningteam")); step = 86; if (OriginatingLead.Attributes.Contains("owningteam")) { leadEntity["ownerid"] = new EntityReference(OriginatingLead["owningteam"] != null ? "team" : "systemuser", ((EntityReference)OriginatingLead.Attributes["ownerid"]).Id); } else { leadEntity["ownerid"] = new EntityReference("systemuser", ((EntityReference)OriginatingLead.Attributes["ownerid"]).Id); } tracingService.Trace("Condition if Cloned Lead: override owner of cloned lead = originating lead's owner"); } #endregion } catch (FaultException <OrganizationServiceFault> ex) { throw new InvalidPluginExecutionException(string.Format("An error occurred in the LeadAssignment_Create plug-in at Step {0}.", step), ex); } catch (Exception ex) { tracingService.Trace("LeadAssignment_Create: step {0}, {1}", step, ex.ToString()); throw; } } }
public static EntityCollection GetLeadEntityCollection(LeadAssignment_Variables _variables, IOrganizationService _service, int _matchingCriteria) { EntityCollection entities = new EntityCollection(); QueryExpression queryExp = new QueryExpression(); switch (_matchingCriteria) { case 1: //First name, last name and office phone on new lead matches First name, last name and office phone on existing lead queryExp = CRMQueryExpression.getQueryExpression("lead", new ColumnSet("leadid", "contactid", "parentcontactid", "accountid", "parentaccountid", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("firstname", ConditionOperator.Equal, _variables.firstName), new CRMQueryExpression("lastname", ConditionOperator.Equal, _variables.lastName), new CRMQueryExpression("telephone2", ConditionOperator.Equal, _variables.phone) }); entities = _service.RetrieveMultiple(queryExp); break; case 2: //First name, last name and office phone on new lead matches First name, last name and office phone on existing contact queryExp = CRMQueryExpression.getQueryExpression("contact", new ColumnSet("parentcustomerid", "fullname", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("firstname", ConditionOperator.Equal, _variables.firstName), new CRMQueryExpression("lastname", ConditionOperator.Equal, _variables.lastName), new CRMQueryExpression("telephone2", ConditionOperator.Equal, _variables.phone) }); entities = _service.RetrieveMultiple(queryExp); break; case 3: //Office Phone on new lead matches office phone on existing lead queryExp = CRMQueryExpression.getQueryExpression("lead", new ColumnSet("leadid", "contactid", "parentcontactid", "accountid", "parentaccountid", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("telephone2", ConditionOperator.Equal, _variables.phone) }); entities = _service.RetrieveMultiple(queryExp); break; case 4: //Email on new lead matches email on existing lead if (_variables.email != "") { queryExp = CRMQueryExpression.getQueryExpression("lead", new ColumnSet("leadid", "contactid", "parentcontactid", "accountid", "parentaccountid", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("emailaddress1", ConditionOperator.Equal, _variables.email) }); entities = _service.RetrieveMultiple(queryExp); } break; case 5: //Company name and zipcode on new lead matches company name and zipcode on an account if (_variables.companyName != "" && _variables.zipcodetext != "") { queryExp = CRMQueryExpression.getQueryExpression("account", new ColumnSet("accountid", "primarycontactid", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("name", ConditionOperator.Equal, _variables.companyName), new CRMQueryExpression("address1_postalcode", ConditionOperator.Equal, _variables.zipcodetext) }); entities = _service.RetrieveMultiple(queryExp); } break; case 6: //Company name and zipcode on new lead matches company name and zipcode on a lead if (_variables.companyName != "" && _variables.zipcodetext != "") { queryExp = CRMQueryExpression.getQueryExpression("lead", new ColumnSet("leadid", "contactid", "parentcontactid", "accountid", "parentaccountid", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("companyname", ConditionOperator.Equal, _variables.companyName), new CRMQueryExpression("address1_postalcode", ConditionOperator.Equal, _variables.zipcodetext) }); entities = _service.RetrieveMultiple(queryExp); } break; case 7: //Email on new lead matches email on account if (_variables.email != "") { queryExp = CRMQueryExpression.getQueryExpression("account", new ColumnSet("accountid", "primarycontactid", "ownerid"), new CRMQueryExpression[] { new CRMQueryExpression("emailaddress1", ConditionOperator.Equal, _variables.email) }); entities = _service.RetrieveMultiple(queryExp); } break; case 8: //Email on new lead matches email on existing contact if (_variables.email != "") { queryExp = CRMQueryExpression.getQueryExpression("contact", new ColumnSet("parentcustomerid", "fullname", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("emailaddress1", ConditionOperator.Equal, _variables.email) }); entities = _service.RetrieveMultiple(queryExp); } break; case 9: //Lastname and phone on new lead matches lastname and any phone on contact queryExp = CRMQueryExpression.getQueryWithChildExpression("contact", new ColumnSet("parentcustomerid", "fullname", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("lastname", ConditionOperator.Equal, _variables.lastName), new CRMQueryExpression("telephone2", ConditionOperator.Equal, _variables.phone), new CRMQueryExpression("telephone1", ConditionOperator.Equal, _variables.phone), new CRMQueryExpression("telephone3", ConditionOperator.Equal, _variables.phone) }); entities = _service.RetrieveMultiple(queryExp); break; case 10: //Lastname and phone on new lead matches lastname and any phone on lead queryExp = CRMQueryExpression.getQueryWithChildExpression("lead", new ColumnSet("leadid", "contactid", "parentcontactid", "accountid", "parentaccountid", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("lastname", ConditionOperator.Equal, _variables.lastName), new CRMQueryExpression("telephone2", ConditionOperator.Equal, _variables.phone), new CRMQueryExpression("telephone1", ConditionOperator.Equal, _variables.phone), new CRMQueryExpression("telephone3", ConditionOperator.Equal, _variables.phone) }); entities = _service.RetrieveMultiple(queryExp); break; case 11: //Office Phone on new lead matches any phone on a lead queryExp = CRMQueryExpression.getQueryExpression("lead", new ColumnSet("leadid", "contactid", "parentcontactid", "accountid", "parentaccountid", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("telephone2", ConditionOperator.Equal, _variables.phone), new CRMQueryExpression("telephone1", ConditionOperator.Equal, _variables.phone), new CRMQueryExpression("telephone3", ConditionOperator.Equal, _variables.phone) }, LogicalOperator.Or); entities = _service.RetrieveMultiple(queryExp); break; case 12: //Mobile Phone on new lead matches any phone on a lead if (_variables.telephone1 != "") { queryExp = CRMQueryExpression.getQueryExpression("lead", new ColumnSet("leadid", "contactid", "parentcontactid", "accountid", "parentaccountid", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("telephone2", ConditionOperator.Equal, _variables.telephone1), new CRMQueryExpression("telephone1", ConditionOperator.Equal, _variables.telephone1), new CRMQueryExpression("telephone3", ConditionOperator.Equal, _variables.telephone1) }, LogicalOperator.Or); entities = _service.RetrieveMultiple(queryExp); } break; case 13: //Office Phone on new lead matches any phone on account queryExp = CRMQueryExpression.getQueryExpression("account", new ColumnSet("accountid", "primarycontactid", "ownerid"), new CRMQueryExpression[] { new CRMQueryExpression("telephone2", ConditionOperator.Equal, _variables.phone), new CRMQueryExpression("telephone1", ConditionOperator.Equal, _variables.phone), new CRMQueryExpression("telephone3", ConditionOperator.Equal, _variables.phone) }, LogicalOperator.Or); entities = _service.RetrieveMultiple(queryExp); break; case 14: //Mobile Phone on new lead matches any phone on account if (_variables.telephone1 != "") { queryExp = CRMQueryExpression.getQueryExpression("account", new ColumnSet("accountid", "primarycontactid", "ownerid"), new CRMQueryExpression[] { new CRMQueryExpression("telephone2", ConditionOperator.Equal, _variables.telephone1), new CRMQueryExpression("telephone1", ConditionOperator.Equal, _variables.telephone1), new CRMQueryExpression("telephone3", ConditionOperator.Equal, _variables.telephone1) }, LogicalOperator.Or); entities = _service.RetrieveMultiple(queryExp); } break; case 15: //Office Phone on new lead matches any phone on contact queryExp = CRMQueryExpression.getQueryExpression("contact", new ColumnSet("parentcustomerid", "fullname", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("telephone2", ConditionOperator.Equal, _variables.phone), new CRMQueryExpression("telephone1", ConditionOperator.Equal, _variables.phone), new CRMQueryExpression("telephone3", ConditionOperator.Equal, _variables.phone) }, LogicalOperator.Or); entities = _service.RetrieveMultiple(queryExp); break; case 16: //Mobile Phone on new lead matches any phone on contact if (_variables.telephone1 != "") { queryExp = CRMQueryExpression.getQueryExpression("contact", new ColumnSet("parentcustomerid", "fullname", "ownerid", "owningteam"), new CRMQueryExpression[] { new CRMQueryExpression("telephone2", ConditionOperator.Equal, _variables.telephone1), new CRMQueryExpression("telephone1", ConditionOperator.Equal, _variables.telephone1), new CRMQueryExpression("telephone3", ConditionOperator.Equal, _variables.telephone1) }, LogicalOperator.Or); entities = _service.RetrieveMultiple(queryExp); } break; case 17: //Assiagn SAE based on zipcode if (_variables.zipTerritory != Guid.Empty) { Entity territory = new Entity(); territory = _service.Retrieve("territory", _variables.zipTerritory, new ColumnSet("managerid")); if (territory.Attributes.Contains("managerid")) { queryExp = CRMQueryExpression.getQueryExpression("systemuser", new ColumnSet(true), new CRMQueryExpression[] { new CRMQueryExpression("systemuserid", ConditionOperator.Equal, ((EntityReference)territory.Attributes["managerid"]).Id) }); entities = _service.RetrieveMultiple(queryExp); } } break; } return(entities); }