1

I am trying to cover the following in my test class.

@RemoteAction
global static Tfunc InsertOrg(String Name,String AddressLine1,String AddressLine2,String AddressLine3,String AddressLine4,String City,String Country,String CountryName,String AliasName,String PostalCode,String StateOrProvince,String BusinessID,String RowID,String WebsiteURL){
    TOrg__c oOrganisation = new TOrg__c();
    oOrganisation.Name = Name;
    oOrganisation.Address_1__c = AddressLine1;
    oOrganisation.Address_2__c = AddressLine2;
    oOrganisation.Address_3__c = AddressLine3; 
    oOrganisation.Address_4__c = AddressLine4;
    oOrganisation.City_Name__c = City;
    oOrganisation.Country__c = Country;
    oOrganisation.Country_Name__c = CountryName;
    oOrganisation.Postal_Code__c = PostalCode;
    oOrganisation.State_Province__c = StateOrProvince;
    oOrganisation.Organization_Business_ID__c = BusinessID;
    oOrganisation.Org_Row_ID__c = RowID;
    oOrganisation.Web_Site__c = WebsiteURL;

    Upsert oOrganisation Organization_Business_ID__c;
    return oOrganisation;

}   

Organization_Business_ID__c is an external id field . how can i get rid of the error :

System.SecurityException: ViewAllData or ViewAllRecords required to access external id fields which do not have a unique index

  • Why is this method global? You need to either use runAs with a User who has those permissions, or modify the field metadata to make it unique. – Adrian Larson May 20 '16 at 13:48
7

The user with whose context the test is running should have View All Data permission on TOrg__c or should have system permission of View all Records if the external Id is not unique.

This permission is required so that duplicate records are not inserted while performing an UPSERT operation because the user is not able to see the record which has same external ID as the one you are trying to upsert.

Updated: Answer to comment.

Link for Documentation

If you are upserting a record for an object that has a custom field with both the External ID and Unique attributes selected (a unique index), you do not need any special permissions, because the Unique attribute prevents the creation of duplicates. If you are upserting a record for an object that has the External ID attribute selected but not the Unique attribute selected, (a non-unique index) your client application must have the permission “View All Data” to execute this call. Having this permission prevents the client application from using upsert() to insert an accidental duplicate record because it couldn’t see that the record existed.

  • Hi @manjit have you got a salesforce article or something for this? Also we could do a SOQL to read ids and assign existing ids (i.e. Ids of records with external id) and do a just upsert without using External Id, so why it does not end up giving the same exception? would like to know what really supported your answer with "UPSERT using external id" please. Appreciate it. Thanks. – Pasan Eeriyagama Jan 17 '18 at 2:20
  • @PasanEeriyagama Please see updated answer for the link. Salesforce Id's are unique so, Salesforce knows that it will not exist anywhere else, hence the permission is not required while upserting using SFDC ID field – manjit5190 Jan 17 '18 at 16:48

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.