locked
crm 2011 plugin help. RRS feed

  • Question

  • i created a new custome entity
    custome entity name="new_bayi"
    custome field name ="new_maxindirimorani"

    i wrote below code  as plugin

        public class BayiIskonto : IPlugin

        {

            public void Execute(IServiceProvider serviceProvider)

            {

                IPluginExecutionContext context = Common.GetContext(serviceProvider);

                IOrganizationService _service = Common.GetService(serviceProvider, context);

                Entity entity;

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

                {

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

                    if (entity.LogicalName == "new_bayi")

                    {

                        if (entity.Attributes.Contains("new_maxindirimorani"))

                        {

                            entity.Attributes.Add("new_maxindirimorani", 5);  // temporary  as 5
                        }

                        else

                        {

                            return;

                        }

                    }

                }

                else

                {

                    return;

                }

           }

       }

    i used registration tool

    Primary Entity

    Message

    Stage

    Run in User's Context

    Execution Order

    Execution Mode

    Pipeline

    new_bayi

    Create

    Pre

    Calling User

    1

    Synchronous

    Child

    but when i open the custom bayi page. i could'n see "5" number in "new_maxindirimorani" on "new_bayi" entity

    What I'm doing it wrong


    Friday, November 25, 2011 1:42 PM

Answers

  • Hi Moustafha,

    Lets say when the use click on New button from the Ribbon of your Custom entity and you want to Pre-Populate a value (in your cause 5 into new_maxindirimorani) into any attribute, and the user want to see that value when they see the Custom Entity, kind of Default or Pre-Populated value.

    If you are trying Pre-Populate a value before its gets created in CRM, is not possible through Plugins and what Andriy said above is correct, you can only use Javascript.

     

    However based on above your plugin logic,  the value get saved when the user click on Save button.

     

    Hope this helps.

     

     

     


    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by Thomas Thankachan Monday, November 28, 2011 1:16 AM
    • Marked as answer by Moustafha Monday, November 28, 2011 7:42 AM
    Monday, November 28, 2011 1:16 AM

All replies

  • i created a new custome entity
    custome entity name="new_bayi"
    custome field name ="new_maxindirimorani"

    i wrote below code  as plugin

        public class BayiIskonto : IPlugin

        {

            public void Execute(IServiceProvider serviceProvider)

            {

                IPluginExecutionContext context = Common.GetContext(serviceProvider);

                IOrganizationService _service = Common.GetService(serviceProvider, context);

                Entity entity;

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

                {

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

                    if (entity.LogicalName == "new_bayi")

                    {

                        if (entity.Attributes.Contains("new_maxindirimorani"))

                        {

                            entity.Attributes.Add("new_maxindirimorani", 5);  // temporary  as 5
                        }

                        else

                        {

                            return;

                        }

                    }

                }

                else

                {

                    return;

                }

           }

       }

    i used registration tool

    Primary Entity

    Message

    Stage

    Run in User's Context

    Execution Order

    Execution Mode

    Pipeline

    new_bayi

    Create

    Pre

    Calling User

    1

    Synchronous

    Child

    but when i open the custom bayi page. i could'n see "5" number in "new_maxindirimorani" on "new_bayi" entity

    What I'm doing it wrong


    Friday, November 25, 2011 1:30 PM
  • Hello Moustafha,

    Can you please describe your scenario? What kind of behaviour do you want to get? Your code points to following - in the case user filled your custom field with some value - it would be overwritten with 5. In the case you want to get other kind of behaviour like this field should be filled with 5 when user left it empty you should use following code:

     

        public class BayiIskonto : IPlugin
        {
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = Common.GetContext(serviceProvider);
                IOrganizationService _service = Common.GetService(serviceProvider, context);
                Entity entity;
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName == "new_bayi")
                    {
                        if (!entity.Attributes.Contains("new_maxindirimorani"))
                        {
                            entity.Attributes.Add("new_maxindirimorani", 5);  // temporary  as 5
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                else
                {
                   return;
                }
           }
       }
    

    UPD: Please don't create duplicated threads! Anyway I will merge it.

     


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter
    Friday, November 25, 2011 1:47 PM
    Moderator
  • scenario

    When I open the custome entity(new_bayi) page, I want to see the value "5"  on the page
    Friday, November 25, 2011 3:16 PM
  • Hello Moustafha,

    You again haven't given me the whole scenario. Should it happen when you create record (click New) or after you've saved (open record for edit).

    For the first case - plugin would not be triggered. But you can solve this issue with clientside JavaScript using following code:

     

    if (Xrm.Page.ui.getFormType() == 1)
    Xrm.Page.getAttribute("new_bayi").setValue(5);
    For the second scenario plugin would be triggered and should fill your field with 5.

     


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter
    Friday, November 25, 2011 3:26 PM
    Moderator
  • yes. i know this way.

    but i wanna do it with plugin.  can you  help about pluginn

    Friday, November 25, 2011 3:55 PM
  • yes. i know this way.

    but i wanna do it with plugin.  can you  help about pluginn

    Unfortunately you would not be able.

    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Follow Andriy on Twitter
    Friday, November 25, 2011 4:01 PM
    Moderator
  • Hi
    Are you using following code
    if (entity.Attributes.Contains("new_maxindirimorani"))
     
                        {
     
                            entity.Attributes.Add("new_maxindirimorani", 5);  // temporary  as 5
                         }
     
                        else
     
                        {
     
                            return;
     
                        }
    
    
    or following
    if (!entity.Attributes.Contains("new_maxindirimorani"))
     
                        {
     
                            entity.Attributes.Add("new_maxindirimorani", 5);  // temporary  as 5
                         }
     
                        else
     
                        {
     
                            return;
     
                        }
    
    

    Can you please let us know which code are you using.
     
    there is a difference in if conditions in both code segment
     
     
    I think, if you use if (!entity.Attributes.Contains("new_maxindirimorani"))
     

    then your plugin will work fine... i dont see any issue

    Thank you

    Friday, November 25, 2011 4:05 PM
  • Hi Moustafha,

    Lets say when the use click on New button from the Ribbon of your Custom entity and you want to Pre-Populate a value (in your cause 5 into new_maxindirimorani) into any attribute, and the user want to see that value when they see the Custom Entity, kind of Default or Pre-Populated value.

    If you are trying Pre-Populate a value before its gets created in CRM, is not possible through Plugins and what Andriy said above is correct, you can only use Javascript.

     

    However based on above your plugin logic,  the value get saved when the user click on Save button.

     

    Hope this helps.

     

     

     


    Thomas T(MCBMSS) If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by Thomas Thankachan Monday, November 28, 2011 1:16 AM
    • Marked as answer by Moustafha Monday, November 28, 2011 7:42 AM
    Monday, November 28, 2011 1:16 AM