locked
Issue while creating a Team using Dynamics CRM Plugins RRS feed

  • Question

  • Hi,

    I am developing a plugin to create a team dynamically. I am getting some issue related to the business unit id.

    Below are the code.

    var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    
    
    //Create the organization service
    IOrganizationService service = factory.CreateOrganizationService(context.UserId);
    
    ColumnSet allFields = new ColumnSet() { AllColumns = true };
    Entity user = service.Retrieve("systemuser", context.UserId, allFields);
    
    //get business unit lookup
    Guid businessUnitId = ((EntityReference)user.Attributes["businessunitid"]).Id;
    
    
    //create a team
    Entity team = new Entity("team");
    team["name"] = "Test Team";
    team["businessunitid"] = businessUnitId;
    team["administratorid"] = context.UserId;
                    
    // Create the team in Microsoft Dynamics CRM.
    service.Create(team);
    
    

    I am getting an error "Attribute: businessunitid cannot be set to NULL" on "service.Create(team)" line. I debug the code and found that I am getting Business Unit ID correctly.

    Can anyone help me to fix this.

    Thanks and Regards

    Sandeep

    CloudFronts Technologies

    www.cloudfronts.com

    Tuesday, May 14, 2013 6:19 AM

Answers

  • debug the code and check the bussnessUnitId has guid or not

    or

    for guid type you need to use

    team["businessunitid"]= new EntityReference("entityname",bussnessUnitId );


    ms crm

    Tuesday, May 14, 2013 6:39 AM
  • Hi,
    you need to set the values as EntityReference, not only the Guid.

    //create a team
    
    Entity team = new Entity("team");
    team["name"] = "Test Team";
    team["businessunitid"] = new EntityReference("businessunit", businessUnitId);
    team["administratorid"] = new EntityReference("systemuser", context.UserId);
    
    // Create the team in Microsoft Dynamics CRM.
    service.Create(team);



    My blog: www.crmanswers.net

    Tuesday, May 14, 2013 6:50 AM

All replies

  • debug the code and check the bussnessUnitId has guid or not

    or

    for guid type you need to use

    team["businessunitid"]= new EntityReference("entityname",bussnessUnitId );


    ms crm

    Tuesday, May 14, 2013 6:39 AM
  • Hi,
    you need to set the values as EntityReference, not only the Guid.

    //create a team
    
    Entity team = new Entity("team");
    team["name"] = "Test Team";
    team["businessunitid"] = new EntityReference("businessunit", businessUnitId);
    team["administratorid"] = new EntityReference("systemuser", context.UserId);
    
    // Create the team in Microsoft Dynamics CRM.
    service.Create(team);



    My blog: www.crmanswers.net

    Tuesday, May 14, 2013 6:50 AM
  • Hi,

         BusinessUnitid is lookup so you need to write following code:

     BusinessUnitId = new EntityReference
                    {
                        LogicalName = BusinessUnit.EntityLogicalName,
                        Name = BusinessUnit.EntityLogicalName,
                        Id = businessUnitId
                    }
    

    Hope this helps.
     
    -----------------------------------------------------------------------
     Minal Dahiya
     
    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"
    • Proposed as answer by Minal Dahiya Tuesday, May 14, 2013 6:54 AM
    Tuesday, May 14, 2013 6:54 AM
  • Hello,

    Check SDK you will get sample code to work with team and users

    refer: http://msdn.microsoft.com/en-us/library/gg327897.aspx


    My Website | My Blog | Follow me on Twitter | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Tuesday, May 14, 2013 7:00 AM
    Moderator
  • Thank guys for your support.

    Thanks and Regards

    Sandeep

    CloudFronts Technologies

    www.cloudfronts.com

    Tuesday, May 14, 2013 7:21 AM