Showing posts with label ASP.net C#. Show all posts
Showing posts with label ASP.net C#. Show all posts

Populate Regarding Field In CRM 2013 Using Plugin

0 comments


Populate regarding work with creating records.

Here are my code (Im Creating new task with populated Regarding Field):

 Entity newTask = new Entity("task");
 newTask["subject"] = "Document" + DateTime.Now.ToShortDateString();
 newTask["description"] = "Description";
 newTask["regardingobjectid"] = new Microsoft.Xrm.Sdk.EntityReference("account", account.Id);
 service.Create(newTask);

Result:

New Task will be created with subject field, description field and regarding field with value.

Pro Rate Calculation (Rental And Service Cost)

0 comments

Problem : How does pro rate work for rental.
*Note : Its different from all company.
There are 6 Condition will hit when u do pro rate calculation 

1. Rental Start <= Charge Start, Rental Start < Rental End < Charge End.
2. Rental End> Rental Start < Charge End., Rental End >= Charge End.
3. Rental Start <= Charge Start, Rental End >= Charge End.
4. Rental Start > Charge Start, Rental End < Charge End.
5. Rental Start < Charge Start, Rental End = Charge End.
6. Rental Start = Rental End.

Int32 DropdownMonth = 8;
Int32 DropdownYear = 2013;
int32 DaysActualPerMonth = 0;
int32 DaysPerMonth = 0;

// Eg. August bill September Rental
Int32 DaysInMonth = DateTime.DaysInMonth(DropdownYear, DropdownMonth + 1);
DateTime FirstDayOfTheMonth = GetFirstDayOfMonth(DropdownYear, DropdownMonth + 1);
DateTime LastDayOfTheMonth = GetLastDayOfMonth(DropdownYear, DropdownMonth + 1);

DateTime RentalCommenceDate = RentalCostCommenceDate; //Get Rental Commence Date
DateTime RentalDueDate = RentalCostDueDate; // Get Rental Due Date

//ParameterSetDateTime;  Equal to previous generation date. May bill invoice date = Invoice date (June Date)
if (RentalCommenceDate <= FirstDayOfTheMonth && RentalDueDate > RentalCommenceDate && RentalDueDate < LastDayOfTheMonth)
{
    //Return Partial Rental Amount (A)
    DaysActualPerMonth = DaysInMonth;
    DaysPerMonth = GetDaysBetweenDates(FirstDayOfTheMonth, RentalDueDate) + 1;
}
else if (RentalCommenceDate > FirstDayOfTheMonth && RentalCommenceDate < LastDayOfTheMonth && RentalDueDate >= LastDayOfTheMonth)
{
    //Return Partial Rental Amount (B)
    DaysActualPerMonth = DaysInMonth;
    DaysPerMonth = GetDaysBetweenDates(RentalCommenceDate, LastDayOfTheMonth) + 1;
}
else if (RentalCommenceDate <= FirstDayOfTheMonth && RentalDueDate >= LastDayOfTheMonth)
{
    //Return Full Rental Amount (C)
    DaysActualPerMonth = DaysInMonth;
    DaysPerMonth = DaysInMonth;
}
else if (RentalCommenceDate > FirstDayOfTheMonth && RentalDueDate > RentalCommenceDate && RentalDueDate < LastDayOfTheMonth)
{
    //Return Partial Rental Amount (D)
    DaysActualPerMonth = DaysInMonth;
    DaysPerMonth = GetDaysBetweenDates(RentalCommenceDate, RentalDueDate) + 1;
}
else if (RentalCommenceDate < FirstDayOfTheMonth && RentalDueDate > RentalCommenceDate && RentalDueDate == LastDayOfTheMonth)
{
    //Return Partial Rental Amount (E)
    DaysActualPerMonth = DaysInMonth;
    DaysPerMonth = 1;
}
else if (RentalCommenceDate == RentalDueDate)
{
    //Return Partial Rental Amount = One Day Rental (F)
    DaysActualPerMonth = DaysInMonth;
    DaysPerMonth = 1;
}
else
{
    //Nothing
}

C# Set DateTime Variables Time to Default Time (12:00:00:00)

0 comments

To Standardize the Time for all DateTime Value.

String NewRentalCommenceDate = RentalCommenceDate.ToShortDateString();
RentalCommenceDate = Convert.ToDateTime(NewRentalCommenceDate);
String NewRentalDueDate = RentalDueDate.ToShortDateString();
RentalDueDate = Convert.ToDateTime(NewRentalDueDate);

C# Get 1 Day of The Month And Last Day of The Month

0 comments

Problem : Getting first and last day of the month.

Solution :

 // Get Last Day Of The Month
public static DateTime GetLastDayOfMonth(int year, int month)
{
    return new DateTime(year, month, DateTime.DaysInMonth(year, month));
}

// Get 1 Day Of The Month
public static DateTime GetFirstDayOfMonth(int year, int month)
{
    return new DateTime(year, month, 1);
}

C# Get Duration of Days Between 2 Dates

0 comments

Problem : How to get duration of days between two dates.

Solution :

// Get Date Between Two Dates
private int GetDaysBetweenDates(DateTime firstDate, DateTime secondDate)
{
      return secondDate.Subtract(firstDate).Days;
}


C# Get Week Number In Year

0 comments

Problem : How do I get number of week per year.

Solution :

// Get Num Of Week Per Year
 public static int Iso8601WeekNumber(DateTime dt)
{
     System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
     int weekNumOnMonth = ci.Calendar.GetWeekOfYear(dt, ci.DateTimeFormat.CalendarWeekRule, ci.DateTimeFormat.FirstDayOfWeek);
     return weekNumOnMonth;
}

Reference:
using System.Globalization;

