private void UpdateExistingContact(Guid contactId, DateTime birthday, int ageonCRM, IOrganizationService iOrgService) { objCRMHelper = new CRMHelper(); iOrgService = objCRMHelper.setSrvice(); objCRMHelper = new CRMHelper(); DateTime nextBirthDate; try { Entity objContact = new Entity(); objContact.LogicalName = "contact"; objContact.Id = contactId; int age = DateTime.Now.Year - birthday.Year; if (DateTime.Now.Month < birthday.Month || DateTime.Now.Month == birthday.Month && DateTime.Now.Day < birthday.Day) { age--; } objContact["rah_age"] = age; nextBirthDate = CalculateNextBirthday(birthday); objContact["rah_nextbirthday"] = nextBirthDate.ToLocalTime(); iOrgService.Update(objContact); } catch (SmtpException ex) { Console.WriteLine("SmtpException {0} \t", ex.ToString()); } catch (Exception exc) { Console.WriteLine("Exception {0} \t", exc.ToString()); } }
public void Run() { objCRMHelper = new CRMHelper(); _serviceProxy = objCRMHelper.setSrvice(); try { //<snippetWorkWithRelationships1> bool eligibleCreateOneToManyRelationship = EligibleCreateOneToManyRelationship("rah_country", "rah_territory"); Console.WriteLine("fileName" + fileName); if (eligibleCreateOneToManyRelationship) { CreateOneToManyRequest createOneToManyRelationshipRequest = new CreateOneToManyRequest { OneToManyRelationship = new OneToManyRelationshipMetadata { ReferencedEntity = "rah_country", ReferencingEntity = "rah_territory", SchemaName = "rah_country_rah_territory", AssociatedMenuConfiguration = new AssociatedMenuConfiguration { // Behavior = AssociatedMenuBehavior.UseLabel, Behavior = AssociatedMenuBehavior.UseLabel, Group = AssociatedMenuGroup.Details, Label = new Label("MASTER Country", 1033), Order = 10000 }, CascadeConfiguration = new CascadeConfiguration { Assign = CascadeType.NoCascade, Delete = CascadeType.Restrict, Merge = CascadeType.NoCascade, Reparent = CascadeType.NoCascade, Share = CascadeType.NoCascade, Unshare = CascadeType.NoCascade } }, Lookup = new LookupAttributeMetadata { SchemaName = "rah_parent_rah_countryid", DisplayName = new Label("Country Lookup", 1033), RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None), Description = new Label("MASTer Sample rah_country Lookup", 1033) } }; CreateOneToManyResponse createOneToManyRelationshipResponse = (CreateOneToManyResponse)_serviceProxy.Execute( createOneToManyRelationshipRequest); _oneToManyRelationshipId = createOneToManyRelationshipResponse.RelationshipId; _oneToManyRelationshipName = createOneToManyRelationshipRequest.OneToManyRelationship.SchemaName; Console.WriteLine( "The One-to-Many relationship has been created between {0} and {1}.", "rah_country", "rah_territory"); } } catch (FaultException <OrganizationServiceFault> ex) { Console.WriteLine("The application terminated with an error."); Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp); Console.WriteLine("Code: {0}", ex.Detail.ErrorCode); Console.WriteLine("Message: {0}", ex.Detail.Message); Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText); Console.WriteLine("Inner Fault: {0}", null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault"); } catch (System.TimeoutException ex) { Console.WriteLine("The application terminated with an error."); Console.WriteLine("Message: {0}", ex.Message); Console.WriteLine("Stack Trace: {0}", ex.StackTrace); Console.WriteLine("Inner Fault: {0}", null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message); } catch (System.Exception ex) { Console.WriteLine("The application terminated with an error."); Console.WriteLine(ex.Message); // Display the details of the inner exception. if (ex.InnerException != null) { Console.WriteLine(ex.InnerException.Message); FaultException <OrganizationServiceFault> fe = ex.InnerException as FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault>; if (fe != null) { Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp); Console.WriteLine("Code: {0}", fe.Detail.ErrorCode); Console.WriteLine("Message: {0}", fe.Detail.Message); Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText); Console.WriteLine("Inner Fault: {0}", null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault"); } } } }
public bool LoadContact() { try { objContact = new Contact(); objCRMHelper = new CRMHelper(); iOrgService = objCRMHelper.setSrvice(); // GetContactDetailFromCRM whos birthday id today. contactResults = GetContactDetailFromCRM(iOrgService); Console.WriteLine("Welcome {0} \t", contactResults.Entities.Count); // Console.ReadKey(); if (contactResults.Entities != null && contactResults.Entities.Count > 1) { foreach (Entity contact in contactResults.Entities) { // DateTime dobdate = default(DateTime); //DateTime defaultDate = default(DateTime); string fullname = contact["fullname"].ToString(); Console.WriteLine("fullname \t {0}", fullname + "\n"); if (contact.Attributes.Contains("contactid") && contact["contactid"] != null) { contactId = new Guid(contact["contactid"].ToString()); } // if (contact.Attributes.Contains("birthdate") && !string.IsNullOrEmpty(contact["birthdate"].ToString()) && contact["birthdate"].ToString()!= null) if (contact.Attributes.Contains("birthdate") && contact["birthdate"].ToString() != null) { // dobdate = ((DateTime)contact["birthdate"]).ToLocalTime(); dobdate = (DateTime)contact["birthdate"]; Console.WriteLine("dobdate \t {0}", dobdate + "\n"); } if (contact.Attributes.Contains("rah_age") && !string.IsNullOrEmpty(contact["rah_age"].ToString())) { age = (int)contact["rah_age"]; } if (contact.Attributes.Contains("birthdate") && contact["birthdate"].ToString() != null) { // dobdate = ((DateTime)contact["birthdate"]).ToLocalTime(); dobdate = (DateTime)contact["birthdate"]; Console.WriteLine("dobdate \t {0}", dobdate + "\n"); } // if (contactId != Guid.Empty && dobdate != null && defaultDate!= dobdate) if (contactId != Guid.Empty && dobdate != null) { //if (contact.Attributes.Contains("birthdate") && contact["birthdate"].ToString() != null) //{ objContact.UpdateExistingContact(contactId, dobdate, age, iOrgService); //} } } } } catch (Exception) { throw; } return(true); }