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
NordbergNordberg 

Change Lead owner Via S-Control

I need to change the Lead owner via an S-Control after the new lead is saved.  The owner will be changed to the value of a custom field that is a lookup field to the user table. 

Any help would be appreciated,

Thanks,

JD
sfdcfoxsfdcfox
Couldn't you just use Assignment Rules or something? You can specify custom criteria for the assignment so they can be assigned based on the value in your custom field. However, if you needed to transfer using a lookup field value, you could do this...

Code:
{!RequireScript("/soap/ajax/9.0/connection.js")}
Lead = new sforce.SObject("Lead")
Lead["id"] = "{!Lead.Id}"
Lead["ownerid"] = "{!Lead.NewOwnerId__c}"
try
{ sforce.connection.update([Lead])
} catch(e)
{ alert(e)
}
window.top.location.href = "/{!Lead.Id}"
Note that this code assumes the field name is "NewOwner", which the merge field will add "ID" to for lookup fields, hence "NewOwnerId__c". Hope this helps you out.

~ sfdcfox ~
NordbergNordberg
I wanted to use the assigment rule but the owner change possiblity is 130+ different options and changes all the time.  The code you gave me resulted in an error
Error: Function REQUIRESCRIPT may not be used in this type of formula

I am obvioulsy new to this type of work and any help would be greatly appreciated.

Thanks,

JD
sfdcfoxsfdcfox
RequireScript only works in some types of items. If you're using an S-Control, try using this form instead:

Code:
<script src="/soap/ajax/9.0/connection.js"></script>
<script language="JavaScript">
Lead = new sforce.SObject("Lead")
Lead["id"] = "{!Lead.Id}"
Lead["ownerid"] = "{!Lead.NewOwnerId__c}"
try
{ sforce.connection.update([Lead])
} catch(e)
{ alert(e)
}
window.top.location.href = "/{!Lead.Id}"
</script>
The format I provided before would be for an OnClick button/link.
 

NordbergNordberg
That worked like a charm!!  The only thing that I would want it to do is to send email notification and run only if the custom field doesnt equal the owner field.  As it is now it continues to run indefinatley.  You have been a huge help.

Thanks,

JD:smileyvery-happy:
sfdcfoxsfdcfox
You can check if the owner matches the dropdown as follows:

Code:
<script src="/soap/ajax/9.0/connection.js"></script>
<script language="JavaScript">
if({!if(Lead.OwnerId<>Lead.NewOwnerId__c,"true","false")})
{ Lead = new sforce.SObject("Lead")
Lead["id"] = "{!Lead.Id}"
Lead["ownerid"] = "{!Lead.NewOwnerId__c}"
try
{ sforce.connection.update([Lead])
window.top.location.href = "/{!Lead.Id}"
 } catch(e)
{ alert(e)
}
}
</script>

 The  code inside the {! ... } section will be evaluated and replaced inline within the code. Basically, the resulting code would look like:

if(true)

If the owner does not equal the selected ID. Otherwise, it would look like:

if(false)

This, of course, means the code would not evaluate; no API call would be used and the page would not refresh. Also, I changed the page refresh so if there's an error then the page won't refresh until they leave the lead and come back to the record.

~ sfdcfox ~
NordbergNordberg
Why do you have too double inlay the IF?  You have if({!if - It seems counterintuitive too me.  Also is there a simple way to send an email notification to the new owner?  You have been a huge help.

I really do appreciate all of your time and effort.

Thanks,

JD
sfdcfoxsfdcfox
I wrote the code that way to create two conditional statements; one is processed by Salesforce, one is processed by the browser. {!If(Lead.OwnerID<>Lead.NewOwnerId__c,"true","false")} is code that tells Salesforce to replace the curley brackets and enclosed code with a value; in this case, true or false.

The effect is that the browser never sees this conditional statement; it will see if(true) or if(false) and then execute the code to change the owner (or not). This same logic could be written as:

if("{!Lead.OwnerId}" != "{!Lead.NewOwnerId}")

You can use that as a replacement to the original "if" line if it makes more sense to you.

To send an email notification, you need to "check the notification checkbox". In the AJAX toolkit, this is done by including the appropriate header; you should add these two lines of code:

sforce.connection.emailHeader = {}
sforce.connection.emailHeader.triggerUserEmail = true


This will tell the API that it should send the default notification email. You may want to refer to the API and AJAX Toolkit documentation for more information.

~ sfdcfox ~
NordbergNordberg
Thank you very much for all of your help.  The Salesforce.com community is truley one of a kind.

Thanks,

JD