📄 leadsservices.java
字号:
} Map input = null; // used later for service inputs try { GenericValue lead = delegator.findByPrimaryKey("PartySummaryCRMView", UtilMisc.toMap("partyId", leadPartyId)); // create a PartyRole of type CONTACT for the lead Map serviceResults = dispatcher.runSync("createPartyRole", UtilMisc.toMap("partyId", leadPartyId, "roleTypeId", "CONTACT", "userLogin", userLogin)); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); } // set the status of the lead to PTYLEAD_CONVERTED serviceResults = dispatcher.runSync("setPartyStatus", UtilMisc.toMap("partyId", leadPartyId, "statusId", "PTYLEAD_CONVERTED", "userLogin", userLogin)); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); } // if no account was given, then create an account based on the PartySupplementalData of the lead if (accountPartyId == null) { input = UtilMisc.toMap("groupName", lead.getString("companyName"), "description", lead.getString("description"), "userLogin", userLogin); input.put("parentPartyId", lead.getString("parentPartyId")); input.put("annualRevenue", lead.getDouble("annualRevenue")); input.put("currencyUomId", lead.getString("currencyUomId")); input.put("numberEmployees", lead.getLong("numberEmployees")); input.put("industryEnumId", lead.getString("industryEnumId")); input.put("ownershipEnumId", lead.getString("ownershipEnumId")); input.put("sicCode", lead.getString("sicCode")); input.put("tickerSymbol", lead.getString("tickerSymbol")); serviceResults = dispatcher.runSync("crmsfa.createAccount", input); if (ServiceUtil.isError(serviceResults)) { return serviceResults; } accountPartyId = (String) serviceResults.get("partyId"); // copy all the datasources over to the new account List dataSources = delegator.findByAnd("PartyDataSource", UtilMisc.toMap("partyId", leadPartyId)); for (Iterator iter = dataSources.iterator(); iter.hasNext(); ) { GenericValue dataSource = (GenericValue) iter.next(); serviceResults = dispatcher.runSync("createPartyDataSource", UtilMisc.toMap("partyId", accountPartyId, "dataSourceId", dataSource.getString("dataSourceId"), "userLogin", userLogin)); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); } } // copy all the contact mechs to the account serviceResults = dispatcher.runSync("copyPartyContactMechs", UtilMisc.toMap("partyIdFrom", leadPartyId, "partyIdTo", accountPartyId, "userLogin", userLogin)); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); } } // erase (null out) the PartySupplementalData fields from the lead lead.set("parentPartyId", null); lead.set("annualRevenue", null); lead.set("currencyUomId", null); lead.set("numberEmployees", null); lead.set("industryEnumId", null); lead.set("ownershipEnumId", null); lead.set("sicCode", null); lead.set("tickerSymbol", null); lead.store(); // assign the lead, who is now a contact, to the account input = UtilMisc.toMap("contactPartyId", leadPartyId, "accountPartyId", accountPartyId, "userLogin", userLogin); serviceResults = dispatcher.runSync("crmsfa.assignContactToAccount", input); if (ServiceUtil.isError(serviceResults)) { return serviceResults; } // expire all lead party relationships (roleTypeIdFrom = PROSPECT) List partyRelationships = delegator.findByAnd("PartyRelationship", UtilMisc.toMap("partyIdFrom", leadPartyId, "roleTypeIdFrom", "PROSPECT")); PartyHelper.expirePartyRelationships(partyRelationships, UtilDateTime.nowTimestamp(), dispatcher, userLogin); // make the userLogin a RESPONSIBLE_FOR CONTACT_OWNER of the CONTACT PartyHelper.createNewPartyToRelationship(userLogin.getString("partyId"), leadPartyId, "CONTACT", "RESPONSIBLE_FOR", "CONTACT_OWNER", PartyHelper.TEAM_MEMBER_ROLES, true, userLogin, delegator, dispatcher); // now we need to assign the account and contact to the lead's work efforts and expire all the lead ones List associations = EntityUtil.filterByDate(delegator.findByAnd("WorkEffortPartyAssignment", UtilMisc.toMap("partyId", leadPartyId))); for (Iterator iter = associations.iterator(); iter.hasNext(); ) { GenericValue wepa = (GenericValue) iter.next(); ModelService service = dctx.getModelService("assignPartyToWorkEffort"); input = service.makeValid(wepa, "IN"); input.put("userLogin", userLogin); // expire the current lead association (done by hand because service is suspect) wepa.set("thruDate", UtilDateTime.nowTimestamp()); wepa.store(); // assign the account to the work effort input.put("partyId", accountPartyId); input.put("fromDate", null); input.put("thruDate", null); input.put("roleTypeId", "ACCOUNT"); serviceResults = dispatcher.runSync("assignPartyToWorkEffort", input); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); } // assign the contact to the work effort input.put("partyId", leadPartyId); input.put("fromDate", null); input.put("thruDate", null); input.put("roleTypeId", "CONTACT"); serviceResults = dispatcher.runSync("assignPartyToWorkEffort", input); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); } } // opportunities assigned to the lead have to be updated to refer to both contact and account List oppRoles = delegator.findByAnd("SalesOpportunityRole", UtilMisc.toMap("partyId", leadPartyId, "roleTypeId", "PROSPECT")); for (Iterator iter = oppRoles.iterator(); iter.hasNext(); ) { GenericValue oppRole = (GenericValue) iter.next(); // create a CONTACT role using the leadPartyId input = UtilMisc.toMap("partyId", leadPartyId, "salesOpportunityId", oppRole.get("salesOpportunityId"), "roleTypeId", "CONTACT"); GenericValue contactOppRole = delegator.makeValue("SalesOpportunityRole", input); contactOppRole.create(); // create an ACCOUNT role for the new accountPartyId input = UtilMisc.toMap("partyId", accountPartyId, "salesOpportunityId", oppRole.get("salesOpportunityId"), "roleTypeId", "ACCOUNT"); GenericValue accountOppRole = delegator.makeValue("SalesOpportunityRole", input); accountOppRole.create(); // delete the PROSPECT role oppRole.remove(); } // set the status of the lead to PTYLEAD_CONVERTED serviceResults = dispatcher.runSync("setPartyStatus", UtilMisc.toMap("partyId", leadPartyId, "statusId", "PTYLEAD_CONVERTED", "userLogin", userLogin)); if (ServiceUtil.isError(serviceResults)) { return UtilCommon.createAndLogServiceError(serviceResults, "CrmErrorConvertLeadFail", locale, module); } } catch (GenericServiceException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateLeadFail", locale, module); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorUpdateLeadFail", locale, module); } // put leadPartyId as partyId Map results = ServiceUtil.returnSuccess(); results.put("partyId", leadPartyId); return results; } public static Map reassignLeadResponsibleParty(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String leadPartyId = (String) context.get("leadPartyId"); String newPartyId = (String) context.get("newPartyId"); // ensure reassign permission on this lead if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_LEAD", "_REASSIGN", userLogin, leadPartyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } try { // reassign relationship with this helper method, which expires previous ones boolean result = PartyHelper.createNewPartyToRelationship(newPartyId, leadPartyId, "PROSPECT", "RESPONSIBLE_FOR", "LEAD_OWNER", PartyHelper.TEAM_MEMBER_ROLES, true, userLogin, delegator, dispatcher); if (result == false) { return UtilCommon.createAndLogServiceError("CrmErrorReassignFail", locale, module); } } catch (GenericServiceException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorReassignFail", locale, module); } return ServiceUtil.returnSuccess(); } /** * Delete a "new" lead. A new lead has status PTYLEAD_NEW, PTYLEAD_ASSIGNED or PTYLEAD_QUALIFIED. * This will physically remove the lead from the Party entity and related entities. * If the party was successfully deleted, the method will return a service success, otherwise it * will return a service error with the reason. */ public static Map deleteLead(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); String leadPartyId = (String) context.get("leadPartyId"); // ensure delete permission on this lead if (!CrmsfaSecurity.hasPartyRelationSecurity(security, "CRMSFA_LEAD", "_DELETE", userLogin, leadPartyId)) { return UtilCommon.createAndLogServiceError("CrmErrorPermissionDenied", locale, module); } try { // first ensure the lead is "new" (note that there's no need to check for role because only leads can have these statuses) GenericValue lead = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", leadPartyId)); if (lead == null) { return UtilCommon.createAndLogServiceError("Lead [" + leadPartyId + "] not found.", "CrmErrorDeleteLeadFail", locale, module); } String statusId = lead.getString("statusId"); if (statusId == null || !(statusId.equals("PTYLEAD_NEW") || statusId.equals("PTYLEAD_ASSIGNED") || statusId.equals("PTYLEAD_QUALIFIED"))) { return UtilCommon.createAndLogServiceError("Lead [" + leadPartyId + "] cannot be deleted. Only new, assigned or qualified leads may be deleted.", "CrmErrorDeleteLeadFail", locale, module); } // delete! PartyHelper.deleteCrmParty(leadPartyId, delegator); } catch (GenericEntityException e) { return UtilCommon.createAndLogServiceError(e, "CrmErrorDeleteLeadFail", locale, module); } return ServiceUtil.returnSuccess(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -