CRM: Javascript to Set Multiple Email Addresses when Replying to an Email - javascript

On CRM 2013 I'm trying to insert multiple participants into the email "to" field when users click on "Reply All". However I need to remove certain email addresses from the To line. So I created an array to loop and get all the email addresses except the one that needs to be removed.
However the problem here is that it only works if there is only one participant left after removing the unwanted participants. If there are two or more participants the script will not populate any participants at all.
Is there a way to populate multiple email participants? Or is there a better approach than what I'm trying to do here?
Here's my code:
var toParty = Xrm.Page.getAttribute("to").getValue();
var partyListArray = new Array();
for (var indxAttendees = 0; indxAttendees < toParty.length; indxAttendees++) {
// using oData to get participant email address
var email = getParticipantEmail(
toParty[indxAttendees].entityType,
toParty[indxAttendees].id
);
if (email != "test#test.com") {
partyListArray[indxAttendees] = new Object();
partyListArray[indxAttendees].id = toParty[indxAttendees].id;
partyListArray[indxAttendees].name = toParty[indxAttendees].name;
partyListArray[indxAttendees].entityType = toParty[indxAttendees].entityType;
}
}
Xrm.Page.getAttribute("to").setValue(null);
Xrm.Page.getAttribute("to").setValue(partyListArray);

Instead of creating a whole new array, you can delete what you want from the array itself. Try this:
var emails = [{email: "add1#domain.com"}, {email: "add2#domain.com"}, {email: "address#toBe.Removed"}, {email: "add3#domain.com"}, {email: "add4#domain.com"}];
var removeIndex = emails.map(function(item) { return item.email; }).indexOf("address#toBe.Removed");
removeIndex > -1 && emails.splice(removeIndex, 1);

Related

Multiple emails with "to" and "CC" Alfresco 5.2

