elsa-workflows / elsa-core

A .NET workflows library
https://v3.elsaworkflows.io/
MIT License
6.4k stars 1.18k forks source link

How to update a List Variable #419

Closed hyzx86 closed 4 years ago

hyzx86 commented 4 years ago

Hi ,

My workflow requires multiple people to approve. I need to save the approval comments of the approver in a collection. I define a list to save all approval records before approval. Each approver updates this approval record variable after approval, but the workflow reports an error saying that the push method cannot be found. Is my method wrong?

image

sfmskywalker commented 4 years ago

I think you should initialize list as follows:

var list = ApproveLogList || [] (notice the double pipe instead of the single pipe).

hyzx86 commented 4 years ago

Oh, there is a mistake, but I tried other way before. I modified my code and it still throws this exception image

hyzx86 commented 4 years ago

Done , I have to convert to array to string then parse to object image

hyzx86 commented 4 years ago

🤣 still not work

image

sfmskywalker commented 4 years ago

🤦 😂 Perhaps you can share the workflow definition's JSON here so I can try it out & see what gives?

hyzx86 commented 4 years ago

Hi Sipke , Here is my workflow definition , Help check what went wrong, thanks!

{"activities":[{"id":"55117a9f-4e4d-4949-9b8c-3f7c476e2e84","type":"SetVariable","left":539,"top":35,"state":{"variableName":"Document","valueExpression":{"expression":"return lastResult().Body","syntax":"JavaScript"},"name":"","title":"保存申请信息","description":"从POST","Name":null},"blocking":false,"executed":false,"faulted":false},{"id":"c8642d44-971e-44e1-a96e-092231700434","type":"ReceiveHttpRequest","left":70,"top":22,"state":{"path":"/submitRequest","method":"POST","readContent":"true","name":"Post请求启动工作流","title":"Post提交申请","description":"","Name":null},"blocking":false,"executed":false,"faulted":false},{"id":"40f3a665-f191-47b4-880b-0ee6ce803d8f","type":"Correlate","left":1018,"top":30,"state":{"valueExpression":{"expression":"Document.requestId","syntax":"JavaScript"},"name":"","title":"保存审批ID","description":""},"blocking":false,"executed":false,"faulted":false},{"id":"4ee61100-1c94-456d-94f9-a30b7cc5e62d","type":"WriteHttpResponse","left":490,"top":1266,"state":{"statusCode":"200","content":{"expression":"return AAA","syntax":"JavaScript"},"contentType":"application/json","responseHeaders":{"expression":"","syntax":"Literal"},"name":"","title":"","description":""},"blocking":false,"executed":false,"faulted":false},{"id":"408eb1f3-dc4a-4e49-9718-79f8821728d7","type":"GetWorkflowInfo","left":489,"top":1011,"state":{"variableName":"AAA","name":"","title":"","description":""},"blocking":false,"executed":false,"faulted":false},{"id":"7722a314-1c22-4289-8f1d-fec7736755cf","type":"ReceiveHttpRequest","left":1045,"top":606,"state":{"path":"/approve","method":"POST","readContent":"true","name":"","title":"审批请求","description":""},"blocking":false,"executed":false,"faulted":false},{"id":"5aedc21c-096a-4202-86c6-c6b6e2ac8b06","type":"IfElse","left":512,"top":408,"state":{"conditionExpression":{"expression":"var temp = ApprovalLog;  return  (temp.requestId&& temp.requestId==Document.requestId)","syntax":"JavaScript"},"name":"","title":"筛选审批请求","description":"requestId is approving"},"blocking":false,"executed":false,"faulted":false},{"id":"0ea0fbe7-819c-41c7-b655-ed4819957b38","type":"WriteHttpResponse","left":543,"top":604,"state":{"statusCode":"200","content":{"expression":"var error={ sucsess:false,msg:\"bad request.\" }\r\nreturn JSON.stringify(error)","syntax":"JavaScript"},"contentType":"application/json","responseHeaders":{"expression":"","syntax":"Literal"},"name":"","title":"错误的请求信息","description":"bad request"},"blocking":false,"executed":false,"faulted":false},{"id":"39ec15bd-e187-4a58-af73-0719917c38de","type":"Finish","left":553,"top":1499,"state":{"workflowOutput":{"expression":"","syntax":"Literal"},"name":"","title":"流程结束","description":"Done"},"blocking":false,"executed":false,"faulted":false},{"id":"0aff0fd7-2726-414d-88e2-da7e104ba954","type":"SetVariable","left":809,"top":403,"state":{"variableName":"ApprovalLog","valueExpression":{"expression":"return lastResult().Body","syntax":"JavaScript"},"name":"","title":"ApprovalLog","description":"保存审批日志"},"blocking":false,"executed":false,"faulted":false},{"id":"f243d7a8-3970-429e-9af1-a25d1bfd2214","type":"IfElse","left":515,"top":743,"state":{"conditionExpression":{"expression":"Document.approvers.length == ApprovalLog.length","syntax":"JavaScript"},"name":"","title":"","description":"是否还需要继续审批"},"blocking":false,"executed":false,"faulted":false},{"id":"2a7d5a64-2d81-4200-bacf-d7f1d44b01a2","type":"SetVariable","left":67,"top":733,"state":{"variableName":"ApproveLogList","valueExpression":{"expression":"var list=ApproveLogList|[];  list.push(ApprovalLog); return list;","syntax":"JavaScript"},"name":"","title":"UpdateApproveLogList","description":"更新审批列表"},"blocking":false,"executed":false,"faulted":false},{"id":"e8e98520-a05f-4252-908a-fb6afdbef6e0","type":"SetVariable","left":1019,"top":250,"state":{"variableName":"ApproveLogList","valueExpression":{"expression":"var temp=[]; return temp;","syntax":"JavaScript"},"name":"","title":"","description":""},"blocking":false,"executed":false,"faulted":false},{"id":"a47c40f9-b20c-4c3b-93ce-c47b93119b6d","type":"SetVariable","left":52,"top":360,"state":{"variableName":"ApproveLogList","valueExpression":{"expression":"var temp=ApproveLogList||[] ;   temp.push(ApprovalLog);  return  temp;","syntax":"JavaScript"},"name":"","title":"ApproveLogList","description":""},"blocking":false,"executed":false,"faulted":false}],"connections":[{"sourceActivityId":"408eb1f3-dc4a-4e49-9718-79f8821728d7","destinationActivityId":"4ee61100-1c94-456d-94f9-a30b7cc5e62d","outcome":"Done"},{"sourceActivityId":"5aedc21c-096a-4202-86c6-c6b6e2ac8b06","destinationActivityId":"0ea0fbe7-819c-41c7-b655-ed4819957b38","outcome":"False"},{"sourceActivityId":"0ea0fbe7-819c-41c7-b655-ed4819957b38","destinationActivityId":"7722a314-1c22-4289-8f1d-fec7736755cf","outcome":"Done"},{"sourceActivityId":"4ee61100-1c94-456d-94f9-a30b7cc5e62d","destinationActivityId":"39ec15bd-e187-4a58-af73-0719917c38de","outcome":"Done"},{"sourceActivityId":"f243d7a8-3970-429e-9af1-a25d1bfd2214","destinationActivityId":"7722a314-1c22-4289-8f1d-fec7736755cf","outcome":"True"},{"sourceActivityId":"f243d7a8-3970-429e-9af1-a25d1bfd2214","destinationActivityId":"408eb1f3-dc4a-4e49-9718-79f8821728d7","outcome":"False"},{"sourceActivityId":"c8642d44-971e-44e1-a96e-092231700434","destinationActivityId":"55117a9f-4e4d-4949-9b8c-3f7c476e2e84","outcome":"Done"},{"sourceActivityId":"55117a9f-4e4d-4949-9b8c-3f7c476e2e84","destinationActivityId":"40f3a665-f191-47b4-880b-0ee6ce803d8f","outcome":"Done"},{"sourceActivityId":"7722a314-1c22-4289-8f1d-fec7736755cf","destinationActivityId":"0aff0fd7-2726-414d-88e2-da7e104ba954","outcome":"Done"},{"sourceActivityId":"2a7d5a64-2d81-4200-bacf-d7f1d44b01a2","destinationActivityId":"f243d7a8-3970-429e-9af1-a25d1bfd2214","outcome":"Done"},{"sourceActivityId":"40f3a665-f191-47b4-880b-0ee6ce803d8f","destinationActivityId":"e8e98520-a05f-4252-908a-fb6afdbef6e0","outcome":"Done"},{"sourceActivityId":"e8e98520-a05f-4252-908a-fb6afdbef6e0","destinationActivityId":"7722a314-1c22-4289-8f1d-fec7736755cf","outcome":"Done"},{"sourceActivityId":"0aff0fd7-2726-414d-88e2-da7e104ba954","destinationActivityId":"5aedc21c-096a-4202-86c6-c6b6e2ac8b06","outcome":"Done"},{"sourceActivityId":"5aedc21c-096a-4202-86c6-c6b6e2ac8b06","destinationActivityId":"a47c40f9-b20c-4c3b-93ce-c47b93119b6d","outcome":"True"},{"sourceActivityId":"a47c40f9-b20c-4c3b-93ce-c47b93119b6d","destinationActivityId":"2a7d5a64-2d81-4200-bacf-d7f1d44b01a2","outcome":"Done"}]}

Start workflow request data: POST to /submitRequest

{
    "submitType": "Monthly",
    "productType": "Resin",
    "clientUrl": "http://xxxx",
    "sender": {
        "userName": "zhangsan",
        "email": "yanzhong.han@jizhousoft.com"
    },
    "requestId": 1,
    "approvers": [
        {
            "userName": "ZhangSan",
            "email": "yanzhong.han@jizhousoft.com"
        },
        {
            "userName": "LiSi",
            "email": "yanzhong.han@jizhousoft.com"
        }
    ],
    "apiBaseUrl": "http://localhost:44388/"
}

Approve request data: POST to:http://localhost:44388/approve

{ "userName":"sdsdfsdf", "remark":"Accept,通过!","approvalStatus":true,"requestId":1}
hyzx86 commented 4 years ago

Done, after I change the code to this:

var arr= []; if( typeof ApproveLogList == "undefined") { arr=ApproveLogList }; arr.push(ApprovalLog); return arr;