• Stuart Grimshaw
  • NEWBIE
  • 55 Points
  • Member since 2013
  • WANdisco

  • Chatter
    Feed
  • 0
    Best Answers
  • 3
    Likes Received
  • 0
    Likes Given
  • 12
    Questions
  • 8
    Replies
I have a trigger that fires when an opportunity is updated, as part of that I need to call our API with some detail from the opportunity.

As per many suggestions on the web I've created a class that contains a @future method to make the callout.

I'm trying to catch an exception that gets thrown in the @future method, but the test method isn't seeing it.

The class under test looks like this:

public with sharing class WDAPIInterface {
    public WDAPIInterface() {

    }

    @future(callout=true) public static void send(String endpoint, String method, String body)  {
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endpoint);
        req.setMethod(method);
        req.setBody(body);

        Http http = new Http();
        HttpResponse response = http.send(req);

        if(response.getStatusCode() != 201) {
            System.debug('Unexpected response from web service, expecte response status status 201 but got ' + response.getStatusCode());
            throw new WDAPIException('Unexpected response from web service, expecte response status status 201 but got ' + response.getStatusCode());
        }
    }
}

here's the unit test:

@isTest static void test_exception_is_thrown_on_unexpected_response() {
        try {
            WDHttpCalloutMock mockResponse = new WDHttpCalloutMock(500, 'Complete', '', null);
            WDAPIInterface.send('https://example.com', 'POST', '{}');
        } catch (WDAPIException ex) {
            return;
        }

        System.assert(false, 'Expected WDAPIException to be thrown, but it wasnt');
    }
Now, I've read that the way to test @future methods is to surround the call with Test.startTest() & Test.endTest(), however when I do that I get another error:

METHOD RESULT 
test_exception_is_thrown_on_unexpected_response : Skip

 MESSAGE 
Methods defined as TestMethod do not support Web service callouts, test skipped
So the question is, how do I unit test a @future method that makes an callout?

I have an approval process that I have written on our Sandbox, it isn't deployed yet, and isn't ready to be deployed yet but I want to refresh the Sandbox.

I have a developer instance that I'd like to deploy the approval process (and asscociated custom objects, code etc) to. I can't add Approval Proceses to a package (and I don't seem to be able to deploy a package created on my sandbox to developer instance?)

I've got Eclipse installed and I have created a project that includes the meta-data for the approval processes I want to deploy, but the list is empty? There are no errors when I refresh the metadata to suggest why they're empty.

How do I deploy my approval process to another instance?
I have a class that I'm using to load data in to the database for my unit tests & I'm trying to populate a Lookup field with some data.

The Lookup field is on a User, and the field is a custom one I added to Product2.

I'm trying to assign the user like this:

  User u = [SELECT Id from User WHERE email = 'stuart.grimshaw@workemail.com'];

  Product2 withTraining = new Product2(Name='Product with Bundled Support & Training', Support_Level__c = 'Silver', Support_Email_Body__c = 'All the things!', Support_Contacts__c = 2, Server_License_Count__c = 5, User_License_Count__c = 15, Product_Type__c = 'Product, Support & Training Bundle');
  withTraining.Trainer__c = u.Id;

or this:

  Product2 withTraining = new Product2(Name='Product with Bundled Support & Training', Support_Level__c = 'Silver', Support_Email_Body__c = 'All the things!', Support_Contacts__c = 2, Server_License_Count__c = 5, User_License_Count__c = 15, Product_Type__c = 'Product, Support & Training Bundle', Trainer__c =u.Id);

or this:

  Product2 withTraining = new Product2(Name='Product with Bundled Support & Training', Support_Level__c = 'Silver', Support_Email_Body__c = 'All the things!', Support_Contacts__c = 2, Server_License_Count__c = 5, User_License_Count__c = 15, Product_Type__c = 'Product, Support & Training Bundle', Trainer__r =u);

None of these things seems to work, when I try and retreive the name of trainer in my test like this:

    public String getTrainerName() {
        if(this.quoteProds == null) {
            this.quoteProds = getQuoteLineItems(this.quoteId);
        }

        for (QuoteLineItem lineItem : this.quoteProds) {
            if(lineItem.PricebookEntry.Product2.Trainer__r.Id  != null) {
                return lineItem.PricebookEntry.Product2.Trainer__r.Name;
            }
        }

        return 'Unknown';
    }

I get 'Unkown'.

Other tests in the suite confirm that the Quote does have line items.
When using an Apex component from within a Visualforce email template, how do I access the Id of the object the email is related to?

In the Apex controller class I've tried using ApexPages.currentPage().getParameters().get('related_to_id') which works when using the "Send test & verify merge fields" button from the template page, but this doesn't work when the email is sent via an Aproval process.

I've also tried using ApexPages.currentPage().getParameters().get('id') which works in neither!
I have a component that I call from an email template, the controller for the component is an Apex class that needs to get the ID of the related object so it can look up some data, in the constructor I do this:

public void ShippingHelper() {
        this.quoteId = ApexPages.currentPage().getParameters().get('related_to_id');
    }

When testing the email on the "Visualforce Email Template" page I can select the related object & it renders the email perfectly.

When I send the same email as part of an approval process none of the content from the class is rendered.
If I have a static method on an Apex class, can I use it in an Approval Process's entry criteria?
I've created an email template that I'm trying to test with the "Send test & Verify Merge Fields" button, but when the email arrives none of the fields are filled in.

I'm not even sure where to begin debugging this. Would it be logged somewhere?
As part of the approval process for our sales team I'd like to assign a quote to a queue to enable easy management of people who are able to approve the request.

Looking at the list of available object type however, Quote isn't one of them.

Do I have to assign each user 1 by 1 or is there a way to assign quotes to a queue?
All yesterday I could run tests from Eclipse with no problems, but today I run them & I get nothing, no failures, no passes, no debug logs, nothing.

If I run the tests through the web interface they fail, and I can see why.

I've also had issues with custom fields not showing up in Eclipse today too.

Is there any way to reset things?
Is there a way I can have my customers update their own accunt details?

When a qote is accepted & we have a PO on the opportunity I'd like to send an email to a customer so they can update their own customer details.
As a final step in our approval process for a quote I'd like to be able to update the account and set the Type to "Customer", I know I can't do it with a field update because both relationships between Quote/Opportunity & Opportunity/Account are lookups and not master/data.

Is there a best practice for this kind of thing, I would imagine it's a pretty common thing to want to do?
As part of an approval process I'm creating I'd like to be able to attach a license key to an email that is then sent to a contact email of an opportunity when the opportunity is approved.

We have a separate system that generates & stores our license keys & I'd like to avoid having to upload the key to SFDC to avoid duplication & confusion over which is the valid key as the attached one will have a short expirey time on it.
When using an Apex component from within a Visualforce email template, how do I access the Id of the object the email is related to?

In the Apex controller class I've tried using ApexPages.currentPage().getParameters().get('related_to_id') which works when using the "Send test & verify merge fields" button from the template page, but this doesn't work when the email is sent via an Aproval process.

I've also tried using ApexPages.currentPage().getParameters().get('id') which works in neither!
I have a component that I call from an email template, the controller for the component is an Apex class that needs to get the ID of the related object so it can look up some data, in the constructor I do this:

public void ShippingHelper() {
        this.quoteId = ApexPages.currentPage().getParameters().get('related_to_id');
    }

When testing the email on the "Visualforce Email Template" page I can select the related object & it renders the email perfectly.

When I send the same email as part of an approval process none of the content from the class is rendered.
As part of the approval process for our sales team I'd like to assign a quote to a queue to enable easy management of people who are able to approve the request.

Looking at the list of available object type however, Quote isn't one of them.

Do I have to assign each user 1 by 1 or is there a way to assign quotes to a queue?
I have a trigger that fires when an opportunity is updated, as part of that I need to call our API with some detail from the opportunity.

As per many suggestions on the web I've created a class that contains a @future method to make the callout.

I'm trying to catch an exception that gets thrown in the @future method, but the test method isn't seeing it.

The class under test looks like this:

public with sharing class WDAPIInterface {
    public WDAPIInterface() {

    }

    @future(callout=true) public static void send(String endpoint, String method, String body)  {
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endpoint);
        req.setMethod(method);
        req.setBody(body);

        Http http = new Http();
        HttpResponse response = http.send(req);

        if(response.getStatusCode() != 201) {
            System.debug('Unexpected response from web service, expecte response status status 201 but got ' + response.getStatusCode());
            throw new WDAPIException('Unexpected response from web service, expecte response status status 201 but got ' + response.getStatusCode());
        }
    }
}

