1st Once case is created, automatically timespan record need to create with current time as startdate.

 Create the timespan entity with name,time differece(integer),start date and time, end date and time,status of case, case entity lookup.

 

1. Once case is created, automatically timespan record need to create with current time as startdate.

Fist Create a Custom entity as TimeSpan.

Next create fields in TimeSpan with name, time differece(integer),start date and time, end date and time,status of case, case entity lookup.

1. Once case is created, automatically timespan record need to create with current time as startdate.

Step1: Create the option set same as case status and values of the both option sets should be same, at that time it help to create easy way.

Step2: Write the plugin code to create the time span record by reading the status of the case.

Step3: Write code to create the record of time span with status of  the case and start time without end date.

Step4: Give the look up of the target entity, because we need to create the relation between these two entity's.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;


namespace CreateTimeSSNE

{

    public class Span : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

            try

            {

                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                IOrganizationService service = factory.CreateOrganizationService(context.UserId);

                ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

 

                //effi_name, effi_lastname, effi_fullname

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

                {

                    Entity caseEntity = (Entity)context.InputParameters["Target"];

                    string caseRecord = caseEntity.Contains("title") ? (string)caseEntity.Attributes["title"] : null;


                    DateTime dateTime = new DateTime();

                    dateTime = DateTime.UtcNow;

                    trace.Trace("Date change {0} ", dateTime.ToString());


                    Entity TimeSpanEntity = new Entity("effizz_timespan");

 

                    int optionValue = 684200001;

                    OptionSetValue value = new OptionSetValue(optionValue);

 

                    TimeSpanEntity["effizz_newstatus"] = value;

                    TimeSpanEntity.Attributes["effizz_name"] = "TimeSpan for " + caseRecord;

                    TimeSpanEntity.Attributes["effizz_case"] = new EntityReference(caseEntity.LogicalName, caseEntity.Id);

                    TimeSpanEntity.Attributes["effizz_start"] = dateTime;

 

                    service.Create(TimeSpanEntity);

                }

            }

            catch (Exception ex)

            {

                throw new InvalidPluginExecutionException(ex.Message);

            }

        }

    }

}

 

Step 5: After writing the code, build the solution and register.

In plugin Registration for Registration a step fill this details from the below picture.


Step 6: Now Create a case record ,click on save and go to Related Tab then check in drop down Time Span is showing or not 



Step 7: Click on Time Span and check record is created with start date and time or not

Then you will get the following output after trigger 





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?