web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Create batch job for on premise 2016

(0) ShareShare
ReportReport
Posted on by 483

Hello,

I want to create a batch job file.. When case create date reaches to 12 months i want to update one field. Pls suggest how to implement this.  Do i create console app first?  Pls suggest how to start on this.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi Krishna,

    You can write a console application and schedule it using windows scheduler, Download CRM 2016 SDK, You can start from here: community.dynamics.com/.../simple-dynamics-crm-console-application

  • dkrishna Profile Picture
    483 on at

    Thanks mahender.. Can you pls give me logic how to implement in it getting active cases and updating description field in it !!

  • dkrishna Profile Picture
    483 on at

    what is the best way to get records where case create date is 12 months ago from now ..can we get from query expression ? or fecth xml.. pls suggest query for this .

    Thanks

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi krishna,

    Give me 30 mins I will share the code.

  • dkrishna Profile Picture
    483 on at

    Thanks Goutam

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Krishna,

    1) Create one console application.

    2) Download SDK and take dll reference

    3) Add following dll reference

    Microsoft.Xrm.Sdk;
    Microsoft.Xrm.Sdk.Client;

    Add System.Configuration; from .NET assembly

    3 )Copy the the code in your class file

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.ServiceModel.Description;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Xrm.Sdk.Query;
    
    namespace CRMConnectionTestProject
    {
        class UpdateTitleFiedOldCase
        {
            static void Main(string[] args)
            {
                OrganizationServiceProxy service = GetCRMService();
                //Write your logic here 
                string FetchXML = @"<fetch version='1.0' output-format='xml - platform' mapping='logical' distinct='false'>" +
                                    "<entity name = 'incident'>" +
                                       "<attribute name = 'title'  />" +
                                        "<attribute name = 'incidentid'  />" +
                                        "<order attribute = 'title' descending = 'false' />" +
                                        "<filter type = 'and' >" +
                                        "<condition attribute = 'createdon' operator= 'olderthan-x-years' value='{0}'  />" +
                                      "</filter>" +
                                    "</entity>" +
                            "</fetch>";
    
                FetchXML = string.Format(FetchXML, "1");
                EntityCollection data = null;
                data = service.RetrieveMultiple(new FetchExpression(FetchXML));
    
                foreach (Entity ent in data.Entities)
                {
                    ent.Attributes["title"] = "Updated Title";
                    service.Update(ent);
                }
                //----
            }
    
    
    
    
            public static OrganizationServiceProxy GetCRMService()
            {
    
                String _organizationUniqueName = ConfigurationManager.AppSettings["OrganizationName"];
                String _userName = ConfigurationManager.AppSettings["Username"];
                String _password = ConfigurationManager.AppSettings["Password"];
                String _organizationUri = ConfigurationManager.AppSettings["OrganizationUri"];
                Guid _userId = new Guid(ConfigurationManager.AppSettings["UserId"]);
                String _domain = ConfigurationManager.AppSettings["Domain"];
    
    
                OrganizationServiceProxy serviceProxy = null;
                try
                {
    
                    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MSCRM_Connection"].ConnectionString;
                    if (!string.IsNullOrEmpty(connectionString))
                    {
    
    
                        Dictionary<string, string> settingsKeyValues = new Dictionary<string, string>();
                        var list = connectionString.Split(';').Select(keyValue => keyValue.Split('=')).ToList();
    
                        foreach (var item in list)
                        {
                            if (item.Length > 1)
                                settingsKeyValues.Add(item[0].Trim(), item[1].Trim());
    
                        }
    
    
                        string configserverurl = _organizationUri;
                        string userId = _userName;
                        string passWord = _password;
                        string domain = _domain;
                        if (!String.IsNullOrEmpty(configserverurl) && !String.IsNullOrEmpty(userId) &&
                            !String.IsNullOrEmpty(passWord) && !String.IsNullOrEmpty(domain))
                        {
                            Uri uri = new Uri(configserverurl);
                            IServiceConfiguration<IOrganizationService> config = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(uri);
                            if (config.AuthenticationType == AuthenticationProviderType.Federation)
                            {
                                ClientCredentials clientCredential = new ClientCredentials();
                                clientCredential.UserName.UserName = userId;
                                clientCredential.UserName.Password = passWord;
                                serviceProxy = new OrganizationServiceProxy(config, clientCredential);
                            }
                            else
                            {
                                ClientCredentials clientCredential = new ClientCredentials();
                                clientCredential.Windows.ClientCredential = new System.Net.NetworkCredential() { Domain = domain, Password = passWord, UserName = userId };
                                serviceProxy = new OrganizationServiceProxy(uri, null, clientCredential, null);
    
                            }
    
                        }
                    }
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
    
                return serviceProxy;
            }
        }
    }
    


    4 ) Copy App.Config and replace the CRM connection details , replace all the related and userid GUID with system user.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    	<appSettings>
    		<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />	
    		<add key="OrganizationName" value="DEV" />
    		<add key="OrganizationUri" value="184.47.86.75/.../Organization.svc" />
    		<add key="Username" value="ServiceUser" />
    		<add key="Password" value="XXXXXXX" />
    		<add key="Domain" value="domainname" />
    		<add key="UserId" value="64675A4F-5983-E611-80E3-0050560111FF" /> <!--UserID of systemuser-->
    	</appSettings>
    	<connectionStrings>
    		<!--On-premises with provided user credentials-->
    		<add name="MSCRM_Connection" connectionString="Url=http://164.48.86.15/DEV; Username=domain/username; Password=*******" />
    	</connectionStrings>
      <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
        </startup>
    </configuration>

    5) Build your console you will get the application exe.

    6)Scheduled it in windows task scheduler

    http://www.thewindowsclub.com/how-to-schedule-batch-file-run-automatically-windows-7

    Hope this helps.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Krishna,

    You can also check below reference for scheduling job in task scheduler.

    www.c-sharpcorner.com/.../console-application-using-windows-scheduler

    Hope this helps.

  • dkrishna Profile Picture
    483 on at

    Thanks alot Gautom ..Pls you tell me where i can find .EXE file after building ? Can you pls suggest path of it ?

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    You exe should be under bin\Debug or bin\Release depending on the your app configuration.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans