locked
MS CRM 4 Workflow RRS feed

  • Question

  • I have created a birthday reminder workflow , I want it run into all the existing data (Contact), how not to run workflow manually one by one for each data ( contact ). Is there any way I can do this ?
    Monday, January 24, 2011 1:15 AM

Answers

  • You can run reminder workflow to use the advenced Find. I think it will be difficult to run manually workflow. You can write a console application to send email like as,

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using EmailApplication.CrmWs;
    using EmailApplication.Properties;

    namespace EmailApplication
    {
        class Program
        {

            static void Main(string [] args)
            {
                //İlgili kişiler varlığından email adresi boş olmayan ve doğum günü bugün olan kişlerin listesini alalım.
                string Query = @"
                                            DECLARE @UTCDiff INT
                                             SET @UTCDiff = DATEDIFF(HOUR, GetUTCDate(), GETDATE())
                                            SELECT
                                                   f.fullname AS fullname,
                                                   f.contactid AS contactid,
                                                f.fullname AS contactname,
                                                   f.birthdate AS birthdate,
                                                   f.emailaddress1 AS email                                                                                                                                     
                                            FROM
                                                    FilteredContact AS f WITH (NOLOCK)                                                                         
                                            WHERE
                                                   f.birthdate IS NOT NULL
                                                   AND (DAY(DATEADD(hh, @UTCDiff, f.birthdate)) = DAY(GETDATE()))
                                                   AND (MONTH(DATEADD(hh, @UTCDiff, f.birthdate)) = MONTH(GETDATE()))    
                                                   AND f.emailaddress1 IS NOT NULL
                                             ORDER BY f.fullname" ;
                try
                {
                    DataTable dt = new DataTable ();
                    SqlConnection con = GetCrmSqlConnection();

                    if (con != null )
                    {
                        SqlDataAdapter adap = new SqlDataAdapter (Query, con);
                        adap.Fill(dt);
                        if (dt.Rows.Count != 0)
                        {
                            SendEmail(dt);
                        }
                    }
                }
                catch (Exception ex)
                {
                    //throw new Exception(ex.Message);
                    throw ex;
                }

            }
            private static void SendEmail(DataTable dt)
            {
                try
                {
                    email mail = new email(); // Email sınıfını oluşturalım.
                    activityparty from = new activityparty();
                    activityparty toSingle = null ;
                    from.partyid = new Lookup();
                    from.partyid.type = EntityName.systemuser.ToString();
                     from.partyid.Value = new Guid (Settings.Default.From.ToString()); // Gönderen Id'si
                    mail.from = new activityparty[] { from };
                    if (dt != null )
                    {
                        CrmService servis = ConnectService(); //crm servisini çağıralım.
                        activityparty[] to;

                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            to = new activityparty[dt.Rows.Count];
                            toSingle = new activityparty();
                            toSingle.partyid = new Lookup();
                            toSingle.partyid.type = EntityName.contact.ToString();
                            toSingle.partyid.Value = new Guid (Convert .ToString(dt.Rows[i]["contactid" ]));
                             to[i] = toSingle;
                            mail.to = to;
                            mail.subject = "Doğum Gününüz Kutlu Olsun." ;                   
                            CrmBoolean direction = new CrmBoolean();
                            direction.Value = true ;
                            mail.directioncode = direction;      
                            Guid mailId = servis.Create(mail);
                            GetTrackingTokenEmailRequest getRequest = new GetTrackingTokenEmailRequest();
                            getRequest.Subject = mail.subject;
                            GetTrackingTokenEmailResponse getResponse = (GetTrackingTokenEmailResponse)servis.Execute(getRequest);
                            SendEmailRequest request = new SendEmailRequest();
                            request.EmailId = mailId;
                            request.IssueSend = true ;
                            request.TrackingToken = getResponse.TrackingToken;
                            SendEmailResponse response = (SendEmailResponse)servis.Execute(request);
                            //}
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception (ex.Message);
                }
            }

            private static CrmService ConnectService()
            {          
                CrmService servis = null ;
                try
                {
                    servis = new CrmService();
                    CrmWs.CrmAuthenticationToken token = new CrmWs.CrmAuthenticationToken();
                    token.AuthenticationType = 0;
                    token.OrganizationName = "crm" ;
                    servis.CrmAuthenticationTokenValue = token;
                    servis.Credentials = System.Net.CredentialCache .DefaultNetworkCredentials;

                 }
                catch (Exception ex)
                {
                    throw new Exception (ex.Message);
                }
                return servis;
            }

            private static SqlConnection GetCrmSqlConnection()
            {
                string connStr = null ;
                SqlConnection conn = null ;
                try
                {
                    connStr = String .Format("Data Source=.;Initial Catalog=ORG_MSCRM;Integrated Security=True" );
                    conn = new SqlConnection (connStr);
                }
                catch (SqlException ex)
                {
                    throw new Exception (ex.Message);
                }
                catch (Exception ex)
                {
                    throw new Exception (ex.Message);
                }
                return conn;
            }

        }

    }

    Regards,
    • Proposed as answer by Jim Glass Jr Tuesday, January 25, 2011 8:41 PM
    • Marked as answer by Jim Glass Jr Wednesday, February 2, 2011 9:28 PM
    Monday, January 24, 2011 2:54 PM

All replies

  • Make it on demand workflow and select all the contacts in the view and click run workflow.
    Monday, January 24, 2011 2:10 AM
  •  

    Hi Padmakar Gollapalli .

    I think, in crm 4 we can not select all the contacts in the viewin one page, if there are 100.000 data ( contact ) so we have to go to next page and do it again. There are some kind of automatization for it ??

    Monday, January 24, 2011 2:55 AM
  • No, you can't; not out of the box, anyway.  I did see mention somewhere of a "hack" to change the default max record display of 250 to some value higher, but it is unsupported.  In any event, I would think doing so via the Web Client would be somewhat unreliable, particularly for ~100,000 records.

    If you have .NET skills, it is relatively trivial to write a small application that will trigger a workflow for a selection (or all if such are your requirements) of entity instances; using the ExecuteWorkflow Message.


    --pogo (pat)
    Monday, January 24, 2011 3:09 AM
  • I agree with pat that you can't do it out of the box.

    As pat mentioned you can able to achieve this with Execute workflow.

    Monday, January 24, 2011 3:36 AM
  • You can run reminder workflow to use the advenced Find. I think it will be difficult to run manually workflow. You can write a console application to send email like as,

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    using EmailApplication.CrmWs;
    using EmailApplication.Properties;

    namespace EmailApplication
    {
        class Program
        {

            static void Main(string [] args)
            {
                //İlgili kişiler varlığından email adresi boş olmayan ve doğum günü bugün olan kişlerin listesini alalım.
                string Query = @"
                                            DECLARE @UTCDiff INT
                                             SET @UTCDiff = DATEDIFF(HOUR, GetUTCDate(), GETDATE())
                                            SELECT
                                                   f.fullname AS fullname,
                                                   f.contactid AS contactid,
                                                f.fullname AS contactname,
                                                   f.birthdate AS birthdate,
                                                   f.emailaddress1 AS email                                                                                                                                     
                                            FROM
                                                    FilteredContact AS f WITH (NOLOCK)                                                                         
                                            WHERE
                                                   f.birthdate IS NOT NULL
                                                   AND (DAY(DATEADD(hh, @UTCDiff, f.birthdate)) = DAY(GETDATE()))
                                                   AND (MONTH(DATEADD(hh, @UTCDiff, f.birthdate)) = MONTH(GETDATE()))    
                                                   AND f.emailaddress1 IS NOT NULL
                                             ORDER BY f.fullname" ;
                try
                {
                    DataTable dt = new DataTable ();
                    SqlConnection con = GetCrmSqlConnection();

                    if (con != null )
                    {
                        SqlDataAdapter adap = new SqlDataAdapter (Query, con);
                        adap.Fill(dt);
                        if (dt.Rows.Count != 0)
                        {
                            SendEmail(dt);
                        }
                    }
                }
                catch (Exception ex)
                {
                    //throw new Exception(ex.Message);
                    throw ex;
                }

            }
            private static void SendEmail(DataTable dt)
            {
                try
                {
                    email mail = new email(); // Email sınıfını oluşturalım.
                    activityparty from = new activityparty();
                    activityparty toSingle = null ;
                    from.partyid = new Lookup();
                    from.partyid.type = EntityName.systemuser.ToString();
                     from.partyid.Value = new Guid (Settings.Default.From.ToString()); // Gönderen Id'si
                    mail.from = new activityparty[] { from };
                    if (dt != null )
                    {
                        CrmService servis = ConnectService(); //crm servisini çağıralım.
                        activityparty[] to;

                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            to = new activityparty[dt.Rows.Count];
                            toSingle = new activityparty();
                            toSingle.partyid = new Lookup();
                            toSingle.partyid.type = EntityName.contact.ToString();
                            toSingle.partyid.Value = new Guid (Convert .ToString(dt.Rows[i]["contactid" ]));
                             to[i] = toSingle;
                            mail.to = to;
                            mail.subject = "Doğum Gününüz Kutlu Olsun." ;                   
                            CrmBoolean direction = new CrmBoolean();
                            direction.Value = true ;
                            mail.directioncode = direction;      
                            Guid mailId = servis.Create(mail);
                            GetTrackingTokenEmailRequest getRequest = new GetTrackingTokenEmailRequest();
                            getRequest.Subject = mail.subject;
                            GetTrackingTokenEmailResponse getResponse = (GetTrackingTokenEmailResponse)servis.Execute(getRequest);
                            SendEmailRequest request = new SendEmailRequest();
                            request.EmailId = mailId;
                            request.IssueSend = true ;
                            request.TrackingToken = getResponse.TrackingToken;
                            SendEmailResponse response = (SendEmailResponse)servis.Execute(request);
                            //}
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception (ex.Message);
                }
            }

            private static CrmService ConnectService()
            {          
                CrmService servis = null ;
                try
                {
                    servis = new CrmService();
                    CrmWs.CrmAuthenticationToken token = new CrmWs.CrmAuthenticationToken();
                    token.AuthenticationType = 0;
                    token.OrganizationName = "crm" ;
                    servis.CrmAuthenticationTokenValue = token;
                    servis.Credentials = System.Net.CredentialCache .DefaultNetworkCredentials;

                 }
                catch (Exception ex)
                {
                    throw new Exception (ex.Message);
                }
                return servis;
            }

            private static SqlConnection GetCrmSqlConnection()
            {
                string connStr = null ;
                SqlConnection conn = null ;
                try
                {
                    connStr = String .Format("Data Source=.;Initial Catalog=ORG_MSCRM;Integrated Security=True" );
                    conn = new SqlConnection (connStr);
                }
                catch (SqlException ex)
                {
                    throw new Exception (ex.Message);
                }
                catch (Exception ex)
                {
                    throw new Exception (ex.Message);
                }
                return conn;
            }

        }

    }

    Regards,
    • Proposed as answer by Jim Glass Jr Tuesday, January 25, 2011 8:41 PM
    • Marked as answer by Jim Glass Jr Wednesday, February 2, 2011 9:28 PM
    Monday, January 24, 2011 2:54 PM