You need to sign in to do that
Don't have an account?
Lukasz Kiwinski 18
how create one record from aother salesforce via apex
Hello!
Please could you help me to get the correct code to the below issue and if it possible?
We have a custom object for requesting new users in Salesforce. If manager wants to request for create new user he needs to create new record (User Request) and provide all infomrations like name, email, profile etc. For now I create users manually using infomration on this record.
I would like to add a buton with apex code to create new user based on the infomration from this record, it si possible?
Thanks in advance!
L.
Please could you help me to get the correct code to the below issue and if it possible?
We have a custom object for requesting new users in Salesforce. If manager wants to request for create new user he needs to create new record (User Request) and provide all infomrations like name, email, profile etc. For now I create users manually using infomration on this record.
I would like to add a buton with apex code to create new user based on the infomration from this record, it si possible?
Thanks in advance!
L.
1. Write a VF page that gets called on click of button, inserts the User record and redirects back to your original page from where button was clicked or you may choose to redirect to the newly created user too.
2. Write a simple apex method to do it, mark it as a web-service and call from on-click javascript on the button that you would be clicking.
All Answers
1. Write a VF page that gets called on click of button, inserts the User record and redirects back to your original page from where button was clicked or you may choose to redirect to the newly created user too.
2. Write a simple apex method to do it, mark it as a web-service and call from on-click javascript on the button that you would be clicking.
Both approaches above were for Classic only.
2 above can be understood from folowing link, its the easier approach -
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_and_ajax.htm
1st is more complex than this but not really complex, no direct documentation available :)
Here you go, the following snippet would create a user from a contact. You can do similar stuff on your custom object and tweak as per your requirement.
{!REQUIRESCRIPT("/soap/ajax/45.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/45.0/apex.js")}
var user = new sforce.SObject("User");
user.LastName = "{!Contact.LastName}";
user.FirstName = "{!Contact.FirstName}";
user.Email = "{!Contact.Email}";
user.UserName = "{!Contact.LastName}" + "@test.com.sandbox";
user.ProfileId = "00e90000000RP4n";
user.Alias = "{!Contact.LastName}";
user.TimeZoneSidKey = "Europe/Istanbul";
user.LocaleSidKey = "en_US";
user.EmailEncodingKey = "ISO-8859-1";
user.LanguageLocaleKey = "en_US";
sforce.connection.create([user],{onSuccess : success, onFailure : failed});
function success(result) {
if (result[0].getBoolean("success")) {
alert("new user created with id " + result[0].id);
}
else {
alert("failed to create user " + result[0]);
}
}
function failed(error) {
alert("An error has occurred " + error);
}
https://developer.salesforce.com/docs/atlas.en-us.ajax.meta/ajax/sforce_api_ajax_more_samples_asynch.htm?search_text=create
Thanks agian!