here's the unit test:

@isTest static void test_exception_is_thrown_on_unexpected_response() {
        try {
            WDHttpCalloutMock mockResponse = new WDHttpCalloutMock(500, 'Complete', '', null);
            WDAPIInterface.send('https://example.com', 'POST', '{}');
        } catch (WDAPIException ex) {
            return;
        }

        System.assert(false, 'Expected WDAPIException to be thrown, but it wasnt');
    }
Now, I've read that the way to test @future methods is to surround the call with Test.startTest() & Test.endTest(), however when I do that I get another error:

METHOD RESULT 
test_exception_is_thrown_on_unexpected_response : Skip

 MESSAGE 
Methods defined as TestMethod do not support Web service callouts, test skipped
So the question is, how do I unit test a @future method that makes an callout?

I have an approval process that I have written on our Sandbox, it isn't deployed yet, and isn't ready to be deployed yet but I want to refresh the Sandbox.

I have a developer instance that I'd like to deploy the approval process (and asscociated custom objects, code etc) to. I can't add Approval Proceses to a package (and I don't seem to be able to deploy a package created on my sandbox to developer instance?)

I've got Eclipse installed and I have created a project that includes the meta-data for the approval processes I want to deploy, but the list is empty? There are no errors when I refresh the metadata to suggest why they're empty.

How do I deploy my approval process to another instance?
When using an Apex component from within a Visualforce email template, how do I access the Id of the object the email is related to?

In the Apex controller class I've tried using ApexPages.currentPage().getParameters().get('related_to_id') which works when using the "Send test & verify merge fields" button from the template page, but this doesn't work when the email is sent via an Aproval process.

I've also tried using ApexPages.currentPage().getParameters().get('id') which works in neither!
If I have a static method on an Apex class, can I use it in an Approval Process's entry criteria?
I've created an email template that I'm trying to test with the "Send test & Verify Merge Fields" button, but when the email arrives none of the fields are filled in.

I'm not even sure where to begin debugging this. Would it be logged somewhere?
As part of the approval process for our sales team I'd like to assign a quote to a queue to enable easy management of people who are able to approve the request.

Looking at the list of available object type however, Quote isn't one of them.

Do I have to assign each user 1 by 1 or is there a way to assign quotes to a queue?
As a final step in our approval process for a quote I'd like to be able to update the account and set the Type to "Customer", I know I can't do it with a field update because both relationships between Quote/Opportunity & Opportunity/Account are lookups and not master/data.

Is there a best practice for this kind of thing, I would imagine it's a pretty common thing to want to do?