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
Dale WorthingtonDale Worthington 

A button to update a record based on a custom field of the owner.

Hi All

Full disclosure: Novice at JS.

Im trying to make a button which will check the Region of the Owner and based on that change the Status of the Lead and Change the owner. 
Ive done simmilar things before an im trying to paste it all together but im stuck at the first step! Trying to check the region of the owner.

Is there a way that i can do this? I thoguht just Lead.OwnerId.Region__c would work but clearly not. 

As for the rest of the button im sure there are tons of errors but this is where i need to started i think. 
 
{!REQUIRESCRIPT("/soap/ajax/31.0/connection.js")}

//Define the object
var leadObj = new sforce.SObject("Lead");

//Define the IDs of things
leadObj.Id = '{!Lead.Id}';

// IF APAC
if(('{!Lead.OwnerId.Region__c}' == 'APAC')
	,leadObj.Status = 'Marketing Qualified';  
	leadObj.Owner = '00520000003YH58'; 
	,
	// IF EMEA
	if(('{!Lead.OwnerId.Region__c}' == 'EMEA')
		,leadObj.Status = 'Marketing Qualified';  
		leadObj.Owner = '00520000003YH58'; 
		,
		// IF Americas
		if(('{!Lead.OwnerId.Region__c}' == 'Americas')
			,leadObj.Status = 'Marketing Qualified';  
			leadObj.Owner = '00520000003YH58'; 
			,enter your else-expression here (only for the last one) - 3)))
as 

//update method takes an array of Leads; init to 1 element - 'leadObj'
var result = sforce.connection.update([handObj]);

if (result[0].success == 'false') {
alert(result[0].errors.message);
}

else {
location.reload(true); /* redisplay the detail page */
}

 
Best Answer chosen by Dale Worthington
Pankaj_GanwaniPankaj_Ganwani
Hi Dale,

I missed to change the object name in second SOQL. Please use below mentioned code:
 
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")}

//Define the object
var leadObj = new sforce.SObject("Lead");

//Define the IDs of things
leadObj.Id = '{!Lead.Id}';

var result = sforce.connection.query(
"SELECT OwnerId from Lead WHERE Id ='" + leadObj.Id + "'");
var records = result.getArray("records");

var ownerRegion = sforce.connection.query(
"SELECT Region__c from User WHERE Id ='" + records[0].OwnerId + "'");
var userrecords = ownerRegion.getArray("records");

if(records.length > 0)
{


// IF APAC
if(userrecords[0].Region__c == 'APAC')
{
  leadObj.Status = 'Some Status'; 
  leadObj.OwnerId = 'SPECIFY ID HERE';

}
else if(userrecords[0].Region__c == 'EMEA')
{

	leadObj.Status = 'Some Status';
	leadObj.OwnerId = 'SPECIFY ID HERE';
	
}
else if(userrecords[0].Region__c == 'Americas')
{
	leadObj.Status = 'Some Status';
	leadObj.OwnerId = 'SPECIFY ID HERE';
	
}
var updateResult = sforce.connection.update([leadObj]);

if (updateResult [0].success == 'false') {
alert(updateResult [0].errors.message);
}

else {
location.reload(true); /* redisplay the detail page */
}
}

 

All Answers

Nayana KNayana K
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")}

//Define the object
var leadObj = new sforce.SObject("Lead");

//Define the IDs of things
leadObj.Id = '{!Lead.Id}';

var result = sforce.connection.query(
"SELECT Id, Owner.Region__c from Lead WHERE Id ='" + leadObj.Id + "'");
var records = result.getArray("records");

if(records.length > 0)
{


// IF APAC
if(records[0].Owner.Region__c == 'APAC')
{
  leadObj.Status = 'Some Status'; 
  leadObj.OwnerId = 'SPECIFY ID HERE';

}
else if(records[0].Owner.Region__c == 'EMEA')
{

	leadObj.Status = 'Some Status';
	leadObj.OwnerId = 'SPECIFY ID HERE';
	
}
else if(records[0].Owner.Region__c == 'Americas')
{
	leadObj.Status = 'Some Status';
	leadObj.OwnerId = 'SPECIFY ID HERE';
	
}
var updateResult = sforce.connection.update([leadObj]);

if (updateResult [0].success == 'false') {
alert(updateResult [0].errors.message);
}

else {
location.reload(true); /* redisplay the detail page */
}
}

 
Dale WorthingtonDale Worthington
Hi Nayana

Thanks for your update and thoughts on this! Truely greatful. 

I have tried this and im getting an error (below), i have updated the fields to that they contain the Status and the ID of the users. I have checked the user object and the field is definaly labeled 'Region__c'. 

I dont quite understand the entity 'Name' error because name isnt mentioned anywhere. This is quite a bit outside of my scope of understanding. 

User-added image
Pankaj_GanwaniPankaj_Ganwani
Hi Dale,

Region__c is a custom field created on User object which cannot be queried in SOQL. You can refer the below mentioned link for the permitted User fields that can be retrieved in SOQL:

