function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SpunkySpunky 

Modify Scontrol to query for account team member

With borrowed code from the discussions board (thank god for this discussion forum),after few days of trial and error, I was able to replicate an scontrol which acts as a list button on the campaign related list :campaign members

 

Presently, this scontrol allows me to mass create tasks for campaign member owners(i.e contact owners)

I've only tried it on a campaign with 15 contacts so far.

 

Issues

  1. I also need to assign tasks to Account Team members where the Team Member Role is "Inside Sales"Is it possible to query the account team member within this scontrol and if so, how would i go about doing this?
  2. The current method displays a pop-up alert which confirms the task creation for each task . How would I go about changing this to a single popup with a count of tasks created?

Posting my code - no laughing from the experts please!

I'm not a programmer and wish I didn't have to do this:(

 

Any help would be greatly appreciated

 



{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}; 
var memberIDs = {!GETRECORDIDS( $ObjectType.CampaignMember)}; 
var tsks= []; 
var campId="{!Campaign.Id}"; 
var campName="{!Campaign.Name}"; 
var campowner="{!CampaignMember.ContactOwner__c}"; 

if (!memberIDs.length) { 
alert("Please select at least one contact to enroll."); 

} else { 
for (var i=0; i<memberIDs.length; i++) { 
var result = sforce.connection.retrieve("Account_Name__c,ID,contactowner__c,contactId", "CampaignMember", [memberIDs[i]]); 
if (result[0] == null) throw "retrive failed"; 
if(result[0].ContactId==null)throw "You have selected a lead."; 
var conId=result[0].ContactId; 
var result2 =sforce.connection.retrieve("ID,AccountID", "Contact", [conId]); 

var tsk = new sforce.SObject("Task"); 
tsk.Subject = campName + " Campaign: " + result[0].Account_Name__c; 
tsk.Description=result2[0].AccountId; 
tsk.WhatId=campId; 
tsk.Status="Not Started"; 
tsk.OwnerId=campowner;
tsk.priority="Normal"
tsk.WhoId=conId; 
tsks.push(tsk); 
} 

var result = sforce.connection.create(tsks); 

for (var i=0; i<result.length; i++) { 
if (result[i].getBoolean("success")) { 
alert("new Task created with id " + result[i].id); 
} else { 
alert("failed to create Task" + result[i]); 
}} 
}

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
{!RequireScript('/soap/ajax/22.0/connection.js')}

var insideSalesQuery = sforce.connection.query(
   'select id,userid from opportunityteammember where opportunityid = \'{!Opportunity.Id}\' and TeamMemberRole = \'Inside Sales\''
)

var insideSalesTeamMembers = insideSalesQuery.getArray('records')

var tasksToCreate = []

for(x = 0; x < insideSalesQuery.getInt('size'); x++) {
  var task = new sforce.SObject('Task')
  task['Subject'] = 'New Assignment'
  task['OwnerId'] = insideSalesTeamMembers[x].UserId
  task['ActivityDate'] = new Date('{!TODAY()+3}')
  task['WhatId'] = '{!Opportunity.Id}'
  task['Status'] = 'Not Started'
  tasksToCreate.push(task)
}

var insertResults

if(tasksToCreate.length > 0)
    insertResults = sforce.connection.create(tasksToCreate)

var successes = 0
var errors = 0

for(var x in insertResults)
  if(insertResults[x].getBoolean('success'))
    successes++
  else
    errors++

alert('Tasks assigned successfully: ' + successes + '\n' + 'Tasks not created: ' + errors)

Here is my version of what this code might look like. Note that I just used generic fields so I could test this in my organization. You would replace the values you need with whatever you need to make it work for you.

 

Note that I make a date out of a string value (see the new Date() call?). You may need to convert some of your data (notably non-string values, such as numbers and dates) to make sure the call goes through okay. JavaScript is loosely typed, and it's often feasible to get the wrong type in the mix from the API point of view, so always try to make those values explicit. Use parseInt, parseFloat, and Date to coerce values to the correct type.

All Answers

sfdcfoxsfdcfox

I'm not going to laugh at your code, as you requested. However, to be fair to you and your users, I should advise you that you do have some poor coding in here.

 

1) Your retrive function should be attempting to retrieve all records at once, instead of one at a time. You will run up your API limits very quickly like this, and slow down your script immensely.

 

2) Your task creation part needs to be inserting batches of no more than 200. It is the recommended maximum batch size of a call.

 

3) To avoid accidental (or even malicious) script breakage, make sure that you use the JSENCODE function on fields that could potentially have quotation marks, apostrophies, or back-slashes in them (basically, any field that a user can fill in manually). Check out the Help & Training section on JSENCODE and JSINHTMLENCODE.

 

Those aside, the answers to your issues are:

 

1) Yes. You can perform this query, but it will take an extra step because you need to first retrieve the AccountID for the involved contacts, then construct a query to find all matching AccountTeamMember records.

 

2). 

var successes = 0
var errors = 0
for(var x in result)
  if(result[x].getBoolean('success'))
    successes++
  else
    errors++

alert("Tasks created successfully: " + successes + "\nTasks with errors: " + errors)

I will bookmark this post, as I am about to log out for the night, and tomorrow I will see if I could not write up some code that you could use if you are still stuck.

SpunkySpunky

Thank you for restraining your laughter:) I agree and know this is poor code and will take a look at your recommended help sites to see if I can rewrite this to upgrade from poor to average. Thanks so much for your feedback as always. It's truly appreciated expecially for those who are in the same boat as I am...forced into developing!

SpunkySpunky

I'm still stuck. Please help!

sfdcfoxsfdcfox

Spunky,

I have heard your cry for help, so fear not. I'm on limited Internet access until Friday, but let me see if I can't whip up a serviceable example here in the next few minutes while I have the time. I have not forgotten about you, just life has done its usual job of interfering with my work.


sfdcfoxsfdcfox
{!RequireScript('/soap/ajax/22.0/connection.js')}

var insideSalesQuery = sforce.connection.query(
   'select id,userid from opportunityteammember where opportunityid = \'{!Opportunity.Id}\' and TeamMemberRole = \'Inside Sales\''
)

var insideSalesTeamMembers = insideSalesQuery.getArray('records')

var tasksToCreate = []

for(x = 0; x < insideSalesQuery.getInt('size'); x++) {
  var task = new sforce.SObject('Task')
  task['Subject'] = 'New Assignment'
  task['OwnerId'] = insideSalesTeamMembers[x].UserId
  task['ActivityDate'] = new Date('{!TODAY()+3}')
  task['WhatId'] = '{!Opportunity.Id}'
  task['Status'] = 'Not Started'
  tasksToCreate.push(task)
}

var insertResults

if(tasksToCreate.length > 0)
    insertResults = sforce.connection.create(tasksToCreate)

var successes = 0
var errors = 0

for(var x in insertResults)
  if(insertResults[x].getBoolean('success'))
    successes++
  else
    errors++

alert('Tasks assigned successfully: ' + successes + '\n' + 'Tasks not created: ' + errors)

Here is my version of what this code might look like. Note that I just used generic fields so I could test this in my organization. You would replace the values you need with whatever you need to make it work for you.

 

Note that I make a date out of a string value (see the new Date() call?). You may need to convert some of your data (notably non-string values, such as numbers and dates) to make sure the call goes through okay. JavaScript is loosely typed, and it's often feasible to get the wrong type in the mix from the API point of view, so always try to make those values explicit. Use parseInt, parseFloat, and Date to coerce values to the correct type.

This was selected as the best answer