C# Get Week Number In Month

0 comments

Problem : How do I get number of week per month for the particular date?

Solution :

// Get Num of Week Per Month
public static int GetWeekInMonth(DateTime date)
{
    DateTime tempdate = date.AddDays(-date.Day + 1);
    CultureInfo ciCurr = CultureInfo.CurrentCulture;
    int weekNumStart = ciCurr.Calendar.GetWeekOfYear(tempdate, CalendarWeekRule.FirstFourDayWeek, ciCurr.DateTimeFormat.FirstDayOfWeek);
    int weekNum = ciCurr.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, ciCurr.DateTimeFormat.FirstDayOfWeek);
    return weekNum - weekNumStart + 1;
}

Reference :
using System.Globalization;


For Loop in C# Sample

0 comments



Sample: Code using for loop to pass each of the array variable value.

string[] names = {"Name1","Name2","Name3"};

for (int ArrayNo = 0; ArrayNo < names.Length; ArrayNo++)
{
    string AccountID= names[ArrayNo];
}

How to Retrieve/Get N:N(Many to Many) Records Using C# && fetchXML

0 comments


Problem with retrieving Many to Many records.
Solution: Using C# fetchxml to retrieve all the information. FetchXML is generated through advance find button in CRM 2011.
Advantage: This fetchXML in C# also can be use in one to many (1:N) records too.

Function And Script


#region Fetch N:N Billing Account Filter By Scheduler

string fetchxml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
                                  "<entity name='vw_billingaccountcode'>" +
                                    "<attribute name='vw_billingaccountcodeid' />" +
                                    "<attribute name='vw_name' />" +
                                    "<attribute name='createdon' />" +
                                    "<order attribute='vw_name' descending='false' />" +
                                    "<link-entity name='vw_vw_scheduler_vw_billingaccountcode' from='vw_billingaccountcodeid' to='vw_billingaccountcodeid' visible='false' intersect='true'>" +
                                      "<link-entity name='vw_scheduler' from='vw_schedulerid' to='vw_schedulerid' alias='aa'>" +
                                        "<filter type='and'>" +
                                          "<condition attribute='vw_schedulerid' operator='eq' uiname='Test' uitype='vw_scheduler' value='{" + ID.ToUpper() + "}' />" +
                                        "</filter>" +
                                      "</link-entity>" +
                                    "</link-entity>" +
                                  "</entity>" +
                                "</fetch>";

EntityCollection entities = OrgService.RetrieveMultiple(new FetchExpression(fetchxml));
foreach (Entity result in entities.Entities)
{
       string BillingAccountId = result.Id.ToString(); //All the result will be show here. Looping.
}

#endregion

C# Get/Retrieve Week In Month And Week In Year

0 comments



*Note get DayOfWeek, Week In Month, Week In Year, Today Date, Today Month, Today Year, Today Time And Format.

Using 

using System.Globalization;

Function


        public static int Iso8601WeekNumber(DateTime dt)
        {
            System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            int weekNumOnMonth = ci.Calendar.GetWeekOfYear(dt, ci.DateTimeFormat.CalendarWeekRule, ci.DateTimeFormat.FirstDayOfWeek);
            return weekNumOnMonth;
        }

        public static int GetWeekInMonth(DateTime date)
        {
            DateTime tempdate = date.AddDays(-date.Day + 1);
            CultureInfo ciCurr = CultureInfo.CurrentCulture;
            int weekNumStart = ciCurr.Calendar.GetWeekOfYear(tempdate, CalendarWeekRule.FirstFourDayWeek, ciCurr.DateTimeFormat.FirstDayOfWeek);
            int weekNum = ciCurr.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, ciCurr.DateTimeFormat.FirstDayOfWeek);
            return weekNum - weekNumStart + 1;
        }

Eg.


WeekInYear = Iso8601WeekNumber(DateTime.Now); //This Year Week
WeekInMonth = GetWeekInMonth(DateTime.Now); //This Month Week
DayOfWeek = DateTime.Now.DayOfWeek.ToString(); //Day Of Week eg.Monday
MonthOnly = DateTime.Now.Month; //Today Month
DateOnly = DateTime.Now.Day; //Today Day
YearOnly = DateTime.Now.Year; //Today Year
TimeOnly2400 = DateTime.Now.ToString("HH:mm:ss tt"); //Now Time In 2400 Format
TimeOnly1200 = DateTime.Now.ToString("h:m tt"); //Now Time In 1200 Format

C# Retrieve/Get Last Character

0 comments



Way to retrieve last character of the string.

public string GetLast(string s, int tail_length)
{
            if (tail_length >= s.Length)
                return s;
            return s.Substring(s.Length - tail_length);
}

eg.


string OneYearLastChar = GetLast(System.DateTime.Now.Year.ToString(), 1);
- Get Last Character

string TwoLastChar = GetLast(System.DateTime.Now.Year.ToString(), 2);
- Get Last Two Character

C# Request.QueryString/ Get Parameter Value From URL

0 comments


This request query string act as action to get url parameter.

eg. 

Request.QueryString["id"]
url: http://Sample.com&id=12345
Request.QueryString will get the value of 12345

How do we get array parameter from URL using Request.QueryString.

eg.

string[] names = Request.QueryString.GetValues("id");
names will hold the value of the parameter pass through URL.

This is the way to retrieve or get value from parameter.

Closing Webpage Without Prompt

0 comments


Add Javascript to webpage


    function AssignContent()
    {
        window.open('', '_self', '');
        window.close();
    }

Add C# code at webpage aspx page.


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!ClientScript.IsClientScriptBlockRegistered("k1"))
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "k1", "<script language='javascript'>AssignContent();</script>");
                }
            }
        }

During form onload the webpage will close.