locked
What is the best way to change the regardingobjectid in an incoming email programatically? RRS feed

  • Question

  • Any wonderful ideas will be greatly appreciated.

    So heres what i wanted to do.
    1.  Intercept incoming emails
    2.  Read the standardized subject line for the keywords im looking for
    3.  Search for this keyword in the specific CRM entity
    4.  Programmatically set the regarding object if it is found

    I can do 1, 2, and 3 easily - but having issues how to implement number 4..
    Any ideas guys? 
    I am implementing this through a plugin in the deliverincomingemail message.
    I know there are 3 relevant classes in the message and entity is one of the classes.
    Can anyone give out an example of how to do this?

    Many thanks

    Friday, April 30, 2010 3:38 AM

Answers

  • Hi,

    I tried retrieving value in a child pipeline and i was able to do so.

    Please try with the following simple code on your system which retrieves account name and number for a given account record.

          CrmService crmService = CreateCrmService(context, true);
          ColumnSet myCols=new ColumnSet();
          myCols.AddColumn("name");
          myCols.AddColumn("accountnumber");
          account myAccount=(account)crmService.Retrieve(EntityName.account.ToString(), new Guid("yourAccountRecordGuid"), myCols);

     

    CreateCrmService method

     

        /// <summary>
        /// Creates a CrmService proxy for plug-ins that execute in the child pipeline.
        /// </summary>
        /// <param name="context">The execution context that was passed to the plug-ins Execute method.</param>
        /// <param name="flag">Set to True to use impersontation.</param>
        /// <returns>A CrmServce instance.</returns>
        private static CrmService CreateCrmService(IPluginExecutionContext context, Boolean flag)
        {
          var authToken = new CrmAuthenticationToken { AuthenticationType = 0, OrganizationName = context.OrganizationName, CallerId = (flag ? context.UserId : context.InitiatingUserId) };
    
          var corToken = new CorrelationToken { CorrelationId = context.CorrelationId, CorrelationUpdatedTime = context.CorrelationUpdatedTime, Depth = context.Depth };
    
          var regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM", false);
    
          var service = new CrmService
          {
            CrmAuthenticationTokenValue = authToken,
            UseDefaultCredentials = true,
            Url = String.Concat(regkey.GetValue("ServerUrl").ToString(), "/2007/crmservice.asmx"),
            CorrelationTokenValue = corToken
          };
    
          return service;
        }

     

    Regards,

    Nishant Rana

     

     

     

     


    http://nishantrana.wordpress.com
    Tuesday, May 4, 2010 7:06 AM
  • Debug through your plugin or enable tracing and see if you can get a more informed error message.
    MSCRM Bing'd - http://bingsoft.wordpress.com
    Tuesday, May 4, 2010 7:14 AM
    Moderator

