0

I have created a Custom setting as follows

Label: MyDomainURL
API Name: MyDomainURL__c
Visibility: 'Public'
Setting Type: 'List'
Custom Field: MyURL__c

I have referenced this in my Apex test class

    MyDomainURL__c custsetting = new MyDomainURL__c();
    custsetting .Name = 'MyDomainURL';
    custsetting .MyURL__c = 'https://galaxy--sb2.csxx.my.salesforce.com/';
    insert custsetting ; 

This is in my sandbox. When i deploy to production, MyURL__c will change.

Is there a better way to re-write the above code to avoid hard coding setting.Name and setting.MyURL__C and getting these values dynamically.

Thanks!

  • Why do you need the My Domain URL in a unit test context? You won't be able to make a callout in that context anyway - can you use a dummy URL? – David Reed Jan 8 at 16:22
  • Thanks for responding @DavidReed I have a trigger handler class, in which is a logic that sends an email out HtmlBody+='The link to the Account is : <a href="'+urlMap.get('MyDomainURL').URL__c+acc.Id+'" target="_blank">Click Here</a> <br><br>'; That's the reason why I have the url is unit test context. Let me try using a dummy URL and see if test class executes successfully. – user4947 Jan 8 at 16:27
  • 1
    You shouldn't need to use a custom setting in an email to appropriately build a Salesforce link. I'd suggest going back and looking at that first as it would remove the need for you to mess with this at all. – Kris Goncalves Jan 8 at 20:43
0

You cannot make a real outbound call of any kind in a unit test context. Typically, unit tests should not be dependent on the actual org in which they are executing.

I would strongly recommend using a dummy URL in this class, and writing your assertions to validate the behavior of the class using that dummy URL.

Note also that your code can always source the org's URL from the URL class, so you probably do not need to keep it in a Custom Setting anyway.

| improve this answer | |

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.