locked
Plugin query RRS feed

  • Question

  • Hi All,

    I want to generate the unique id using plugin and concatenate it with the location  code. this location code is coming from another entity which has relation. I wrote the code for generating series number , but now I want your help how to get the value of the related entity field . please find the below code and give me the code which suits the above requirement.

    using

    System;


    using

    System.Collections.Generic;


    using

    System.Linq;


    using

    System.Text;


    using

    System.Threading.Tasks;


    using

    Microsoft.Xrm.Sdk;


    using

    Microsoft.Xrm.Sdk.Query;



    namespace

    autonumbergen

    {

       

    publicclassClass1: IPlugin


        {

           

    publicvoidExecute(IServiceProviderserviceProvider)

            {

               

    IPluginExecutionContextcontext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

               

    IOrganizationServiceFactoryserviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

               

    IOrganizationServiceservice = serviceFactory.CreateOrganizationService(context.UserId);

               

    //  IOrganizationService ServiceProxy = ServiceProxy


               

    if(context.InputParameters.Contains("Target") && context.InputParameters["Target"] isEntity)

                {

                   

    Entityentity = (Entity)context.InputParameters["Target"];

                   

    QueryExpressionquery = newQueryExpression("msjps_incidentreport");

                    query.ColumnSet =

    newColumnSet(true);

                   

    EntityCollectionresult = service.RetrieveMultiple(query);

     

                   

    inttotalRecords = result.Entities.Count;

                   

    //string final = totalRecords.ToString();


                   

    //string total = Convert.ToString(totalRecords);


                   

    intfinal = totalRecords + 1;

                    entity.Attributes[

    "msjps_trackingnumber"] = Convert.ToString(final);

                }

            }

        }

    }

    Wednesday, July 9, 2014 5:30 AM

Answers

  • Use this simple statement

    service.Update(entity);

    In your case assign the unique value to your attribute, and then call the service

    It will be some thing like this

    entity.Attributes["msjps_trackingnumber"] = Convert.ToString(final);
    service.Update(entity);

    Good Luck!

    Mark it as answer if it helps.



    Sreeni Pavalla

    Wednesday, July 9, 2014 11:17 AM

All replies

  • https://community.dynamics.com/crm/b/microsoftdynamicscrmandstuff/archive/2012/08/31/microsoft-dynamics-crm-2011-plugin-based-auto-numbering.aspx

    http://advcrm2011autonumber.codeplex.com/


    ms crm

    Wednesday, July 9, 2014 5:48 AM
  • Hi,

    Try the below

    Entityentity = (Entity)context.InputParameters["Target"];
    
                   
    
    QueryExpressionquery = newQueryExpression("msjps_incidentreport");
    
                    query.ColumnSet =
    
    newColumnSet(true);
    
    Relationship relationship = new Relationship();
    
    
    			// name of relationship between incidentreport& your entity
                        relationship.SchemaName = "incidentreport_customer_yourentity";
     
                        //create relationshipQueryCollection Object
                        RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
     
                        //Add the your relation and query to the RelationshipQueryCollection
                        relatedEntity.Add(relationship, query);
     
                        //create the retrieve request object
                        RetrieveRequest request = new RetrieveRequest();
     
                        //add the relatedentities query
                        request.RelatedEntitiesQuery = relatedEntity;
     
                        //set column to  and the condition for the your entity
                        request.ColumnSet = new ColumnSet("yourentityid");
                        request.Target = new EntityReference { Id = yourentityID, LogicalName = "yourentity" };
     
                        //execute the request
                        RetrieveResponse response = (RetrieveResponse)_orgserviceproxy.Execute(request);

    Replace the enitty you are loking for "youEntity" and retrive the attributes you need and then create your unique id.

    Hope this helps!


    Sreeni Pavalla

    Wednesday, July 9, 2014 5:59 AM
  • Hi Sreeni,

    the above code should be mixed up with my code...

    I request u to please provide me the full code.

    Thank you

    Wednesday, July 9, 2014 7:09 AM
  • Hi,

    The below link can help you to get an idea to retrieve entity with related.

    https://community.dynamics.com/crm/b/crminogic/archive/2011/09/07/retrieve-related-entity-records-along-wih-the-primary-entity-using-retrieve-method.aspx

    Thank you,


    Sreeni Pavalla

    Wednesday, July 9, 2014 9:18 AM
  • I got it , the code is working fine for creation of record, now I want to work it for update of the record also . how can I do this ?

    Wednesday, July 9, 2014 11:05 AM
  • Use this simple statement

    service.Update(entity);

    In your case assign the unique value to your attribute, and then call the service

    It will be some thing like this

    entity.Attributes["msjps_trackingnumber"] = Convert.ToString(final);
    service.Update(entity);

    Good Luck!

    Mark it as answer if it helps.



    Sreeni Pavalla

    Wednesday, July 9, 2014 11:17 AM