I'm creating a plugin for CRM 2013 (on-premise). My requirement is like this:
- When a custom entity "Contract" creates, fire the plugin.
- Contract has "1:1" relationship with the Quote entity.
- Quote has a 1:N relationship with custom entity 'Property'.
- For every properties that Quote has, create new Account records.
- Link the newly created Account records to the Contract. The relationship Contract to Account is 1 to N.
I got all working however keep getting problems with #5. For some reason the plugin throws an error that Account ID does not exist.
Here's my code:
foreach("**Property records found in Quote**"){var accountEntity =newEntity();
accountEntity =newEntity("account");if(record.Attributes.Contains("name")){
accountEntity["name"]= record.Attributes["propertyname"];}elsethrownewInvalidPluginExecutionException(OperationStatus.Failed,"New Property Name is needed.");
service.Create(accountEntity);var referenceCollection =newEntityReferenceCollection();var relatedEntity =newEntityReference{Id= record.Id,LogicalName= record.LogicalName};
referenceCollection.Add(relatedEntity);//The relationship schema name in CRM you are using to associate the entities. var relRelationship =newRelationship{SchemaName="new_new_contract_account"};
service.Associate("account",ContractId, relRelationship, referenceCollection);}