locked
How to call CrmService Update method in Plugin RRS feed

  • Question

  • Hi,

    I need to retrieve the contactid from lookup field,Crmdate time from datetime field in Appointment entity and update the crm date time field in Contact entity with this contactid.

    How can i do this in Plugin, Please provide me some C# sample code.

    Thanks,

    Naveen

    Friday, June 25, 2010 4:02 AM

Answers

  • Hi Naveen,

    you can use below code

    //Create icrmservice object

    //This example is to take location from appointment entity and update city in contact entity

     ICrmService _Service = context.CreateCrmService(true);
                  CrmDateTime dt=new CrmDateTime();
                    string loc=string.Empty;
                     Guid _contactid = Guid.Empty;
                   
                    ICrmService _Service = context.CreateCrmService(true);
                    if (context.OutputParameters.Contains("id"))
                    {
                        _Apmt =(Guid)context.OutputParameters.Properties["id"];
                    }

              if (_Entity.Properties.Contains("location"))
                    {
                        loc = _Entity.Properties["location"].ToString();
                    }
                   //for example you want to pick scheduledend date
                    if (_Entity.Properties.Contains("scheduledend"))
                    {
                       dt.Value =((CrmDateTime)_Entity.Properties["scheduledend"]).Value;

                    }
    //catch regarding id
     if (_Entity.Properties.Contains("regardingobjectid"))
                    {
                        if (((Lookup)_Entity.Properties["regardingobjectid"]).type == "contact")
                            _contactid = ((Lookup)_Entity.Properties["regardingobjectid"]).Value;
                        else
                            return;
                    }

                                                                  
                    DynamicEntity _Contact = new DynamicEntity();
                    _Contact.Name = EntityName.contact.ToString();
                    _Contact.Properties.Add(new KeyProperty("contactid", new Key(_contactid)));

    //you can add dataproperty also

    //_Contact.Properties.Add(new CrmDateTimeProperty("DateFieldName", dt));
                     _Contact.Properties.Add(new StringProperty("address1_city", loc));
                    TargetUpdateDynamic update = new TargetUpdateDynamic();
                    update.Entity = _Contact;
                    UpdateRequest req = new UpdateRequest();
                    req.Target = update;
                    UpdateResponse res = (UpdateResponse)_Service.Execute(req);

     

    Let me know


    Mahain
    Friday, June 25, 2010 5:36 AM
    Moderator