http://skaruz.com/en/2015/03/english-salesforce-how-to-access-owner-custom-fields-in-a-soql-query/

I have just modified the above code a little:
 
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")}

//Define the object
var leadObj = new sforce.SObject("Lead");

//Define the IDs of things
leadObj.Id = '{!Lead.Id}';

var result = sforce.connection.query(
"SELECT Id, OwnerId from Lead WHERE Id ='" + leadObj.Id + "'");
var records = result.getArray("records");

var ownerRegion = sforce.connection.query(
"SELECT Id, Region__c from Lead WHERE Id ='" + records[0].OwnerId + "'");
var userrecords = ownerRegion.getArray("records");

if(records.length > 0)
{


// IF APAC
if(userrecords[0].Region__c == 'APAC')
{
  leadObj.Status = 'Some Status'; 
  leadObj.OwnerId = 'SPECIFY ID HERE';

}
else if(userrecords[0].Region__c == 'EMEA')
{

	leadObj.Status = 'Some Status';
	leadObj.OwnerId = 'SPECIFY ID HERE';
	
}
else if(userrecords[0].Region__c == 'Americas')
{
	leadObj.Status = 'Some Status';
	leadObj.OwnerId = 'SPECIFY ID HERE';
	
}
var updateResult = sforce.connection.update([leadObj]);

if (updateResult [0].success == 'false') {
alert(updateResult [0].errors.message);
}

else {
location.reload(true); /* redisplay the detail page */
}
}

 
Dale WorthingtonDale Worthington
Hi Pankaj,

I made the changes you suggested so now the ID of the owner is actually showing up in the error. 
Am i right in understanding that what i want to do is not possiblebecause the custom field will not be readable, and there is no work around?

The current error i get is below, it mentions that the custom field is invalid leading me to think that based on the link you provided i will be unable to achieve what im trying to do.

Is this correct?

Dale

User-added image
Dale WorthingtonDale Worthington
Using the link provided i used a formula to pull the region of the owner to the lead object.
Ive put an alert in there to show where its getting up to. Now i get to an alert saying APAC but then it throws an error reading:
"Unable to Create/Update fields: LeadRegion__c" despite the fact that im not trying to update that field....

Frustrating! 
 
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")}

//Define the object
var leadObj = new sforce.SObject("Lead");


//Define the IDs of things

leadObj.Id = '{!Lead.Id}';

/* var result = sforce.connection.query(
"SELECT Id, OwnerId from Lead WHERE Id ='" + leadObj.Id + "'");
var records = result.getArray("records");

var ownerRegion = sforce.connection.query(
"SELECT Id, Region__c from Lead WHERE Id ='" + records[0].OwnerId + "'");
var userrecords = ownerRegion.getArray("records");

if(records.length > 0)
{ */


// IF APAC
if(leadObj.LeadRegion__c = "APAC")
{
  leadObj.Status = 'Marketing Qualified'; 
  leadObj.OwnerId = '00520000003YH58';
  alert('APAC')

}
else if(leadObj.LeadRegion__c = "EMEA")
{

	leadObj.Status = 'Marketing Qualified';
	leadObj.OwnerId = '00520000003YH58';
	
}
else if(leadObj.LeadRegion__c = "Americas")
{
	leadObj.Status = 'Marketing Qualified';
	leadObj.OwnerId = '00520000003YH58';
	
}
var updateResult = sforce.connection.update([leadObj]);

if (updateResult [0].success == 'false') {
alert(updateResult [0].errors.message);
}

else {
alert('Reloading the page')
location.reload(true); /* redisplay the detail page */
}


//}

 
Pankaj_GanwaniPankaj_Ganwani
Hi Dale,

I missed to change the object name in second SOQL. Please use below mentioned code:
 
{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")}

//Define the object
var leadObj = new sforce.SObject("Lead");

//Define the IDs of things
leadObj.Id = '{!Lead.Id}';

var result = sforce.connection.query(
"SELECT OwnerId from Lead WHERE Id ='" + leadObj.Id + "'");
var records = result.getArray("records");

var ownerRegion = sforce.connection.query(
"SELECT Region__c from User WHERE Id ='" + records[0].OwnerId + "'");
var userrecords = ownerRegion.getArray("records");

if(records.length > 0)
{


// IF APAC
if(userrecords[0].Region__c == 'APAC')
{
  leadObj.Status = 'Some Status'; 
  leadObj.OwnerId = 'SPECIFY ID HERE';

}
else if(userrecords[0].Region__c == 'EMEA')
{

	leadObj.Status = 'Some Status';
	leadObj.OwnerId = 'SPECIFY ID HERE';
	
}
else if(userrecords[0].Region__c == 'Americas')
{
	leadObj.Status = 'Some Status';
	leadObj.OwnerId = 'SPECIFY ID HERE';
	
}
var updateResult = sforce.connection.update([leadObj]);

if (updateResult [0].success == 'false') {
alert(updateResult [0].errors.message);
}

else {
location.reload(true); /* redisplay the detail page */
}
}

 
This was selected as the best answer
Dale WorthingtonDale Worthington
You're a gentleman among men!
Thanks so much