none
Plug-in for updating contact

    Question

  • Hi,

    I am very new to crm

    I have two fields in contact entity. Last Appointment Date and Next Appointment Date

    My question is i need a plug-in for if i create a new appointment the two fields should get updated form the newly created appointment entity values.

    Please any one help me. It's Urgent please.

    Provide me the source code for the plug-in.

    Please

     

    Thanks,

    Mathan

    Wednesday, June 16, 2010 12:41 PM

Answers

All replies

  • Hello Mathan,

    You have to write a plugin on the appointment entity (create & update event) and check teh regardingobjectid, if the regardingobjectid == contact, then update the contact entity.

    I think you should be able to write the source code yourself, although it will be a pain on the first attempt but trust me this pain is worth it.

    For Plugin Development

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

    http://mscrmsupport.wordpress.com/2008/04/05/total-no-of-calls-made-to-an-account-plugin-crm-40/


    Muhammad Ali Khan
    http://malikhan.wordpress.com
    Wednesday, June 16, 2010 12:53 PM
  • Hi,

    I tried creating Plug-in.

    I want to update the contact entity with the values in the appointment entity.

    When i try to match the start time date from appointment entity to the last appointment date in contact, the field names are not getting retrived in the coding.

                    // Create object for Contact entity

                    contact con =new contact();

                   // create obj for appointment entity

                     appointment app=new appointment();
                    conobj.lastname = app.requiredattendees.ToString();
                    conobj.new_lastappointmentdate = app.scheduledstart.ToString();

    this new_lastappointmentdate is coming in vs2008 when i type.

    here i have typed manually.

    how to match the start time of appointment entity to the Last Appointment date of the Contact entity.

    Please any one help???

     

     

    Thanks,

    Mathan

    Wednesday, June 16, 2010 1:52 PM
  • Hi, you have to use dynamic entities for custom entities and custom attributes. The attributes can be accessed if you are using MS CRM webservices, But inside plugin you are using the ICrmService Interface and not the webservice so thes custom entities and custom attributes are not available in intellisense and design time.

    So you have use dyanmicEntity to update it, something liket this.

    DynamicEntity entity = new DyanmicEntity("contact");

    KeyProperty prop1 = new KeyProperty("contactid", new Key(guidOfContactId));

    CrmDateTimeProperty prop2 = new CrmDateTimeProperty("new_lastappointmentdate ", new DateTime());

    StringProperty prop3 = new StringProperty("lastname", app.requiredattendees.ToString());

    prop2.Value.Value = app.scheduledstart.ToString();

     

    entity.Properties.Add(prop1);

    entity.Properties.Add(prop2);

    entity.Properties.Add(prop3);

    // Now update the entity.
                TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();

                // Set the properties of the target.
                updateDynamic.Entity = entity;

                //   Create the update request object.
                UpdateRequest update = new UpdateRequest();

                //   Set request properties.
                update.Target = updateDynamic;

                //   Execute the request.
                UpdateResponse updated = (UpdateResponse)service.Execute(update);


    Muhammad Ali Khan
    http://malikhan.wordpress.com
    Wednesday, June 16, 2010 1:58 PM
  • Hi Khan,

    Thanks for your reply,

    But still i am getting trouble in Executing the Request.

    Showing error in that stage

    "Microsoft.crm.sdktypeproxy does not contain definition for Update"

    This is the rror which i am receiving

     

    Thanks,

    Mathan

    Wednesday, June 16, 2010 3:14 PM
  • Hi,

    Could you Paste your code here??


    Mahain
    Wednesday, June 16, 2010 3:19 PM
    Moderator
  • Hi,

    This is the code which i used.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Services;

    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;

    namespace Account__contact
    {
        public class Appointment : IPlugin
        {
            public void Execute(IPluginExecutionContext context)
            {
                DynamicEntity entity = null;

                // Check if the input parameters property bag contains a target
                // of the create operation and that target is of type DynamicEntity.
                if (context.InputParameters.Properties.Contains("Target") &&
                   context.InputParameters.Properties["Target"] is DynamicEntity)
                {
                    // Obtain the target business entity from the input parmameters.
                    entity = (DynamicEntity)context.InputParameters.Properties["Target"];

                    // Verify that the entity represents an account.
                    if (entity.Name != EntityName.appointment.ToString()) { return; }
                }
                else
                {
                    return;
                }
                try
                {
                    DynamicEntity con = new DynamicEntity();
                    con.Name = EntityName.contact.ToString();

                    appointment app = new appointment();

                    con.Properties = new PropertyCollection();
                    con.Properties.Add(new StringProperty("address1_city", app.location.ToString()));


                    // Now update the entity.
                    TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();

                    // Set the properties of the target.
                    updateDynamic.Entity = con;

                    //   Create the update request object.
                    UpdateRequest update = new UpdateRequest();
                    //   Set request properties.
                    update.Target = updateDynamic;

                    //   Execute the request.
                    ICrmService service = context.CreateCrmService(true);
                    UpdateResponse up = (UpdateResponse)service.Update(update);
                    // UpdateResponse updated = (UpdateResponse)service.Update(update);

                }
                catch (System.Web.Services.Protocols.SoapException ex)
                {
                    throw new InvalidPluginExecutionException(
                       "An error occurred in the plug-in.", ex);
                }

                   

                   
                }
            }
        }


    Wednesday, June 16, 2010 3:23 PM
  • Hi, Its Execute() method  actually and  not update

    UpdateResponse updated = (UpdateResponse)service.Execute(update);

    while  the below  one  is wrong.

    UpdateResponse up = (UpdateResponse)service.Update(update); //Update  doesn't  work  with  UpdateRequest  Object


    Muhammad Ali Khan
    http://malikhan.wordpress.com
    Wednesday, June 16, 2010 3:30 PM
  • Hi,

    make below changes to your code

                    con.Properties.Add(new StringProperty("address1_city", app.location.ToString()));

    you should give here value for "address1_city".

     for -->>UpdateResponse up = (UpdateResponse)service.Update(update);
    you need to use

    UpdateResponse up = (UpdateResponse)service.Execute(update);


    Mahain
    Wednesday, June 16, 2010 3:38 PM
    Moderator
  • Hi,

    I made the changes .

    I want that "addredd1_city" value to be retrieved from the location field in the appointment entity when new appointment is created

    will it take like that???

     

     

    Thanks,

    Mathan

    Wednesday, June 16, 2010 3:49 PM
  • No, You  just created an appointment class  object, with location  is empty

    appointment app = new appointment();

                    con.Properties = new PropertyCollection();
                    con.Properties.Add(new StringProperty("address1_city", app.location.ToString()));

    you have to  revterive the appointment  entity from the context properties object. use this code instead

    DynamicEntity  app = (DynamicEntity)context.Properties[ParameterName.Target];

    string location = (string)app.properties["location"];

     con.Properties.Add(new StringProperty("address1_city", location);

    Note the  above code  will work if you change  the location  on the appointment  form,  otherwise it will be null and an exception will be thrown from the below  line.

    string location = (string)app.properties["location"];

    In  order to handle  this senario,  you  can  get  the activity id and reterive the location from  the crmservice.Reterive() method,  like this.

    Guid activityId = ((Key)app.Propertes["activityid"]).Value;

    appointment appObj = (appointment)crmService.Reterive(EntityName.appointment.ToString(), actviityId, new ColumnSet(new string[]{"location"});

    string location  = appObj.location;


    Muhammad Ali Khan
    http://malikhan.wordpress.com
    Wednesday, June 16, 2010 4:12 PM
  • Hi,

    do i need to add any namespace for using this retrieve method???

    if i include  the above coding showing error???

    Thursday, June 17, 2010 5:53 AM
  • These two namespace are enough

    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;

    Also it is Retrieve  & not Reterive (my mistake).


    Muhammad Ali Khan
    http://malikhan.wordpress.com
    Thursday, June 17, 2010 6:03 AM
  • i have already added those namespace

    k fine i changed that...

    appointment appObj = (appointment)crmService.Reterive(EntityName.appointment.ToString(), actviityId, new ColumnSet(new string[]{"location"});

    In the above line is it ColumnSet or ColumnMapping??

    I am getting error in that too???


    Thursday, June 17, 2010 6:13 AM
  • It is  ColumnSet(), by the way,  you  can  always  right click the mouse on  your identifier name and click Resolve.

    or  you can type intial characters in visual studio and it iwll give u the intellisense.

     


    Muhammad Ali Khan
    http://malikhan.wordpress.com
    Thursday, June 17, 2010 6:25 AM
  • I did that also still having trouble in the same thing..

    This is the coding which i have modified and i have at present.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Services;

    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;

    namespace AutoCreate
    {
        public class Appointment : IPlugin
        {
            public void Execute(IPluginExecutionContext context)
            {
                DynamicEntity entity = null;

                // Check if the input parameters property bag contains a target
                // of the create operation and that target is of type DynamicEntity.
                if (context.InputParameters.Properties.Contains("Target") &&
                   context.InputParameters.Properties["Target"] is DynamicEntity)
                {
                    // Obtain the target business entity from the input parmameters.
                    entity = (DynamicEntity)context.InputParameters.Properties["Target"];

                    // Verify that the entity represents an account.
                    if (entity.Name != EntityName.appointment.ToString()) { return; }
                }
                else
                {
                    return;
                }
                try
                {
                    DynamicEntity con = new DynamicEntity();
                    con.Name = EntityName.contact.ToString();

                    DynamicEntity app = new DynamicEntity();
                    app.Name = EntityName.appointment.ToString();
                    app.Properties = new PropertyCollection();

                   // string location = (string)app.Properties["location"];

                    Guid activityId = ((Key)app.Properties["activityid"]).Value;
                   
             

    appointment appObj = (appointment)CrmService.Retrieve(EntityName.appointment.ToString(), activityId, new ColumnSet(new string[]{"location"}));

    string location  = appObj.location;
                   
                    app.Properties.Add(new StringProperty("address1_city",location));

                    //appointment app = new appointment();
                    //app.location = "Chennai";
                    //con.Properties = new PropertyCollection();
                    //con.Properties.Add(new StringProperty("address1_city", app.location.ToString()));
                   

                    // Now update the entity.
                    TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic();

                    // Set the properties of the target.
                    updateDynamic.Entity = con;

                    //   Create the update request object.
                    UpdateRequest update = new UpdateRequest();
                    //   Set request properties.
                    update.Target = updateDynamic;

                    //   Execute the request.
                    ICrmService service = context.CreateCrmService(true);
                    UpdateResponse up = (UpdateResponse)service.Execute(update);
                    // UpdateResponse updated = (UpdateResponse)service.Update(update);

                }
                catch (System.Web.Services.Protocols.SoapException ex)
                {
                    throw new InvalidPluginExecutionException(
                       "An error occurred in the plug-in.", ex);
                }

                   

                   
                }
            }
        }


    Please make the changes that should be done in the coding and send me.. please.


    My requirement is if i create an appointment in that i will select one contact that related contact which i selected in appointment  should get updated ..


    the values in start time and end time fields in appointment  should be copied to last appointment date and next appointment date in contact entity.

    please help me..

     

    Thursday, June 17, 2010 6:40 AM
  • Hi Raj,
    On what action do you want to populate these fields in Contact entity? You can easily create a workflow on Appointment record creation which updates the fields on Regarding(Contact).
    Similarly, this can be achieved by a Plug-in, though I would recommend workflow here, as you wont need to write any code and is a very basic requirement.

    Do let me know if you need any further help.

    Best regards,
    Ashish Kapoor


    Ashish Kapoor || ashish.1982@gmail.com http://msdynamicscrmworld.blogspot.com/
    Thursday, June 17, 2010 7:11 AM
  • Hi Kapoor,

    I have done this in work flow and i have got the output correctly already.

    I want this in Plug-in.

    If i create a new appointment the related contact that i select in appointment in regarding field  should be updated.

    Start Time values To Last Appointment Date Field

    End Time Value To Next Appointment Date Filed in Contact

    I have used the above coding but no fruits.

    Please help me.

     

    Thanks,

    Mathan

    Thursday, June 17, 2010 7:36 AM
  • Hi Muhammad Ali Khan,

    As u said i have added those codings, but still i have trouble in that ColumnSet().

    I also right clicked and added resolve then too the problem continues in that place..

    Please help me????

     

     

    Thanks,

    Mathan

     

     

    Thursday, June 17, 2010 9:19 AM
  • Please any one help me in this.

    i have posted my code and requirement above.

     

    Thanks,

    Mathan

    Thursday, June 17, 2010 9:23 AM
  • Hi Mathan,

    Are you getting error ??

    Also this plugin is not complete yet, you have to fetch scheduledstart,scheduledend with location to set in your contact entity.

    if you are getting error please try to debug your plugin (As you are new to CRM Development you will face some issues,but it's natural and it will really help you in future).

    refer below link to debug plugin.

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

    let us know what error are you getting

    Hope it will help you !!!


    Mahain
    Thursday, June 17, 2010 9:53 AM
    Moderator
  • Hi Mahain,

    appointment appObj = (appointment)CrmService.Retrieve(EntityName.appointment.ToString(), activityId, new ColumnSet(new string[]{"location"}));

    In the above line i an getting error as  AnError    1    The type or namespace name 'ColumnSet' could not be found (are you missing a using directive or an assembly reference?)  

    I right clicked the ColumnSet and clicked Resolve and added the available NameSpace

    Then too i an receiving the error.

    I know the Plug-in is not complete.

    first i am trying for simple like just only for location.

     

    Thanks,

    Mathan

    Thursday, June 17, 2010 10:15 AM
  • Did you added reference for Microsoft.Crm.Sdk.Query ???

     


    Mahain
    Thursday, June 17, 2010 10:22 AM
    Moderator
  • ya i added that also and tried.

    If i add that and build means i receive the following error.

     

    Error    1    An object reference is required for the non-static field, method, or property 'Microsoft.Crm.SdkTypeProxy.CrmService.Retrieve(string, System.Guid, Microsoft.Crm.Sdk.Query.ColumnSetBase)'  

    Thursday, June 17, 2010 10:28 AM
  • Also you are using

    appointment appObj = (appointment)CrmService.Retrieve(EntityName.appointment.ToString(), activityId, new ColumnSet(new string[]{"location"}));

    but you have not created this object. so try to use

     ICrmService service = context.CreateCrmService(true);
     appointment appObj = (appointment)service .Retrieve(EntityName.appointment.ToString(), activityId, new ColumnSet(new string[]{"location"}));


    Mahain
    Thursday, June 17, 2010 10:30 AM
    Moderator
  • Hi Mahain,

    Thanks for your reply.Now no error comes.

    But if i create an appointment and specify the location that location is not getting copied to City field in contact entity.

    I have selected the contact too in the appointment which i created. but it's not getting fired.

     

    Thanks,

    Mathan

    Thursday, June 17, 2010 10:44 AM
  • Are you sure you registered this plugin on PostCreate ??
    Mahain
    Thursday, June 17, 2010 10:50 AM
    Moderator
  • Ya i am sure i did the registration correctly.

    Is it Synchronous or Asynchronous???

    Thursday, June 17, 2010 10:55 AM
  • It Should be Synchronous,

    Also did you follow all steps to create plugin (Digitally sign the plug-in)

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

    Also if you are not getting city value then debug your plugin.

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


    Mahain
    Thursday, June 17, 2010 11:02 AM
    Moderator
  • I tried every thing.

    But it's still not firing.

    My Plug-in is not getting debugged.

    I keeping BreakPoint in the Execute Method, attaching process tow3wp.exe and performed the action in CRM server.

    But the action is going well but not getting debugged.

    I have used one plug-in before Account create plug-in. In that too i tried to debug but i did not get that.But i got the answer for that plug-in.

    In this Plug-in i am not getting anything.

     

    Thanks,

    Mathan

    Thursday, June 17, 2010 11:15 AM
  • Did you follow these steps (The compiled plug-in assembly's .pdb file must be copied to the server's <crm-root>\Server\bin\assembly folder and IIS must then be restarted. After debugging has been completed, you must remove the .pdb file and reset IIS to prevent the w3wp.exe process from consuming additional memory)

    Aslo try to reset IIS and then check


    Mahain
    Thursday, June 17, 2010 11:24 AM
    Moderator
  • Did you follow these steps (The compiled plug-in assembly's .pdb file must be copied to the server's <crm-root>\Server\bin\assembly folder and IIS must then be restarted. After debugging has been completed, you must remove the .pdb file and reset IIS to prevent the w3wp.exe process from consuming additional memory)

    Aslo try to reset IIS and then check


    Mahain

    I tried that too no use.. nothing happens.
    Thursday, June 17, 2010 11:37 AM
  • I tried that too, but nothing happens.

    No error too showing.

    But the value is not generating.

    Thursday, June 17, 2010 11:48 AM
  • Hi Mahain,

    Should i register the step in the Child Pipeline??

    Thursday, June 17, 2010 12:45 PM
  • No,

    It will be on parent pipeline only, Did you try to debug after registering plugin on Disk rather then Database ???


    Mahain
    Thursday, June 17, 2010 12:47 PM
    Moderator
  • I am not using any database.

    while registering the plug-in the assembly file should be placed in the root folder of crm server only.

    so how can i register from the other location.

    If i do so it will show error only. also i have tried that in before plug-in itself.

     

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

    Mathan

    Thursday, June 17, 2010 12:54 PM
  • I am talking about

    1. Step 3 - Select where the assembly should be stored for execution
      1. Database - Storing the files on the database allows users to update the assembly file through this wizard as many times as they want without having to perform an "iisreset," as you had to in CRM 3.0.  Advantage #2 is now the server\bin\assembly folder will not become cluttered if you have numerous  plug-ins.  Finally, this makes migration from different environments much easier because there are less files to move.

     **NOTE: If you want to debug the assembly files, you will need to place the symbols file (.pdb) in <crm installation directory>\Server\bin\assembly folder. **

    1. Disk - This option allows you to store the assembly file as you did in CRM 3.0 under the <crm installation directory>\Server\bin\assembly directory.  For debugging purposes, the symbols file (.pdb) must also be in this directory.  If you store files here, an "iisreset" will be needed anytime any changes are made to the assembly.
    2. GAC - (Global Assembly Cache), for more information on this, please see my other post: http://www.crowehorwath.com/cs/blogs/crm/archive/2008/03/05/gac-it-developing-portable-code-for-microsoft-crm.aspx

    Source  http://www.crowehorwath.com/cs/blogs/crm/archive/2009/02/21/crm-4-0-plug-in-registration-tool-walkthrough.aspx


    Mahain
    Thursday, June 17, 2010 1:09 PM
    Moderator
  • ya i understand now.

    Still no result..

    Thursday, June 17, 2010 1:22 PM
  • Mathan, surely you are missing some step, I will suggest you to go through the CRM plugin debugging checklist

    refer below post http://mayankp.wordpress.com/2010/05/04/crm-plug-in-custom-workflow-debugging-check-list/ 


    Mahain
    Thursday, June 17, 2010 1:28 PM
    Moderator
  • Hi Folks,

    Here is my question:

    I have created three custom entities, the Custom entity2 is the primary entity which the updating has to occure. The CE2 has 1:N relationship with CE1 and CE3. In CE2 I have an attribute by the name attrib_BalanceAmount. * If I update the attrib_BalanceAmount value in CE2, then the related entities CE1 / CE3's Total Amount should update automatically.

    *please share your information/code to update the attributes in all custome entities.

    Thanks,

    Israel

    Monday, April 11, 2011 5:13 PM
  • Hi Muhammad,

    Here is my question:

    I have created three custom entities, the Custom entity2 is the primary entity which the updating has to occure. The CE2 has 1:N relationship with CE1 and CE3. In CE2 I have an attribute by the name attrib_BalanceAmount. * If I update the attrib_BalanceAmount value in CE2, then the related entities CE1 / CE3's Total Amount should update automatically.

    *please assist me with your code to update the attributes in all custome entities.

    Thanks,

    Israel


    Thanks, Israel Pradeep, Software Developer & Entrepreneur, If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"
    • Proposed as answer by Kumar G Friday, April 22, 2011 8:09 PM
    Wednesday, April 20, 2011 6:19 PM