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
ASFASF 

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");
}
}

Best Answer chosen by Admin (Salesforce Developers) 
izayizay

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

izayizay

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;
}

This was selected as the best answer
Bhawani SharmaBhawani Sharma
Make sure to pull contact id and lead Id in your query and check for the nun null case before setting up ownerid.
ASFASF

Its Working......... Thank you So much................