Thắng Trương
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

Go down
avatar
Admin
Admin
Tổng số bài gửi : 1562
Join date : 20/05/2018
https://thangcrm2.forumvi.com

[Workflow] Nhập ngày sinh, xuất tuổi Empty [Workflow] Nhập ngày sinh, xuất tuổi

Fri May 25, 2018 9:17 am
Mở Visual Studio lên, chọn File > New > Project


[Workflow] Nhập ngày sinh, xuất tuổi Picture1


Trong cửa sổ New Project, chọn Installed > Visual C# > Workflow > Activity Library


[Workflow] Nhập ngày sinh, xuất tuổi Picture2
Ở thời điểm tôi viết bài hướng dẫn này, framework nên sử dụng là 4.5.2.


Chuột phải vào Project > Manage NuGet Packages… > Cài các packages sau:


Code:
Microsoft.Xrm.Sdk.2016
Microsoft.CrmSdk.Workflow


[Workflow] Nhập ngày sinh, xuất tuổi Picture3
Ở thời điểm tôi viết bài hướng dẫn này, Microsoft.Xrm.Sdk phiên bản 2016
Phiên bản Microsoft.CrmSdk.Workflow nên gần với Microsoft.Xrm.Sdk, ví dụ, cùng là 8.x


Chuột phải vào Project > Add > Class…


[Workflow] Nhập ngày sinh, xuất tuổi Picture4
Ở đây, tôi đặt tên class là Workflow


Thay đổi toàn bộ code thành:


Code:
using System;
using Microsoft.Xrm.Sdk;
using System.Activities;
using Microsoft.Xrm.Sdk.Workflow;

namespace Demo_Workflow
{
    public class Workflow : CodeActivity
    {
        [Input("Date of Birth")]
        public InArgument<DateTime> new_dob { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            ITracingService tracingService = (ITracingService)context.GetExtension<ITracingService>();
            IWorkflowContext workflowContext = (IWorkflowContext)context.GetExtension<IWorkflowContext>();
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)context.GetExtension<IOrganizationServiceFactory>();
            IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);

            DateTime dtDOB = new_dob.Get(context);
            int CalculateAge = Convert.ToInt32(DateTime.Now.Subtract(dtDOB).TotalDays) / 365;
            new_age.Set(context, CalculateAge);
        }
        [Output("Age")]
        public OutArgument<Int32> new_age { get; set; }
    }
}


[Workflow] Nhập ngày sinh, xuất tuổi Picture5


Trong đó:

Microsoft.Xrm.SdkMicrosoft.Xrm.Sdk.Workflow là 2 packages tôi vừa add vào ở phía trên,
new_dobnew_age là tên field tôi sẽ demo


[Workflow] Nhập ngày sinh, xuất tuổi Picture6


Chuột phải vào Project > Properties


[Workflow] Nhập ngày sinh, xuất tuổi Picture7


Chọn Signing > Tick chọn Sign the assembly > Trong phần Choose a strong name key file chọn <New…>


[Workflow] Nhập ngày sinh, xuất tuổi Picture8


Đặt tên Key file name


[Workflow] Nhập ngày sinh, xuất tuổi Picture9


Save all

Chuột phải Project > Build


[Workflow] Nhập ngày sinh, xuất tuổi Picture10


Build thành công


[Workflow] Nhập ngày sinh, xuất tuổi Picture11


Mở Plugin Registration Tool lên > Create New Connection




Điền đầy đủ thông tin:
User Name: email bạn đăng nhập
Password: pass bạn đăng nhập
Tool sẽ tự động dựa vào username của bạn để chuyển đến trang dynamics tương ứng


[Workflow] Nhập ngày sinh, xuất tuổi Picture13


Chọn Register > Register New Assembly


[Workflow] Nhập ngày sinh, xuất tuổi Picture14


Chọn … > chọn đường dẫn đến file <tenproject>.dll


[Workflow] Nhập ngày sinh, xuất tuổi Picture15
Workflow là tên solution, ở dự án này sẽ là Demo_Workflow


Chọn Register Selected Plugins


[Workflow] Nhập ngày sinh, xuất tuổi Picture16


Thành công


[Workflow] Nhập ngày sinh, xuất tuổi Picture17
Workflow là tên solution, Caculated_Age là tên project, của mình sẽ là Demo_Workflow.Workflow


[Workflow] Nhập ngày sinh, xuất tuổi Picture18
Trong solution, chọn Components > Processes > New


Đặt tên Process name > Chọn kiểu Category là Workflow > Chọn Entity


[Workflow] Nhập ngày sinh, xuất tuổi Picture19


[Workflow] Nhập ngày sinh, xuất tuổi Picture20
Trong cửa sổ: Process – Demo_WF chọn Convert to a real-time workflow


[Workflow] Nhập ngày sinh, xuất tuổi Picture22
Bạn chọn như trong hình, với Record fieds change chọn Date of Birth.


Chọn Add Step > Demo_Workflow (1.0.0.0) > Demo_Workflow.Workflow


[Workflow] Nhập ngày sinh, xuất tuổi Picture23


Đặt tên Step là getDOB, sau đó chọn Set Properties.


[Workflow] Nhập ngày sinh, xuất tuổi Picture24


Chọn Look for là Account (tên entity), Day of Birth (tên field) > Add > OK


[Workflow] Nhập ngày sinh, xuất tuổi Picture25


Thành phẩm:


[Workflow] Nhập ngày sinh, xuất tuổi Picture30231c23614d27c7f3


Chọn Add Step > Update Record


[Workflow] Nhập ngày sinh, xuất tuổi Picture26


Đặt tên setAge > Set Properties


[Workflow] Nhập ngày sinh, xuất tuổi Picture27


Gán DOB và Age vào Form (trong cửa sổ Solution, chọn Entity Acount, chọn Form)

Chọn Look for là tên step bước trên (getDOB), sau đó Add và OK


[Workflow] Nhập ngày sinh, xuất tuổi Picture28


[Workflow] Nhập ngày sinh, xuất tuổi Picture29
Sau đó bạn chọn Activate là hoàn tất


Được sửa bởi Admin ngày Fri May 25, 2018 9:21 am; sửa lần 1.
avatar
Admin
Admin
Tổng số bài gửi : 1562
Join date : 20/05/2018
https://thangcrm2.forumvi.com

[Workflow] Nhập ngày sinh, xuất tuổi Empty Re: [Workflow] Nhập ngày sinh, xuất tuổi

Fri May 25, 2018 9:20 am
Ở phần chọn Convert to a real-time workflow, nếu bạn không chọn, dữ liệu chỉ được thêm vào ở phía server mà không hiện lên phía client, đồng nghĩa khi bạn mở view lên 1 lần nữa nó mới hiện (không đồng bộ giữa server và client).
Về Đầu Trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết