You need to sign in to do that
Don't have an account?

Can any one help me to solve this problem
First i create a button in campaign and if click this button. campaign members contact and lead ownership to be changed as campaign owner name....
Thi is my code can any one tell me where did i make a mistake..
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
var url = parent.location.href;
var AccId = "{!Account.Id}";
var CampId ="{!Campaign.Id}";
var CampOwnerId= "{!Campaign.OwnerId}";
var updateRecords = [];
var update_Cons = new sforce.SObject("Contact");
var result = sforce.connection.query("SELECT Id, Contact.OwnerId, Lead.OwnerId, campaignid FROM CampaignMember WHERE campaignid = '"+CampId +"'");
var recs = result.getArray("records");
if(confirm("Are you sure you want to update the name on related contacts and leads?") )
{
for(var i = 0; i < recs.length; i++)
{
recs[i].Contact.OwnerId= CampOwnerId;
recs[i].Lead.OwnerId= CampOwnerId;
updateRecords.push(recs[i]);
}
var result = sforce.connection.update(updateRecords);
parent.location.href = url;
if(result[0].success == 'true')
{
alert("Related contacts and leads updated successfully");
}
else
{
alert("Failed to update the related contacts and leads");
}
}
Try making this changes to your code, Hope this helps!
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
var url = parent.location.href;
var AccId = "{!Account.Id}";
var CampId ="{!Campaign.Id}";
var CampOwnerId= "{!Campaign.OwnerId}";
var updateRecords = [];//Array to hold records to update
var updateRecords = new Array();
var update_Cons = new sforce.SObject("Contact");
var result = sforce.connection.query("SELECT Id, ContactId, LeadId, Contact.OwnerId, Lead.OwnerId, campaignid FROM CampaignMember WHERE campaignid = '"+CampId +"'");
var recs = result.getArray("records");
if(confirm("Are you sure you want to update the name on related contacts and leads?") )
{
for(var i = 0; i < recs.length; i++)
{
recs[i].Contact.OwnerId= CampOwnerId;recs[i].Lead.OwnerId= CampOwnerId;updateRecords.push(recs[i]);//If the CampaignMember is a Contact, update the contact record
if(recs[i].ContactId != null){
var contact = new sforce.SObject("Contact");
contact .OwnerId= CampOwnerId;
contact .Id = recs[i].ContactId;
updateRecords.push(contact);
//If the CampaignMember is a Lead, update the leadrecord
}else if(recs[i].LeadId != null){
var lead= new sforce.SObject("Lead");
lead.OwnerId= CampOwnerId;
lead.Id = recs[i].LeadId;
updateRecords.push(lead);
}//End If
}
var result = sforce.connection.update(updateRecords);
parent.location.href = url;if(result[0].success == 'true')
{
alert("Related contacts and leads updated successfully");
}
else
{
alert("Failed to update the related contacts and leads");
}
parent.location.href = url;
}
All Answers
Try making this changes to your code, Hope this helps!
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
var url = parent.location.href;
var AccId = "{!Account.Id}";
var CampId ="{!Campaign.Id}";
var CampOwnerId= "{!Campaign.OwnerId}";
var updateRecords = [];//Array to hold records to update
var updateRecords = new Array();
var update_Cons = new sforce.SObject("Contact");
var result = sforce.connection.query("SELECT Id, ContactId, LeadId, Contact.OwnerId, Lead.OwnerId, campaignid FROM CampaignMember WHERE campaignid = '"+CampId +"'");
var recs = result.getArray("records");
if(confirm("Are you sure you want to update the name on related contacts and leads?") )
{
for(var i = 0; i < recs.length; i++)
{
recs[i].Contact.OwnerId= CampOwnerId;recs[i].Lead.OwnerId= CampOwnerId;updateRecords.push(recs[i]);//If the CampaignMember is a Contact, update the contact record
if(recs[i].ContactId != null){
var contact = new sforce.SObject("Contact");
contact .OwnerId= CampOwnerId;
contact .Id = recs[i].ContactId;
updateRecords.push(contact);
//If the CampaignMember is a Lead, update the leadrecord
}else if(recs[i].LeadId != null){
var lead= new sforce.SObject("Lead");
lead.OwnerId= CampOwnerId;
lead.Id = recs[i].LeadId;
updateRecords.push(lead);
}//End If
}
var result = sforce.connection.update(updateRecords);
parent.location.href = url;if(result[0].success == 'true')
{
alert("Related contacts and leads updated successfully");
}
else
{
alert("Failed to update the related contacts and leads");
}
parent.location.href = url;
}
Its Working......... Thank you So much................