Home > database >  How to run azure function quarter of first month and quarter of last month
How to run azure function quarter of first month and quarter of last month

Time:04-27

I have written one program but i need to run below program each quarter.

  public static void connect(string connection, string connectionSecret, string connectionOrg)
        {
            ServiceClient crmServiceClient = null;
            System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
            string connectionString = "Url="   connectionOrg   "; "  
            "AuthType=ClientSecret; "  
            "ClientId= "   connection   "; "  
            "ClientSecret="   connectionSecret;

            crmServiceClient = new ServiceClient(connectionString);
            if (crmServiceClient.IsReady)
            {
                ReadTarget(crmServiceClient);
            }
        }


        private static void ReadTarget(ServiceClient crmServiceClient)
        {
            try
            {
                Entity newAsmtarget = new Entity("fmc_asmtarget");
                string nameTarget, fmcAsm;
                decimal fmcTargetBonus;
                DateTime fmcFormDate, fmcToDate;
                QueryExpression query = new QueryExpression
                {
                    EntityName = "fmc_asmtarget",
                    ColumnSet = new ColumnSet("fmc_asmid", "fmc_fromdate",
                    "fmc_productongroundtarget", "fmc_salesrevenuetarget", "fmc_todate", "fmc_targetsipbonusamount", "fmc_dsotarget")
                };
                var qe = new QueryExpression("systemuser");
                qe.ColumnSet = new ColumnSet("systemuserid", "fullname", "lastname", "firstname", "domainname", "businessunitid");

                query.ColumnSet = new ColumnSet(true);
                Guid g1 = Guid.NewGuid();

                EntityCollection response = crmServiceClient.RetrieveMultiple(query);
                foreach (Entity item in response.Entities)
                {
                    if (response.Entities.Count == 0)
                    {
                        return;
                    }
                    if (response.Entities.Count > 0)
                    {
                        fmcFormDate = item.GetAttributeValue<DateTime>("fmc_fromdate").ToLocalTime();
                        var fmcFormDate1 = fmcFormDate.ToString("yyyy-MM-dd");
                        fmcToDate = item.GetAttributeValue<DateTime>("fmc_todate").ToLocalTime();
                        var fmcToDate1 = fmcToDate.ToString("yyyy-MM-dd");

                        if ((fmcFormDate >= fmcToDate | fmcFormDate <= fmcToDate))
                        {
                            nameTarget = item.GetAttributeValue<string>("fmc_name");
                            fmcAsm = item.GetAttributeValue<EntityReference>("fmc_asmid").Name;

                            var fmcId = item.GetAttributeValue<EntityReference>("fmc_asmid").Id;
                            double saleRevacutal = FetchSRacutalFromGoal(crmServiceClient, fmcId, fmcFormDate1, fmcToDate1);
                            var fmcSalesRev = item.GetAttributeValue<Money>("fmc_salesrevenuetarget").Value;
                            var fmcProductOnGround = item.GetAttributeValue<Money>("fmc_productongroundtarget").Value;
                            fmcTargetBonus = item.GetAttributeValue<Money>("fmc_targetsipbonusamount").Value;
                            Decimal fmcDsoTarget = item.GetAttributeValue<Decimal>("fmc_dsotarget");
                            int intValue = Convert.ToInt32(fmcDsoTarget);
                            newAsmtarget.Id = item.Id;

                            Entity _actual = new Entity("fmc_asmactual");

                            _actual.Attributes["fmc_asmid"] = new EntityReference("systemuser", fmcId);
                            _actual.Attributes["fmc_name"] = nameTarget;
                            _actual.Attributes["fmc_targetsipbonusamount"] = Convert.ToDecimal(fmcTargetBonus);
                            _actual.Attributes["fmc_fromdate"] = fmcFormDate;
                            _actual.Attributes["fmc_todate"] = fmcToDate;
                            _actual.Attributes["fmc_salesrevenuetarget"] = fmcSalesRev;
                            _actual.Attributes["fmc_productongroundtarget"] = fmcProductOnGround;
                            _actual.Attributes["fmc_dsotarget"] = fmcDsoTarget;
                            _actual.Attributes["fmc_salesrevenueactual"] = saleRevacutal;

                            crmServiceClient.Create(_actual);
                        }
                    }

                }

            }

            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("An error occured in : "   ex.Message.ToString(), ex);

            }
        }

        private static double FetchSRacutalFromGoal(ServiceClient crmServiceClient, Guid fmcId, string fmcFormDate1, string fmcToDate1)
        {
            ////EntityCollection collection = null;
            string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"  
                                                "<entity name='invoice'>"  
                                                "<attribute name='name' />"  
                                                "<attribute name='customerid' />"  
                                                "<attribute name='statuscode' />"  
                                                "<attribute name='totalamount' />"  
                                                "<attribute name='invoiceid' />"  
                                                "<order attribute='name' descending='false' />"  
                                                "<filter type='and'>"  
                                                    "<condition attribute='overriddencreatedon' operator='on-or-after' value='"   fmcFormDate1   "' />"  
                                                    "<condition attribute='overriddencreatedon' operator='on-or-before' value='"   fmcToDate1   "' />"  
                                                    "<condition attribute='statuscode' operator='eq' value='4' />"  
                                                    "<condition attribute='ownerid' operator='eq'  value='"   fmcId   "' />"  
                                                "</filter>"  
                                                "</entity>"  
                                                "</fetch>";


            EntityCollection response1 = crmServiceClient.RetrieveMultiple(new FetchExpression(fetchXml));

            List<Result1> result1 = new List<Result1>();
            double baseAmount = 0;

            if (response1.Entities.Count > 0)
            {
                foreach (Entity item1 in response1.Entities)
                {
                    Result1 Types1 = new Result1();
                    //var sra = item1.GetAttributeValue<Money>("totalamount").Value;
                    //Types1.fmc_salesrevenueactual = decimal.ToDouble(sra);
                    var sra = item1.GetAttributeValue<Money>("totalamount").Value;
                    Types1.fmc_baseamount = decimal.ToDouble(sra);
                    baseAmount  = decimal.ToDouble(sra);
                    Types1.fmc_fromdate = item1.GetAttributeValue<DateTime>("fmc_fromdate").ToLocalTime();
                    Types1.fmc_todate = item1.GetAttributeValue<DateTime>("fmc_todate").ToLocalTime();
                    result1.Add(Types1);
                }

            }

            return baseAmount;
        }

    }
    public class RootClassAsmCreate
    {
        public List<Result1> Results { get; set; }
    }

    public class Result1
    {
        public DateTime fmc_fromdate { get; set; }
        public DateTime fmc_todate { get; set; }
        public double fmc_salesrevenueactual { get; set; }
        public double fmc_baseamount { get; set; }

    }

}

CodePudding user response:

Use this CRON expression to run your function at 12:00 AM, on day 1 of the month, every 3 months.

0 0 1 */3 *

Your scheduled Azure function declaration will look like that:

public static class MyFunctionThatShouldRunQuarterly
{
  [FunctionName(nameof(MyFunctionThatShouldRunQuarterly))]
  public static async Task Run([TimerTrigger("0 0 1 */3 *")] TimerInfo myTimer, ILogger externalLogger) 
  {
     // your code goes here     
  }
}

CodePudding user response:

For a Scheduled Azure Function, you can set up a CRON expression to specify when it should be executed.

This CRON expression takes several values in the form

{second} {minute} {hour} {day} {month} {day-of-week}

So this will execute on the 1st of every quarter (January, April, July, October), at midnight:

0 0 0 1 1,4,7,10 *

A month-expression */3 will execute every three months, but (possibly) on months 3, 6, 9 and 12


To fire every Saturday at (for example) 1AM (01:00), you can use this:

0 0 1 * * Sat

But then your code would need to check whether "now" is the first or last Saturday of the quarter and just exit if it is not.

  •  Tags:  
  • c#
  • Related