0

I am trying to create a Test Method which will test a REST API however I keep hitting duplicate rules when I am performing upsert. I have added the addHeader('Sforce-Duplicate-Rule-Header', 'allowSave=true') to the RestRequest in order to bypass the duplicate rules however this doesn't seem to be working. Does anyone have any ideas?

TEST CLASS

@isTest
    static void registerExistingLeadWebId(){
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        req.requestURI = '/services/apexrest/Register-Lead/';
        req.httpMethod = 'PUT';
        req.addHeader('Sforce-Duplicate-Rule-Header', 'allowSave=true');
        RestContext.request = req;
        RestContext.request.addHeader('Sforce-Duplicate-Rule-Header', 'allowSave=true');
        RestContext.response = res;


        Test.startTest();
            RegisterLeadAPI.createLead('Upsert Lead', '[email protected]', 'Test Company', '123456789');
        Test.stopTest();
2
  • Make sure that your API version is 52.0 or later, as the Sforce-Duplicate-Rule-Header is available from API version 52.0. If the header approach doesn't work, you can use Database.DMLOptions to set the duplicate rule options directly in your Apex code. Commented Sep 6 at 11:29
  • Thanks for the reply Tushar. The version is above 52.0 and unfortunately you cannot use Database.DMLOptions with upsert. It's strange as when I send the header via postman the class performs as expected it just doesn't seem to like it when adding via test method.
    – Sean
    Commented Sep 6 at 14:09

0

You must log in to answer this question.

Browse other questions tagged .