When the user clicks a button called "convert to service catalogue" it should trigger the creation of a new record. In some instances, this will fail, because the user logged in clicking the button does not have the privileges to perform the action.

How can I ensure an error message comes up when the record creation has not occured?

At this moment in time it takes me to a page that says "URL No Longer Exsists". This happens if the record creation has been successful and failed.

Below is the code for my button.


var result = sforce.apex.execute("ProductAction","convertToService",

window.location.href = '/'+result[0].Id+'/e';

And below is the code for my apex class that links to the button.

global class ProductAction {
WebService static String convertToService(Id product2) {
    string result; // Return a string telling us if this worked or not
    Id currentProfile = UserInfo.getUserId(); // Get the current user profile Id
    Id CurrentUser = UserInfo.getUserId(); // Get the current user Id

    Set<Id> allowedProfiles = new Set<Id>(); // Create a new set to profile Ids
    for (Profile p: [SELECT Id, Name FROM Profile WHERE Name = 'System Administrator']) {// Get the System Administrator profile
        allowedProfiles.add(p.Id); // Add them to the set

    Set<Id> allowedUsers = new Set<Id>(); // Create a new set to store User Ids
    for (User u: [SELECT Id FROM User WHERE Id = '005w0000004j93kAAA' OR Id = '005w0000004Noi6']) {//Select Joy Palmer and Andy McGregor Id's and select Jeremey Sharp and Mark O'Leary once they're in SF}
        allowedUsers.add(u.Id);// Add them to the set
    Product2 prod = [SELECT Convert_to_Service_catalogue__c, R_D_Service_Owner__r.id , Project_Owner__r.Id From Product2 WHERE Id = :product2]; // Get the product

    if( //If...
        (currentUser == prod.R_D_Service_Owner__r.id) || // They own it
        (currentUser == prod.Project_Owner__r.id) || // They own it 
        (allowedProfiles.contains(currentProfile)) || //They're a system admin
        (allowedUsers.contains(currentUser))// They're a specific user
        ) {
            //Do stuff!
            prod.Convert_to_Service_Catalogue__c = true;

            UPDATE prod;

            result = 'Success!';

    else {// Otherwise...
          // Do nothing
          result = 'Go Away!';
       // Return a message
       return result;

  • Quick note: if you are returning a string, but trying to access a property on the string (result[0].Id), you'll always get a null result. Just result[0] will get you the id in this case. Nov 15 '16 at 14:41

Check the returned string from your function. If the record was inserted and returned, your function should return a valid Id. If the function fails, either return an error code, or message.


var result = sforce.apex.execute('TestClass', 'TestFunction', {successful: true});

// Check result in console window 

// If this result starts with a string equal to the id key for the object type you are accessing, relocate the browser to that page 
if (result[0].startsWith('001')) {
    window.location.href = '/' + result[0] + '/e';
} else {
    // Otherwise, show the error message to the viewer 

Here's a class which emulates the class you posted. Passing a boolean to it controls what it returns, either an error or an Id.

global class TestClass {
    WebService static String TestFunction(Boolean successful) {
        return successful ? '0013B000008bjem' : 'Error: No Such Record Found'; 

There may be better ways to do this, such as returning an object containing the result, and an error object, with some extra information. This is how the connection.js library handles calls such as update. It's worth taking a look at how they structure the return values from such calls to see how you should be structuring your code, but the method above should work as a band-aid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.