Create a web job and get all active contract records which will be expire in 7 days

Create a web job and get all active contract records which will be expire in 7 days.

So for the above scenario we have to Create a Contract. 

For Contract creation you should go for advance in advance settings.


Now search contracts and click on result > click on new contract to create a contract.


Then a select template popup will be displayed > now in check box click on search icon and select service 


While creating contract we should provide some mandatory details. They are

contract name , customer , contract start date  , contract end date  and bill to customer 

Now according to our scenario the contract start date and contract end date difference should come for 7 days and that should be visible in Duration in days where as 7 days.


After Creating of contract you should create contract line which is visible left side of the contract which was created. 

In contract lines we have some mandatory fields they are

Title , Total cases / minutes and total price. After giving these details click on save 


To activate contract on top we have blue ribbon bar click on service and click on contracts there your new contract which is created will be visible. Beside it we have a check box click on it and on top Click on active then your contract will be activated. 


Now again click on advance find search according to the below filter image > click on result > click download fetch Xml.


The Fetch XML Code is : 

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">

  <entity name="contract">

    <attribute name="title" />

    <attribute name="contractid" />

    <order attribute="title" descending="false" />

    <filter type="and">

      <condition attribute="statecode" operator="eq" value="2" />

      <condition attribute="billingendon" operator="next-seven-days" />

    </filter>

  </entity>

</fetch>


Now go for console application in visual studio and write the code.

While writing the code you will be required for client id and  Secret id for that you need to Go to Azure portal.

https://portal.azure.com

After opening the azure portal we have view in Manage Micro soft Entra Id click on it.


Now Micro Soft Azure home page will be displayed click on App registration which was displayed in left side of the home page.

Now App Registrations Page was displayed in that click on all application and click new registration.

Name it and copy the Org URL (dynamics 365 url) till .com , past in Re directory url give /auth, WEB and click on registration.

Then App which you have been Register that page will be displayed. In that copy the client id (7ae5d5af-4c3e-44df-a289-d728a8281eab).



Now Click on Certificates and Secrets which is visible in App registration of left side list.

Next click on new client secrets then right side one popup will come add a client secret now give description and take expire as 3months and click on add

Then copy the value (4l18Q~.yCtJxsP-acHXty_34opgpbPr34yGsPbz6) which acts like secret id.


The app registration which was created for that you should get app user with privileges of System admin and System Customizer.

For App Users open Power app >click on gear icon >click on Admin > Environment > Sales trial > in access User click on see all > click on app users list > click on new app user > in right side create a new app user popup will be displayed > In that click on add an app.

Gear Icon and Click on Admin.


Environment 


Sales Trail.


Users.


Click on add Users.



New app USER



Create a new app user and click on add an app.


When click on add an app there then you can see the Certificates and Secrets in azure. So select that app > click on add button >












In Business unit take the App Registration , in security roles select System admin and system customizer and click on create.


Security roles


In console application from visual studio, write the code inside it take client id , value and fetch XML.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Tooling.Connector;


namespace wEbJoBcheckQ

{

    class Program

    {

        static void Main(string[] args)

        {

            string url = "https://org804e67eb.crm8.dynamics.com/main.aspx?";

            string ClientID = "6d78571c-2e15-45b1-b421-9ca45fb1c9a8";

            string ClientSecret = "zZV8Q~lQ22rv0-U5wL9h6MgGnqyC_ed7h5ap~dke";

            string AuthType = "ClientSecret";


            string conn = $@"AuthType={AuthType};url={url};ClientId={ClientID};ClientSecret={ClientSecret}";

            Console.WriteLine(conn);


            CrmServiceClient service = new CrmServiceClient(conn);

            string contractFetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +

"  <entity name='contract'>" +

"    <attribute name='title' />" +

"    <attribute name='contractid' />" +

"    <order attribute='title' descending='false' />" +

"    <filter type='and'>" +

"      <condition attribute='statecode' operator='eq' value='2' />" +

"      <condition attribute='billingendon' operator='next-seven-days' />" +

"    </filter>" +

"  </entity>" +

"</fetch>";


            EntityCollection contractActive = service.RetrieveMultiple(new FetchExpression(contractFetchXml));


            foreach (Entity contrcatActive in contractActive.Entities)

            {

                string contrcatName = contrcatActive.Contains("title") ? (string)contrcatActive.Attributes["title"] : null;

                Console.WriteLine(contrcatName);

            }

        }

    }

}

Build the solution in console app and Debug or execute the app. Then we will get the all active which will be expire in 7 days.



 
























Comments

Popular posts from this blog

If any case created, then check for the same user how many cases are created with in 30 days, if more than 2 and less than 5 send a mail to team lead, if more than 5 and less than 9 then send a mail to manager using power automate.

Create approve & reject ribbon buttons, once click on approve it should change the status field to approve.If clicked on reject it should change to Reject. Based on status field change trigger work flow to send a email to stating request is approved/Rejected.

How to get and set values in plugins?