+ Start a Discussion
Lukasz Kiwinski 18Lukasz 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.
Best Answer chosen by Lukasz Kiwinski 18
JayantJayant
Yes, its possible. Approaches below - 

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

JayantJayant
Yes, its possible. Approaches below - 

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.
This was selected as the best answer
JayantJayant
You are on Classic or Lightning ?
Lukasz Kiwinski 18Lukasz Kiwinski 18
I am on Classic (unfortunately :))
JayantJayant
Not that unfortunate my friend :).

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 :)
Lukasz Kiwinski 18Lukasz Kiwinski 18
Thank you for showing right direction :)
JayantJayant
You're welcome, let me know if you need any help with the code (in case you are not a programmer).
Lukasz Kiwinski 18Lukasz Kiwinski 18
I am basic about programming but I am trying ot learn as much as possible, so it may be good opportunity. If you be so kind you can help me start writing this code and I will try to finish it by myself. What details do you need?

 
Lukasz Kiwinski 18Lukasz Kiwinski 18
Let's try with the easier one first :)
JayantJayant
Okay, since we are anyways doing a JavaScript button so we can also create user directly using AJAX toolkit rather than calling an Apex web service method from AJAX toolkit.
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);
}

Custom button to create a user from contact.


https://developer.salesforce.com/docs/atlas.en-us.ajax.meta/ajax/sforce_api_ajax_more_samples_asynch.htm?search_text=create

 
Lukasz Kiwinski 18Lukasz Kiwinski 18
Wow this looks perfect :) I will check it with my custom object and I will share the reuslts:)

Thanks agian!