locked
CRM 2013 Plugin: Setting up new relationship on newly created Accounts RRS feed

  • Question

  • I'm creating a plugin for CRM 2013 (on-premise). My requirement is like this:

    1. When a custom entity "Contract" creates, fire the plugin.
    2. Contract has "1:1" relationship with the Quote entity.
    3. Quote has a 1:N relationship with custom entity 'Property'.
    4. For every properties that Quote has, create new Account records.
    5. 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 = new Entity();
                        accountEntity = new Entity("account");
                        if (record.Attributes.Contains("name"))
                        {
                            accountEntity["name"] = record.Attributes["propertyname"];
                        }
                        else throw new InvalidPluginExecutionException(OperationStatus.Failed, "New Property Name is needed.");
    
                        service.Create(accountEntity);
    
                        var referenceCollection = new EntityReferenceCollection();
                        var relatedEntity = new EntityReference
                        {
                            Id = record.Id,
                            LogicalName = record.LogicalName
                        };
                        referenceCollection.Add(relatedEntity);
                        //The relationship schema name in CRM you are using to associate the entities. 
                        var relRelationship = new Relationship
                        {
                            SchemaName = "new_new_contract_account"
                        };
    
                        service.Associate("account", ContractId, relRelationship, referenceCollection);
    
                    }
    Tuesday, August 25, 2015 6:55 PM

Answers

  • Hi,

    I think your problem is here:

    service.Associate("account", ContractId, relRelationship, referenceCollection);

    if your ReferenceCollection have accounts, so the first parameters are contract reference.

    try this:

    service.Associate("new_contract",ContractId, relRelationship, referenceCollection);

    if referenceCollections are as David said!

    It's make sense for you?

    Let me know the result.

    If my answers help you, please give me a vote.

    Regards,

    Vânia Braziela

    https://vaniabraziela.wordpress.com/

    @vbraziela



    Thursday, August 27, 2015 9:30 AM
  • Thanks for the reply David. I actually fixed my code as you suggested and it is still giving me the same error message 'account With Id = c781cd99-5a4b-e511-80e5-005056971aab Does Not Exist'.

    I'm thinking a workaround to probably pass in the Guid of the Contract to a text field in Account and then associating it in another plugin...

    Just wondering if it works to associate a fresh newly created entity record from the plugin?

    Thanks.


    • Edited by triangular Wednesday, August 26, 2015 2:07 PM
    • Marked as answer by triangular Thursday, August 27, 2015 3:39 PM
    Wednesday, August 26, 2015 1:53 PM

All replies

  • Your call to the Associate method is passing an EntityReferenceCollection that references to original record, and not the newly created account. Try the following:

     var acctId = service.Create(accountEntity);
    
     var referenceCollection = new EntityReferenceCollection();
     var relatedEntity = new EntityReference
     {
      Id = acct.Id,
      LogicalName = accountEntity.LogicalName
     };
    

     

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Wednesday, August 26, 2015 8:41 AM
    Moderator
  • Thanks for the reply David. I actually fixed my code as you suggested and it is still giving me the same error message 'account With Id = c781cd99-5a4b-e511-80e5-005056971aab Does Not Exist'.

    I'm thinking a workaround to probably pass in the Guid of the Contract to a text field in Account and then associating it in another plugin...

    Just wondering if it works to associate a fresh newly created entity record from the plugin?

    Thanks.


    • Edited by triangular Wednesday, August 26, 2015 2:07 PM
    • Marked as answer by triangular Thursday, August 27, 2015 3:39 PM
    Wednesday, August 26, 2015 1:53 PM
  • Hi,

    You confirm that account was created and the Guid are "c781cd99-5a4b-e511-80e5-005056971aab"?



    Wednesday, August 26, 2015 3:03 PM
  • Hi Vania.

    Usually the Account got created successfully if I omit the function that set the relationship.

    However in this case I think it was 'waiting' to set the relationship between Account and Contract, probably the Account was still in the middle of 'creating', then it fails when trying to set the relationship and as a result the entire plugin was rolled back. That's what I think....

    That's why I'm wondering if we can associate a fresh newly created entity record altogether from the plugin? 

    Thanks,

    Wednesday, August 26, 2015 4:19 PM
  • Hi,

    I think your problem is here:

    service.Associate("account", ContractId, relRelationship, referenceCollection);

    if your ReferenceCollection have accounts, so the first parameters are contract reference.

    try this:

    service.Associate("new_contract",ContractId, relRelationship, referenceCollection);

    if referenceCollections are as David said!

    It's make sense for you?

    Let me know the result.

    If my answers help you, please give me a vote.

    Regards,

    Vânia Braziela

    https://vaniabraziela.wordpress.com/

    @vbraziela



    Thursday, August 27, 2015 9:30 AM
  • Thanks so much Vania and David, it's working now!! :)
    Thursday, August 27, 2015 3:39 PM