public static Map<String, Object> assignContactToAccount( DispatchContext dctx, Map<String, Object> context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = UtilCommon.getLocale(context); String contactPartyId = (String) context.get("contactPartyId"); String accountPartyId = (String) context.get("accountPartyId"); try { // check if this contact is already a contact of this account EntityCondition searchConditions = EntityCondition.makeCondition( EntityOperator.AND, EntityCondition.makeCondition("partyIdFrom", EntityOperator.EQUALS, contactPartyId), EntityCondition.makeCondition("partyIdTo", EntityOperator.EQUALS, accountPartyId), EntityCondition.makeCondition("roleTypeIdFrom", EntityOperator.EQUALS, "CONTACT"), EntityCondition.makeCondition("roleTypeIdTo", EntityOperator.EQUALS, "ACCOUNT"), EntityCondition.makeCondition( "partyRelationshipTypeId", EntityOperator.EQUALS, "CONTACT_REL_INV"), EntityUtil.getFilterByDateExpr()); List<GenericValue> existingRelationships = delegator.findByCondition("PartyRelationship", searchConditions, null, null); if (existingRelationships.size() > 0) { return UtilMessage.createAndLogServiceError( "CrmErrorContactAlreadyAssociatedToAccount", locale, MODULE); } // check if userLogin has CRMSFA_ACCOUNT_UPDATE permission for this account if (!CrmsfaSecurity.hasPartyRelationSecurity( security, "CRMSFA_ACCOUNT", "_UPDATE", userLogin, accountPartyId)) { return UtilMessage.createAndLogServiceError("CrmErrorPermissionDenied", locale, MODULE); } // create the party relationship between the Contact and the Account PartyHelper.createNewPartyToRelationship( accountPartyId, contactPartyId, "CONTACT", "CONTACT_REL_INV", null, UtilMisc.toList("ACCOUNT"), false, userLogin, delegator, dispatcher); } catch (GenericServiceException e) { return UtilMessage.createAndLogServiceError( e, "CrmErrorAssignContactToAccountFail", locale, MODULE); } catch (GenericEntityException e) { return UtilMessage.createAndLogServiceError( e, "CrmErrorAssignContactToAccountFail", locale, MODULE); } return ServiceUtil.returnSuccess(); }
/** * Creates an contact relationship of a given type for the given party and removes all previous * relationships of that type. This method helps avoid semantic mistakes and typos from the * repeated use of this code pattern. */ public static boolean createResponsibleContactRelationshipForParty( String partyId, String contactPartyId, GenericValue userLogin, Delegator delegator, LocalDispatcher dispatcher) throws GenericServiceException, GenericEntityException { return PartyHelper.createNewPartyToRelationship( partyId, contactPartyId, "CONTACT", "RESPONSIBLE_FOR", "CONTACT_OWNER", PartyHelper.TEAM_MEMBER_ROLES, true, userLogin, delegator, dispatcher); }