web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Plugin (Fetching value of diffrent entity in my current entity). Creating plugin for that

(0) ShareShare
ReportReport
Posted on by 547

I am working on a crm plugin MS crm 2011.

What I need is : To get value of 3 fields from let's say "Counter" entity and setting it in my current entity let's say "Testingcrm"

when I save few of the details on my "Testingcrm" entity in front end I need to populate few of my fields in "Testingcrm" to be updated with values from "Counter" entity.

I have created a plugin as CREATE and PRE OPERATION.

Any help will be much appreciated!!!

-----------------------------------------

// TODO: Implement your custom Plug-in business logic.
// Obtain the execution context from the service provider.
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;

// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "new_testingcrm1")
{
try
{
//int x = ((OptionSetValue)entity["abaxis_speices"]).Value;
String x = entity.FormattedValues["abaxis_spieces"].ToString();

Guid counter = ((EntityReference)entity["abaxis_prefixcounter"]).Id;
//Entity member = service.Retrieve("prefixcounter",counter,new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
//var x1 = member.Attributes["abaxis_prefix1"];
entity.Attributes["new_medid"] = counter;
/*if (x.Contains("MED"))
{

entity.Attributes["new_medid"] = "x1";
}
else
{
entity.Attributes["new_medid"] = "x1";
}*/

}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Looks like you already have a lookup of abaxis_prefixcounter on new_testingcrm1 entity and you are trying to save the guid of that lookup into new_medid field. My first question is, why are you populating the GUID of the record which you already have as a lookup on the same record. Now is the guid not getting populated or are you getting an error?

  • chikhaleankush Profile Picture
    547 on at

    Hey Vikas Thanks for your reply again on my post.

    If you remember I asked before about Auto number generation and this is just one part of the solution I am working on.

    Particular to this post, No I don't have any lookup field. What I need is to fetch values from my Prefix counter entity the value of Prefix field and counter field and increment counter value and add to one of my field.

  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    I think you are asking how to retrieve an entity by field other than it's primary key?

    If so you can use the QueryByAttribute class in combination with RetreiveMultiple - see msdn.microsoft.com/.../gg334708.aspx

    Hope this helps

  • chikhaleankush Profile Picture
    547 on at

           protected void ExecutePretestingCrm1Create(LocalPluginContext localContext)

           {

               if (localContext == null)

               {

                   throw new ArgumentNullException("localContext");

               }

               // TODO: Implement your custom Plug-in business logic.

               // TODO: Implement your custom Plug-in business logic.

               // Obtain the execution context from the service provider.

               IPluginExecutionContext context = localContext.PluginExecutionContext;

               IOrganizationService service = localContext.OrganizationService;

               // The InputParameters collection contains all the data passed in the message request.

               if (context.InputParameters.Contains("Target") &&

              context.InputParameters["Target"] is Entity)

               {

                   // Obtain the target entity from the input parmameters.

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

                   if (entity.LogicalName == "new_testingcrm1")

                   {

                       try

                       {

                           Microsoft.Xrm.Sdk.Query.QueryByAttribute querybyattribute = new Microsoft.Xrm.Sdk.Query.QueryByAttribute("abaxis_prefixcounter");

                           querybyattribute.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("abaxis_prefix1", "abaxis_prefix2", "abaxis_counter");

                           EntityCollection retrieved = service.RetrieveMultiple(querybyattribute);

                           //int x = ((OptionSetValue)entity["abaxis_speices"]).Value;

                           String x = entity.FormattedValues["abaxis_spieces"].ToString();

                           //var counter = "DC3F0C08-E477-E511-849F-005056830006";

                        //   Guid myid = new Guid("DC3F0C08-E477-E511-849F-005056830006");

                           //var contract = ((Contract).context.InputParameters["Target"]);

                           //Guid counter = (Guid)entity.Attributes["abaxis_prefixcounter"];

                           //Entity newlyCreated = service.Retrieve("Prefix counter", counter, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

                           //Guid counter = ((Guid)entity.Attributes["abaxis_prefixcounter"]);

                           //Entity member = service.Retrieve("prefixcounter",myid,new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

                           var x1 ="";

                           foreach (var c in retrieved.Entities)

                           {

                               x1 = c.Attributes["abaxis_prefix1"].ToString();

                           }

                           //entity.Attributes["new_medid"] = counter;

                           if (x.Contains("MED"))

                           {

                               entity.Attributes["new_medid"] = x1;

                           }

                           else

                           {

                               entity.Attributes["new_medid"] = x1;

                           }

                       }

                       catch (FaultException ex)

                       {

                           throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);

                       }

                   }

               }

           }

       }

    }

    ---------------------------------------

    above is the code which i tried to work on also if you can see i tried to work with Guid too. My problem is how to get the value of another entity in any object and once I get it in object then to retrive it.

    Thanks

  • ScottDurow Profile Picture
    21 on at

    Are you getting an error?

  • chikhaleankush Profile Picture
    547 on at

    Not when building and deploying plugin. But when I run it on entity it gives error. It says key not yet available.

    I am running my plugin on cretae and pre operation. Is that the cause.

  • Suggested answer
    Community Member Profile Picture
    on at

    The key error you have specified was this one? "The given key was not present in the dictionary" ? If yes then it means the field that you were trying to select did not have any value. SO before using that field you must check if the entity object contains that field or not.

  • Verified answer
    chikhaleankush Profile Picture
    547 on at

    Thank you very much @Vikas and @Scott.

    I could sucessfully fetch entity and it's value. Here is my working code.

    -----------------

    Guid counter = new Guid("98d4db70-ab7c-e511-849f-005056830006");

                           Entity member = service.Retrieve("abaxis_prefixcounter",counter,new Microsoft.Xrm.Sdk.Query.ColumnSet(true));

                           var prefix1 = member.Attributes["abaxis_prefix1"];

                           var prefix2 = member.Attributes["abaxis_prefix2"];

                           var medcounter = member.Attributes["abaxis_medcounter"];

                           var vetcounter = member.Attributes["abaxis_vetcounter"];

    ------------------------

    I could also update the entity from which I fetched the records.

  • Community Member Profile Picture
    on at

    Hi Ankush, i have exactly same requirement to generate custom auto number for case. i would need your help with the code.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans