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();
52.0
or later, as theSforce-Duplicate-Rule-Header
is available from API version52.0
. If the header approach doesn't work, you can useDatabase.DMLOptions
to set the duplicate rule options directly in your Apex code.