1

My requirement is to allow a user (other than salesforce admin) to allow to add picklist values by filling a value in text field and submitting form via a visualforce page. I am using javascript remoting for this. Below is my VF code :-

<apex:page controller="addfieldtopicklist" showHeader="false">
<apex:form id="frm">
<div id="nm0"></div>
<table>
<tr>
<td><input type="text" id="nm"  /></td>
<td>
<input type="button" value="Add To Picklist" onclick="customjsfnc()" />
</td>
</tr>
</table>
</apex:form>
<script>
function customjsfnc(){
var p = document.getElementById('nm').value;
Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.addfieldtopicklist.PicklistField}',
        p, 
        function(result, event){
        alert(result);
       }, 
        {escape: true}
    );
}
 </script>
 </apex:page>

And below is the Apex code :-

public class addfieldtopicklist {
 public String plField{get;set;}
 @RemoteAction
 public static String PicklistField(String p) {
    MetadataService.MetadataPort service = new    MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();
    MetadataService.CustomField customField = (MetadataService.CustomField) service.readMetadata('CustomField',new String[] { 'Scope_Of_Services_Tasks__c.Meeting_Type__c' }).getRecords()[0];
    MetadataService.PicklistValue picklistField = new MetadataService.PicklistValue();
    picklistField.fullName= p;
    picklistField.default_x=false;
    customField.picklist.picklistValues.add(picklistField);
    return handleSaveResults(
    service.updateMetadata(
        new MetadataService.Metadata[] { customField })[0]);   
  }
public static MetadataService.MetadataPort createService(){ 
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();
    return service;     
}

private static String handleSaveResults(MetadataService.SaveResult saveResult){
    // Nothing to see?
    if(saveResult==null || saveResult.success){
        return 'success';
    }
    List<String> messages = new List<String>();
    // Construct error message 
    if(saveResult.errors!=null) {
        messages.add('Error occured processing component ' + saveResult.fullName + '.');
        }
    }
return String.join(messages, ' ');
}   

}

I am getting this error from api:- "Error occured on processing component 'Scope_Of_Services_Tasks__c.Meeting_Type__c' insufficient access rights on cross-reference id insufficient_access_on_cross_reference_entity"

I have given this user 'Modify All data' permission already. I think I have to allow more permissions from perimission sets but not sure. I am new to Salesforce please help me. Thanks in Advance.

Update:- I figured that 'Scope_Of_Services_Tasks__c' is child object to some other object in master detail relationship. So that is why the above error is coming . But I still can't figure out what else I need to give this user in permissions since I have already given 'Modify all data' permission in profile. Please help me know. Thanks

  • This should be an admin only operation. If you really want users to be able to do something like that, you should at least create some sort of request object and have an admin do the actual add. – Adrian Larson Feb 2 '15 at 17:47
  • 1
    I suspect the user also needs Customize Application privileges – cropredy Feb 3 '15 at 0:09
  • @crop1645 Thanks... Your suggestion did the trick... – user3361920 Feb 3 '15 at 12:44
1

Updating metadata using the Apex wrapper to the Metadata API (which is what it appears you are using) requires the running user have Customize Application privileges

0

If this is a custom object, you should use the Delegated Administration functionality. Delegated Administration

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.