All replies

  • Hi,

    To set the regarding object field you could use a code similar to the one given below

          email mail = new email();
          // pass the guid of the email as key
          mail.activityid = new Key();
          mail.activityid.Value = new Guid("guidOfEmail");
    
          // set type, name and value for the regarding object field
          mail.regardingobjectid = new Lookup();
          mail.regardingobjectid.type = EntityName.account.ToString();
          mail.regardingobjectid.name = "NameOfTheAccount";
          mail.regardingobjectid.Value = new Guid("AccountGuid");
    
          service.Update(mail);

    Regards,

    Nishant Rana

     


    http://nishantrana.wordpress.com
    Friday, April 30, 2010 5:24 AM
  • Thanks for the reply Nishant.

    My code is being triggered in the pre-stage, does the above code work? since it is already doing an update to an email that is just about to be created.

    Friday, April 30, 2010 5:49 AM
  • Hi,

    Normally for Update and Create in Pre stage we directly manipulate the input parameter's target properties.

    However it seems there are no target inputparameter parameter for Deliver Incoming message.

    So you could try updating the record using CrmService. 

     

    Regards,

    Nishant Rana

     


    http://nishantrana.wordpress.com
    Friday, April 30, 2010 6:49 AM
  • You can use a plugin on the child pipeline Pre Stage Create message of an email as DeliverIncoming triggers this message. Then just do as Nishant mentions and update the InputParameters.
    MSCRM Bing'd - http://bingsoft.wordpress.com
    Friday, April 30, 2010 8:21 AM
    Moderator
  • Thanks Rhett.

    Will give that a try and will let all know.
    Friday, April 30, 2010 12:36 PM
  • You can use a plugin on the child pipeline Pre Stage Create message of an email as DeliverIncoming triggers this message. Then just do as Nishant mentions and update the InputParameters.
    MSCRM Bing'd - http://bingsoft.wordpress.com

    Hi Rhett.

    Quick question as maybe you know this right off.  Im just trying your recommendation today and got into thinking -
    First off, in the pre Create message - there is not an activityId that I can use for an update.  It is however in the post Create as an output parameter.
    Question is, does the DeliverIncoming trigger the post Create message?

    Thanks

    Monday, May 3, 2010 1:50 AM
  • It should do in the child pipeline, though you can just get the Target property (Dynamic Entity) from the InputParameters, insert your regarding value and then set the InputParameter Target with the updated Dynamic Entity.
    MSCRM Bing'd - http://bingsoft.wordpress.com
    Monday, May 3, 2010 9:53 AM
    Moderator
  • The problem is that I needed to query the database when I am inside the plugin.
    Somehow I am struggling so much on the instantiation of a working crmService.

    I am able to instantiate a crmService alright using the following :

     private CrmService CreateCrmService(IPluginExecutionContext context, Boolean flag)
        {
          CrmAuthenticationToken authToken = new CrmAuthenticationToken();
          authToken.AuthenticationType = 0;
          authToken.OrganizationName = context.OrganizationName;
    
          // Include support for impersonation.
          if (flag)
            authToken.CallerId = context.UserId;
          else
            authToken.CallerId = context.InitiatingUserId;
    
          CrmService service = new CrmService();
          service.CrmAuthenticationTokenValue = authToken;
          service.UseDefaultCredentials = true;
    
          
          // Include support for infinite loop detection.
          CorrelationToken corToken = new CorrelationToken();
          corToken.CorrelationId = context.CorrelationId;
          corToken.CorrelationUpdatedTime = context.CorrelationUpdatedTime;
          corToken.Depth = context.Depth;
    
          RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM");
    
          service.Url = String.Concat(regkey.GetValue("ServerUrl").ToString(), "/2007/crmservice.asmx");
          service.CorrelationTokenValue = corToken;
          
          return service;
        }

    but seems to fail when I do a service.Execute.

    Any tricks you guys know when working with child pipelines and a sample code perhaps related to retrieving data from the database from within a child pipeline.

    Thanks

    Tuesday, May 4, 2010 1:10 AM
  • Also, here is how I am trying to retrieve data from the db

         TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();
    
          // Set the properties of the target.
          targetRetrieve.EntityName = entityName;
          targetRetrieve.EntityId = id;
    
          // Create the request object.
          RetrieveRequest retrieve = new RetrieveRequest();
    
          // Set the properties of the request object.
          retrieve.Target = targetRetrieve;
          retrieve.ColumnSet = new ColumnSet();
    
          foreach (String attr in attributesToRetrieve)
          {
            retrieve.ColumnSet.AddColumn(attr);
          }
                
          // Indicate that the BusinessEntity should be retrieved as a DynamicEntity.
          retrieve.ReturnDynamicEntities = true;
    
          // Execute the request.
          RetrieveResponse retrieved = (RetrieveResponse)service.Execute(retrieve);
    
          // Extract the DynamicEntity from the request.
          DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity;
    
          return entity;
    Tuesday, May 4, 2010 1:11 AM
  • Hi,

    I tried retrieving value in a child pipeline and i was able to do so.

    Please try with the following simple code on your system which retrieves account name and number for a given account record.

          CrmService crmService = CreateCrmService(context, true);
          ColumnSet myCols=new ColumnSet();
          myCols.AddColumn("name");
          myCols.AddColumn("accountnumber");
          account myAccount=(account)crmService.Retrieve(EntityName.account.ToString(), new Guid("yourAccountRecordGuid"), myCols);

     

    CreateCrmService method

     

        /// <summary>
        /// Creates a CrmService proxy for plug-ins that execute in the child pipeline.
        /// </summary>
        /// <param name="context">The execution context that was passed to the plug-ins Execute method.</param>
        /// <param name="flag">Set to True to use impersontation.</param>
        /// <returns>A CrmServce instance.</returns>
        private static CrmService CreateCrmService(IPluginExecutionContext context, Boolean flag)
        {
          var authToken = new CrmAuthenticationToken { AuthenticationType = 0, OrganizationName = context.OrganizationName, CallerId = (flag ? context.UserId : context.InitiatingUserId) };
    
          var corToken = new CorrelationToken { CorrelationId = context.CorrelationId, CorrelationUpdatedTime = context.CorrelationUpdatedTime, Depth = context.Depth };
    
          var regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM", false);
    
          var service = new CrmService
          {
            CrmAuthenticationTokenValue = authToken,
            UseDefaultCredentials = true,
            Url = String.Concat(regkey.GetValue("ServerUrl").ToString(), "/2007/crmservice.asmx"),
            CorrelationTokenValue = corToken
          };
    
          return service;
        }

     

    Regards,

    Nishant Rana

     

     

     

     


    http://nishantrana.wordpress.com
    Tuesday, May 4, 2010 7:06 AM
  • Debug through your plugin or enable tracing and see if you can get a more informed error message.
    MSCRM Bing'd - http://bingsoft.wordpress.com
    Tuesday, May 4, 2010 7:14 AM
    Moderator
  • Hi All,In CRM 4.0 how to find the number of activites added to a particular account? i am writing a plugin for finding the number of activities added to a account. i,e total number of appointment/activities to be summed up and shown in a custom attribute in account.

    Very very Urgent


    Sai Krishna Yadav Software Engineer..

    Thursday, June 21, 2012 5:52 AM