public void Example(IOrganizationService service) { /* Notes: * - Use Delete when removing a small number of records * - Use BulkDeleteRequest/Response when you need to remove larger number of records * - Remember delete can be blocked by relationship rules (cascade, etc..) for related data * - Same rules can cause delete of one record to delete many records * - Choose beetween Delete and Inactivate of records */ entities.Account acc = new entities.Account(); acc.CreditLimit = new Money((decimal)50000); Guid accountID = service.Create(acc); entities.Account retAccount = service.Retrieve("account", accountID, new ColumnSet("creditlimit")) as entities.Account; service.Delete(retAccount.LogicalName, retAccount.Id); }
public void Example(IOrganizationService service) { entities.Account earlyBoundAccount = new entities.Account(); earlyBoundAccount.Name = "Cool Business"; Guid accountID = service.Create(earlyBoundAccount); entities.Contact earlyBoundContact = new entities.Contact(); earlyBoundContact.FirstName = "Jimmy"; earlyBoundContact.LastName = "Beans"; Guid contactID = service.Create(earlyBoundContact); var relInfo = new Relationship("account_primary_contact"); var relatedIDs = new EntityReferenceCollection(); relatedIDs.Add(earlyBoundAccount.ToEntityReference()); // How to use Associate: service.Associate(entities.Contact.EntityLogicalName, contactID, relInfo, relatedIDs); // How to use Disassociate: service.Disassociate(entities.Contact.EntityLogicalName, contactID, relInfo, relatedIDs); }
public void Example(IOrganizationService service) { // NOTES: // - You can use importsequencenumber on create for things like import batch number, etc... /* *********************************************************** * EXAMPLE: 'Late Bound' Entity * (late-bound means that it is not typed) * ***********************************************************/ /* **** SETTING different kinds of fields on a late-bound **** */ Entity account = new Entity("account"); account["name"] = "test"; // make sure that 'name' is an attribute that exists, // and that its type is string, otherwise error // INTEGER FIELDS account["numberofemployees"] = 12345; account["numberofemployees"] = "12345"; // this will throw an error // OPTION SET FIELDS account["accountcategorycode"] = new OptionSetValue(1); // MONEY FIELDS account["creditlimit"] = new Money((decimal)50000); // LOOKUP FIELDS var contact = new Entity("contact") { Id = Guid.NewGuid() }; // dummy contact account["primarycontactid"] = new EntityReference(contact.LogicalName, contact.Id); // LOOKUP FIELDS (method 2): var contact2 = new Entity("contact") { Id = Guid.NewGuid() }; // dummy contact account["primarycontactid"] = contact.ToEntityReference(); // Create using the IOrganizationService Guid accountID = service.Create(account); // Retrieve the account we just created: entities.Account retAccount = service.Retrieve("account", accountID, new ColumnSet(true)) as entities.Account; // Some system fields, such as createdon, can be overridden with special fields: account["overriddencreatedon"] = new DateTime(2011, 11, 1, 1, 00, 00); // - During the objects creation, the 'overriddencreatedon' that we set, and the // 'createdon' system field will swap /* *********************************************************** * EXAMPLE: 'EARLY Bound' Entity * (early-bound means that we have defined a class for each * entity type in our CRM) * ***********************************************************/ entities.Account account2 = new entities.Account(); account2.Name = "Snapographcs, LLC"; account2.NumberOfEmployees = 124; account2.AccountCategoryCode = new OptionSetValue(1); account2.CreditLimit = new Money((decimal)50000); // Create using the IOrganizationService Guid accountID2 = service.Create(account2); // Retrieve the account we just created: entities.Account retAccount2 = service.Retrieve("account", accountID2, new ColumnSet(true)) as entities.Account; }
public void Example(IOrganizationService service) { /* Notes: * - retrieve is NOT required before updating * - specify only the columns you need to update * - All columns passed will be tracked as changed by CRM events and auditing even if they didn't change * - you can update statuscode but not statecode directly * - Modified By and Modified On are updated by CRM */ // When working with items that you have already retrieved, be careful about the columns: Entity account = new Entity("account"); // pretend that this is not a new entity, but rather one that we pulled from CRM Entity accountInfo = service.Retrieve( account.LogicalName, Guid.NewGuid(), new Microsoft.Xrm.Sdk.Query.ColumnSet(true)); if (!accountInfo.Contains("creditlimit")) { accountInfo["creditlimit"] = new Money(500.00M); service.Update(accountInfo); // HERE: // Unfortunately, ALL columns on the account will be updated (and audited), // INCLUDING any plugins that run on those columns being modified!! } // instead, use: Entity accountInfo2 = service.Retrieve( account.LogicalName, Guid.NewGuid(), new Microsoft.Xrm.Sdk.Query.ColumnSet("accountid", "creditlimit")); // specify exact columns // You CANNOT Update the following: // Primary key // createdon, createdby, modifiedon, modifiedby // (see exception in GettingDataExample) // ownerid, owningteam, owninguser // statecode // ***** SIMPLE EARLY BOUND EXAMPLE ****** entities.Account acc = new entities.Account(); acc.CreditLimit = new Money((decimal)50000); Guid accountID = service.Create(acc); entities.Account retAccount = service.Retrieve("account", accountID, new ColumnSet("creditlimit")) as entities.Account; // Updating the credit limit: retAccount.CreditLimit = new Money((decimal)500); // Update the account (only updates the fields we specified in the ColumnSet when we pulled the account from CRM) service.Update(retAccount); // ***** UPDATE USING A NEW ENTITY ***** entities.Account retAccount2 = service.Retrieve("account", accountID, new ColumnSet("creditlimit")) as entities.Account; // Use the Id of the queried account in the constructor of a new account object: entities.Account updater = new entities.Account() { Id = retAccount2.Id }; // Updating the credit limit: updater.CreditLimit = new Money((decimal)400); // Update the retAccount2 account with our new account: service.Update(updater); // ***** SETTING STATE ***** // Entities have Status (aka statecode) and status reason (aka statuscode) // Generated types include enums for system entity state values // Status and Status Reason can be set using SetStateRequest Entity lateBoundAccount = new Entity("account"); account["name"] = "test"; Guid myAccountId = service.Create(lateBoundAccount); lateBoundAccount = service.Retrieve("account", myAccountId, new ColumnSet("name")); SetStateRequest reqState = new SetStateRequest(); // moniker is now an EntityReference object (moniker means 'name') reqState.EntityMoniker = lateBoundAccount.ToEntityReference(); reqState.State = new OptionSetValue((int)entities.AccountState.Inactive); // status is also inactive reqState.Status = new OptionSetValue(2); var respState = service.Execute(reqState) as SetStateResponse; // ***** ASSIGNING RECORDS ***** // User/Team owned records can have owner assigned/changed // At time of create ownerid defaults to calling user unless set // Use AssignRequest/Response to change owner entities.SystemUser user = new entities.SystemUser(); entities.Account myAccount = new entities.Account(); AssignRequest assignreq = new AssignRequest(); assignreq.Assignee = user.ToEntityReference(); assignreq.Target = myAccount.ToEntityReference(); var assignResp = service.Execute(assignreq) as AssignResponse; }