Esempio n. 1
0
 /// <summary>
 /// If it was created in Outlook and doesn't exist in CRM,  (in which case it won't yet have a
 /// magic SShouldSync property) then we need to guarantee changes made in CRM are copied back
 /// by setting the Sync to Outlook checkbox in CRM.
 /// </summary>
 /// <param name="contactIdInCRM">The identifier of the contact in the CRM system</param>
 /// <param name="syncProperty">If null, set the checkbox.</param>
 /// <param name="create">If provided and false, then remove rather than creating the relationship.</param>
 private static void EnsureSyncWithOutlookSetInCRM(CrmId contactIdInCRM, Outlook.UserProperty syncProperty, bool create = true)
 {
     if (syncProperty == null)
     {
         SetRelationshipParams info = new SetRelationshipParams
         {
             module1    = CrmModule,
             module1_id = contactIdInCRM.ToString(),
             module2    = "user_sync",
             module2_id = RestAPIWrapper.GetUserId(),
             delete     = create ? 0 : 1
         };
         RestAPIWrapper.SetRelationshipUnsafe(info);
     }
 }
        /// <summary>
        /// Try, in turn, each field in this list of candidate fields seeking one which allows a relationship
        /// to be successfully created.
        /// </summary>
        /// <remarks>
        /// If the common relationship field names don't work, brute force it by getting all the possibles.
        /// </remarks>
        /// <param name="relationship">The relationship we're trying to make.</param>
        /// <param name="candidateFields">Fields through which the relationship might be made.</param>
        /// <returns>True if the relationship was made.</returns>
        private static bool TrySetRelationship(SetRelationshipParams relationship, IEnumerable <Field> candidateFields)
        {
            bool result = false;

            foreach (Field field in candidateFields)
            {
                result |= TrySetRelationship(relationship, field.name.ToLower());

                if (result)
                {
                    break;
                }
            }

            return(result);
        }
        /// <summary>
        /// Sets up a CRM relationship to mimic an Outlook relationship
        /// </summary>
        /// <param name="meetingId">The ID of the meeting</param>
        /// <param name="foreignModule">the name of the module we're seeking to link with.</param>
        /// <returns>True if a relationship </returns>
        private bool SetCrmRelationshipFromOutlook(string meetingId, string foreignModule, string foreignId)
        {
            bool result = false;

            if (foreignId != String.Empty)
            {
                SetRelationshipParams info = new SetRelationshipParams
                {
                    module2    = AppointmentSyncing.CrmModule,
                    module2_id = meetingId,
                    module1    = foreignModule,
                    module1_id = foreignId
                };
                result = RestAPIWrapper.SetRelationshipUnsafe(info);
            }

            return(result);
        }
        /// <summary>
        /// Sets a CRM relationship and returns boolean success. 'Unsafe' because most
        /// callers ignore the result. Call 'SetRelationship' instead, which throws an
        /// exception on failure.
        /// </summary>
        /// <param name="relationship">The relationship to set.</param>
        public static bool SetRelationshipUnsafe(SetRelationshipParams relationship)
        {
            bool result;

            try
            {
                result = TrySetRelationship(relationship, Objective.Meeting);

                if (!result)
                {
                    Log.Warn("RestAPIWrapper.SetRelationshipUnsafe: failed to set relationship");
                }
            }
            catch (System.Exception exception)
            {
                Log.Error("RestAPIWrapper.SetRelationshipUnsafe:", exception);
                result = false;
            }

            return(result);
        }
        /// <summary>
        /// The protocols for how link fields are named vary. Try this possibility,
        /// and log failures.
        /// </summary>
        /// <param name="relationship">The relationship to set.</param>
        /// <param name="linkFieldName">The link field name to try.</param>
        /// <returns>True if the relationship was created, else false.</returns>
        public static bool TrySetRelationship(SetRelationshipParams relationship, string linkFieldName)
        {
            bool result;

            linkFieldName = linkFieldName.ToLower();

            if (EnsureLoggedIn())
            {
                object data = new
                {
                    @session         = SuiteCRMUserSession.id,
                    @module_name     = relationship.module1,
                    @module_id       = relationship.module1_id,
                    @link_field_name = linkFieldName,
                    @related_ids     = new string[] { relationship.module2_id },
                    @name_value_list = new NameValue[] { },
                    @delete          = relationship.delete
                };
                var value = SuiteCRMUserSession.RestServer.GetCrmResponse <RESTObjects.eNewSetRelationshipListResult>("set_relationship", data);

                if (value.Failed == 0)
                {
                    Log.Info($"RestAPIWrapper.TrySetRelationship: successfully set relationship using link field name '{linkFieldName}'");
                }
                else
                {
                    Log.Warn($"RestAPIWrapper.TrySetRelationship: failed to set relationship using link field name '{linkFieldName}'");
                }

                result = (value.Created != 0);
            }
            else
            {
                result = false;
            }

            return(result);
        }
 /// <summary>
 /// The protocols for how link fields are named vary. Try the most likely two possibilities,
 /// and log failures.
 /// </summary>
 /// <param name="relationship">The relationship to set.</param>
 /// <returns>True if the relationship was created, else false.</returns>
 public static bool TrySetRelationship(SetRelationshipParams relationship, Objective objective)
 {
     return(TrySetRelationship(relationship, $"{relationship.module2}") ||
            TrySetRelationship(relationship, $"{relationship.module2}_{relationship.module1}") ||
            TrySetRelationship(relationship, GetActivitiesLinks(relationship.module1, objective)));
 }