I need to be able to return a few variables based on a code. If code A then return these. If Code B then return these. In no code, return these.
This is what I have so far.
if (inputData.code === 'WSDCD-D2DUK') {
output = 'Company A';
} else if (inputData.code === '6P1CX-5U2TY'){
output = 'Company B';
}
else {
output = 'Not Avaliable';
}
return {result: output};
And what I need is something like this:
if (inputData.code === 'WSDCD-D2DUK') {
output = 'Company A';
course = 'ABC'
} else if (inputData.code === '6P1CX-5U2TY'){
output = 'Company B';
course = 'XYZ'
}
else {
output = 'Not Avaliable';
course = 'Not in one';
}
return {result: output, course};
Looks like you did not define the course variable. However, output doesn't need to be defined as it is available in the Code by Zapier scope.
I've modified the code a slight bit to reflect your use-case.
var inputData = {'code': 'D2DUK'}
//Remove the line above before pasting in the Code step.
//You will need to configure it in the Zap.
//Defining the variables here.
let course = '';
let company = '';
if (inputData.code === 'WSDCD-D2DUK') {
company = 'Company A';
course = 'ABC'
} else if (inputData.code === '6P1CX-5U2TY'){
company = 'Company B';
course = 'XYZ'
}
else {
company = 'Not Avaliable';
course = 'Not in one';
}
//Returning the JSON object with the keys company and course.
return {company, course};
Related
final_map = ["/7amd64-Aug2022.1":"2022-08-09","/7amd64-Oct2022.1":"2022-10-12","/7":"2022-11-08","/7amd64-Jul2022.1":"2022-07-12","/7amd64":"2022-11-08","/7amd64-June2022.1":"2022-06-14","/7amd64-beta":"2022-11-08","/7amd64-Sep2022.1":"2022-09-14","/7amd64-Nov2022.1":"2022-11-08","/_uploads":"2022-11-08"]
Jenkins Pipeline (below is the code I have, which is not working)
result = final_map.sort { a,b -> a.value <=> b.value }
echo "Output: ${result}"
Expecting to sort the map with date (value).
You can use a custom comparator for this. Check the following Groovy code.
final_map = ["/7amd64-Aug2022.1":"2022-08-09","/7amd64-Oct2022.1":"2022-10-12","/7":"2022-11-08","/7amd64-Jul2022.1":"2022-07-12","/7amd64":"2022-11-08","/7amd64-June2022.1":"2022-06-14","/7amd64-beta":"2022-11-08","/7amd64-Sep2022.1":"2022-09-14","/7amd64-Nov2022.1":"2022-11-08","/_uploads":"2022-11-08"]
final_map.sort { s1, s2 ->
def s1Date = new Date(s1.value.replace('-', '/'))
def s2Date = new Date(s2.value.replace('-', '/'))
if( s1Date.before(s2Date)) {
return -1
} else {
return 1
}
}
println final_map
return{
initime = 1;
isneed= true; -- need to modify
testfn=function()
isneed = false; ---how to modify the "isneed" value?
end
}
i wanna modify the isneed's value,i have try like this
local testdata;
testdata={
initime = 1;
isneed= true;
testfn=function()
testdata.isneed = false;
end
}
return testdata;
but the code that i dont want,i think have another way to set the value.
Building on #luther's comment, the code you have in your second example should work.
local testdata = {
initime = 1,
isneed = true,
testfn = function()
testdata.isneed = false
return
end
}
print(testdata.isneed)
testdata.testfn()
print(test.data.isneed)
This should output the following:
true
false
Alternatively, if you wanted to get a little fancier, you could use a metatable to overload the call operator for your table testdata:
local testdata = {
initime = 1,
isneed = true,
testfn = function()
testdata.isneed = false
return
end
}
testdata = setmetatable(testdata, {
__call = function(self)
return self.testfn()
end
})
print(testdata.isneed)
testdata()
print(testdata.isneed)
This example's output is equivalent to the above output. Depending on what exactly you wish to accomplish with your code, overloading the call operator with a metatable could offer you some more flexibility. Using this approach, you could change your code slightly like this, making use of the self parameter in the __call function:
local testdata = setmetatable({initime = 1, isneed = true}, {
__call = function(self)
self.isneed = false
return
end
})
print(testdata.isneed)
testdata()
print(testdata.isneed)
This will produce the same output as the first example.
So I have tried this a number of ways but every time I have someone score above 80 it returns Failed and not Passed...
First Way:
if (inputData.score >= '80') {
return {result: 'Passed'};
} else {
return {result: 'Failed'};
}
Second Way:
if (inputData.score >= '80') {
output = 'Passed';
} else {
output = 'Failed';
}
return {result: output};
However, if someone gets 80 it will return a Pass... I am at a loss.
Tried removing single-quotes from '80'? You are checking a string against greater-than operator.
if (inputData.score >= 80) {
return {result: 'Passed'};
} else {
return {result: 'Failed'};
}
David here, from the Zapier Platform team.
You definitely want to be comparing numbers to numbers. As the other answerer mentions, convert your incoming score to a number and compare it to a number.
if (parseInt(inputData.score, 10) >= 80) {
return {result: 'Passed'};
} else {
return {result: 'Failed'};
}
As for which of the two return methods to use, they're equal as far as Zapier is concerned. I find the first a little cleaner, so I went with that.
I have some problems with LINQ and maby someone got answers
string[] roleNames = Roles.GetRolesForUser(currentUserName);
result = context.MenuRoles.Select(mr => new MenuGenerateViewModel
{
MenuID = mr.MenuID,
MenuNazwa = mr.Menu.MenuNazwa,
MenuKolejnosc = mr.Menu.MenuKolejnosc,
MenuStyl = mr.Menu.MenuStyl,
MenuParentID = mr.Menu.MenuParentID,
MenuActive = mr.Menu.MenuActive,
MenuActionName = mr.Menu.MenuAction.MenuActionName,
MenuControlName = mr.Menu.MenuControl.MenuControlName,
RoleName = mr.Role.RoleName,
RoleID = mr.RoleID,
MenuID = mr.MenuID
})
.Where(mr => mr.MenuActive == true)
.ToList();
How to take only compare string[] roleNames and return only if match. Problem alwais is when user is in the 2 or more roles.
Tx for answers
If I understand what you are asking for, add a second condition to your Where clause:
.Where(mr => mr.MenuActive && roleNames.Contains(mr.Role.RoleName))
You would be better off switching round your Where clause and Select for the simple reason that then you will not be retrieving from the database records which are not required.
result = context.MenuRoles.Where(mr => mr.MenuActive
&& roleNames.Contains(mr.Role.RoleName))
.Select(mr => ... )
.ToList();
This will generate a sql which only selects the necessary records, instead of selecting the whole lot and then filtering it. Try it and watch SQL profiler to see the difference (useful skill in any case when using EF)
With the help of brilliant people here, reached the target.
string[] roleNames = Roles.GetRolesForUser(currentUserName);
result = context.MenuRoles
.Where(mr => mr.Menu.MenuActive && roleNames.Contains(mr.Role.RoleName))
.Select(mr => new MenuGenerateViewModel
{
MenuID = mr.MenuID,
MenuNazwa = mr.Menu.MenuNazwa,
MenuKolejnosc = mr.Menu.MenuKolejnosc,
MenuStyl = mr.Menu.MenuStyl,
MenuParentID = mr.Menu.MenuParentID,
MenuActive = mr.Menu.MenuActive,
MenuActionName = mr.Menu.MenuAction.MenuActionName,
MenuControlName = mr.Menu.MenuControl.MenuControlName,
RoleName = mr.Role.RoleName
})
.ToList();
var userresult = context.MenuUsers
.Where(mr => mr.Menu.MenuActive && mr.User.Username == currentUserName)
.Select(mr => new MenuGenerateViewModel
{
MenuID = mr.MenuID,
MenuNazwa = mr.Menu.MenuNazwa,
MenuKolejnosc = mr.Menu.MenuKolejnosc,
MenuStyl = mr.Menu.MenuStyl,
MenuParentID = mr.Menu.MenuParentID,
MenuActive = mr.Menu.MenuActive,
MenuActionName = mr.Menu.MenuAction.MenuActionName,
MenuControlName = mr.Menu.MenuControl.MenuControlName,
Username = mr.User.Username
})
.ToList();
Here, gets all the menu to which you have the right, both through group membership and assigned directly to the menu itself.
// Kick all duplicates
var noduplicates = result.Concat(userresult)
.Distinct(new RoleMenuGenerateComparer());
Because usually we do not want duplicates in the menu so we remove them. For this to work properly we need to implement IEqualityComparer (U can read about this little bit up)
public class RoleMenuGenerateComparer : IEqualityComparer<MenuGenerateViewModel>
{
public bool Equals(MenuGenerateViewModel x, MenuGenerateViewModel y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the products' properties are equal.
return x.MenuNazwa == y.MenuNazwa && x.MenuID == y.MenuID;
}
public int GetHashCode(MenuGenerateViewModel menuGenerateViewModel)
{
if (Object.ReferenceEquals(menuGenerateViewModel, null)) return 0;
int hashMenuName = menuGenerateViewModel.MenuNazwa == null ? 0 : menuGenerateViewModel.MenuNazwa.GetHashCode();
int hashMenuID = menuGenerateViewModel.MenuID == null ? 0 : menuGenerateViewModel.MenuID.GetHashCode();
return hashMenuName ^ hashMenuID;
}
}
Of course I believe that you can optimize this code, but for the moment I have something like this.
Tx all for help.
I'm reading email headers (in Node.js, for those keeping score) and they are VARY varied. E-mail addresses in the to field look like:
"Jake Smart" <jake#smart.com>, jack#smart.com, "Development, Business" <bizdev#smart.com>
and a variety of other formats. Is there any way to parse all of this out?
Here's my first stab:
Run a split() on - to break up the different people into an array
For each item, see if there's a < or ".
If there's a <, then parse out the email
If there's a ", then parse out the name
For the name, if there's a ,, then split to get Last, First names.
If I first do a split on the ,, then the Development, Business will cause a split error. Spaces are also inconsistent. Plus, there may be more e-mail address formats that come through in headers that I haven't seen before. Is there any way (or maybe an awesome Node.js library) that will do all of this for me?
There's a npm module for this - mimelib (or mimelib-noiconv if you are on windows or don't want to compile node-iconv)
npm install mimelib-noiconv
And the usage would be:
var mimelib = require("mimelib-noiconv");
var addressStr = 'jack#smart.com, "Development, Business" <bizdev#smart.com>';
var addresses = mimelib.parseAddresses(addressStr);
console.log(addresses);
// [{ address: 'jack#smart.com', name: '' },
// { address: 'bizdev#smart.com', name: 'Development, Business' }]
The actual formatting for that is pretty complicated, but here is a regex that works. I can't promise it always will work though. https://www.rfc-editor.org/rfc/rfc2822#page-15
const str = "...";
const pat = /(?:"([^"]+)")? ?<?(.*?#[^>,]+)>?,? ?/g;
let m;
while (m = pat.exec(str)) {
const name = m[1];
const mail = m[2];
// Do whatever you need.
}
I'd try and do it all in one iteration (performance). Just threw it together (limited testing):
var header = "\"Jake Smart\" <jake#smart.com>, jack#smart.com, \"Development, Business\" <bizdev#smart.com>";
alert (header);
var info = [];
var current = [];
var state = -1;
var temp = "";
for (var i = 0; i < header.length + 1; i++) {
var c = header[i];
if (state == 0) {
if (c == "\"") {
current.push(temp);
temp = "";
state = -1;
} else {
temp += c;
}
} else if (state == 1) {
if (c == ">") {
current.push(temp);
info.push (current);
current = [];
temp = "";
state = -1;
} else {
temp += c;
}
} else {
if (c == "<"){
state = 1;
} else if (c == "\"") {
state = 0;
}
}
}
alert ("INFO: \n" + info);
For something complete, you should port this to JS: http://cpansearch.perl.org/src/RJBS/Email-Address-1.895/lib/Email/Address.pm
It gives you all the parts you need. The tricky bit is just the set of regexps at the start.