We have a question for Alfresco 5.2
Using javascript (API) we want to send some emails for a CronJobs that we are developing.
We have seen that if we use "mail.parameters.to_many" we cannot send "CC".
We need to send
To: "email1#xxx.com", "email2#xxx.com"
cc: "email3#xxx.com"
If we use:
mail.parameters.to = "email1#xxx.com"
mail.parameters.cc = "email3#xxx.com"
we correctly receive the "to" and the "CC"
But if we use:
mail.parameters.to_many = ["email1#xxx.com", "email2#xxx.com"]
mail.parameters.cc = "email3#xxx.com"
We received in the "to" "email1#xxx.com", "email2#xxx.com" but "CC" empty.
How can you send multiple emails "to" with "CC"?
Example with "to" and "CC":
var subj= "prueva de multiples";
var mail = actions.create("mail");
mail.parameters.to ="email1#xxx.com";
mail.parameters.cc = "email3#xxx.com";
mail.parameters.subject = subj;
mail.parameters.from = "serverMail.com";
mail.parameters.text = "This test is OK";
mail.executeAsynchronously(node);
(Works correctlym It works fine, but a "to" and a "CC")
Example 2:
var subj= "prueva de multiples";
var mail = actions.create("mail");
var emails = ["email1#xxx.com","email2#xxx.com","email3#xxx.com"];
mail.parameters.to_many = emails;
mail.parameters.cc = ""email4CC#xxx.com";
mail.parameters.subject = subj;
mail.parameters.from = "serverMail.com";
mail.parameters.text = "This test is not Ok";
mail.executeAsynchronously(node);
(Sends the "to_many", but the "CC" does not send it)
We want to send multiple emails in "to" and multiple in "CC".
The most important thing for us is to be able to send one "to" and multiple "CC"
Thanks guys
I looked at the source code for the MailActionExecuter and it looks to me like the CC param is only used if the TO is set (not the TO_MANY):
// set recipient
String to = (String)ruleAction.getParameterValue(PARAM_TO);
String toRecipients = null;
if (to != null && to.length() != 0)
{
messageRef[0].setTo(to);
toRecipients = to;
// Note: there is no validation on the username to check that it actually is an email address.
// TODO Fix this.
Serializable ccValue = (String)ruleAction.getParameterValue(PARAM_CC);
Based on that, I don't think you can use TO_MANY in combination with CC.
You can always just write your own action and have it work exactly like you need it to. You might start with the MailActionExecuter source to save some time.

Find a string within a jQuery array that has multiple elements per object

I've got an array of employee's and assigned to each employee are a few elements.
Sample of array below:
var employees = [
{"name":"Johhny Test","salary":"1","email":"abc#123.com"},
{"name":"Mike Milbury","salary":"10","email":"184895#hdsjhfs.com"}
];
I've got a means of gathering the employee's last name and I'm storing it in a variable. I'd like to be able to search for the indexOf the last name housed in this variable so that I know at which position within the array that match is made.
So for instance, this array could be 100 items in size. Ideally I want to know that someone with the last name of "johnson" is in position 50 of this array. That way, I can go in and get the salary and email associated with their record from position 50 in the array.
The code I've got so far is this:
var lname = "Test";
var lnameMatch = employees.indexOf(lname);
console.log(lnameMatch);
This isn't working though - my console is logging -1, suggesting that it doesn't exist in the array. Is there a way that I can specify a element of that array to search against?
Almost like employees(name).indexOf(lname) where it is searching against the name element?
Or am I going about this all wrong and there is perhaps an easier less messy way to accomplish this?
You can use .findIndex:
const employees = [
{"name":"Johhny Test","salary":"1","email":"abc#123.com"},
{"name":"Mike Milbury","salary":"10","email":"184895#hdsjhfs.com"}
];
const lastName = "Test";
const index = employees.findIndex(employee => {
const { name = '' } = employee;
const nameParts = name.split(' ');
const lname = nameParts[nameParts.length-1];
return lastName === lname;
});
console.log(index);
console.log(employees[index]);

compare value from arrayvalues in javascript

I have requirement in ServiceNow application that user has manager and that manager has another manager so i'am getting New Manager ( field in table) managers and storing in array.
I am trying to match that If login user ( user logged in ServiceNow) is one of the New Manager managers
Script:
var NewReportsTo ='';
NewReportsTo = current.variables.new_manager;
var item = [];
for ( var i =1 ; i<=6;i++){
NewReportsTo = NewReportsTo.manager;
var Newmanager = NewReportsTo.toString();
item.push(Newmanager);
//var list = item.split(',');
gs.log('Check ACL:'+Newmanager, 'TEST ACL TCR');
if(item.indexOf(gs.getUserID())){
gs.log('Check ACL:'+current.sys_id, 'TEST ACL INSIDE TCR');
answer = true;
}
}
getUserID() is the login user and item array has all the manager for New manager field
Array.indexOf() returns the first index at which the given item can be found in the array, or -1 if it's not found.
You should change your code to:
if(item.indexOf(gs.getUserID() != -1)){
...
}

Getting Double response in socket.io on emit

I am making a chat application in node js and socket.io . What I have don is, when a user click a link to go to the inbox of a user he or she chatted with, on that click even I send the id of that current user who clicked target chat to open its messages and the id of the target user who he or she chatted with through these two ids I am retrieving the messages of every user. But the problem is, on the first click the messages retrieved is the array of objects which works fine, but when the same target has been clicked again the messages array retrieved is twice in number and each time it is clicked it double the number of arrays and retrieves them.
TO Better understand , have a look at the snap shot. You can see that on each click the arrays retrieved are double in number.
My Code - NodeJs One
socket.on("get_user_messages",(data)=>{
console.log(data);
var GET_CLICKED_USER_MESS = "SELECT mess_txt,mess_time,mess_to FROM (select mess_to as user_id,mess_txt,mess_time,mess_id,mess_to from messages where mess_from = '"+data.active+"' AND mess_to='"+data.clicked+"' UNION select mess_to as user_id,mess_txt,mess_time,mess_id,mess_to from messages where mess_from = '"+data.clicked+"' AND mess_to='"+data.active+"' ORDER BY mess_id ASC) sq join users on users.id = sq.user_id";
con.query(GET_CLICKED_USER_MESS,(err,res)=>{
if(err) throw err;
socket.emit("get_ret_messages",result);
})
});
JavaScript where it is received
var d = document, _id, mess, messs, chatBody = document.getElementsByClassName("indChatBody")[0], result;
d.addEventListener("click",(e)=>{
e.preventDefault();
var event = e.target;
console.log("clicked");
if(event.parentNode.className=="indUser" || event.parentNode.className=="ind_user_messages"){
if(event.parentNode.className=="ind_user_messages"){
_id = event.parentNode.getAttribute("data-id");
} else if(event.parentNode.className=="indUser"){
_id = event.getAttribute("data-id");
}
socket.emit("get_user_messages",{clicked:_id,active:data.active_user});
socket.on("get_ret_messages",(data)=>{
result = [];
/*for(let i = 0; i<data.length;i++){
if(data[i].mess_to == data.active_user){
mess = '<span class="mess fromMess"><div class="textTime"><span class="messTime">'+data[i].mess_time+'</span></div><p class="senderText">'+data[i].mess_txt+'</p></span>';
chatBody.insertAdjacentHTML("beforeend",mess);
}
messs = '<span class="mess toMess"><div class="textTime"><span class="messTime">'+data[i].mess_time+'</span></div><p class="senderText">'+data[i].mess_txt+'</p></span>';
chatBody.insertAdjacentHTML("beforeend",messs);
}*/
result = data;
console.log(result);
});
}
})
Your bug is that you are adding a new socket listener each time you click.
You need to move the socket listener outside of your click listener:
d.addEventListener("click",(e)=>{
};
socket.on("get_ret_messages",(data)=>{}

Not able to get objectId in query.containedIn query with parse?

I am using parse.com as my back end.
I am using containedIn query to retrieve multiple user rows from parse "User" table.
My problem is that I am not able to get user's object Id in the success block
following is my code:
r_userIdList= ['PzpdRdKTVZ','PoJdRdKTVU','fvdRdKTVX'];
var query = new Parse.Query("_User");
query.containedIn("objectId", r_userIdList); // returns multiple rows
query.find({
success: function(r_uobject) {
var userObjId ,fName,lName,full_Name="Unknown User";
for(var i =0; i<r_uobject.length;i++){
var r_obj = r_uobject[i];
uID = r_obj.get('objectId');//not working
var u = uID.id;//not working
fName = r_obj.get('firstName');
lName = r_obj.get('lastName');
full_Name = firstName+' '+lastName;
r_userIdList[u].fullName = full_Name; //not working
}
}
});
Once I enter in the success block, I am unable to identify that which user's data has been retrieved.
Actually I need user's ObjectId because I have array called r_userIdList in which I need to store the firstName and LastName of user as Object.
IDs can usually be accessed with .id - in your case r_obj.id. Your uID.id won't work as uID is itself going to be undefined.
You also need to check that you are getting any results back at all: does r_userIdList have a length?

Categories

Resources