Result Objects

Result objects are wrapper objects that indicate whether or not the API call was a success and, if successful, include the requested data.
Note
API calls that don't have validations, such as searches, will return a collection of requested objectsinstead of a result object.

Success resultsAnchorIcon

If the API call was successful, the `IsSuccess()` on the result will return {true}.
  1. C#
bool success = result.IsSuccess();
Transaction transaction = result.Target;
Generics are used to preserve static typing of the resource objects. The target will be an instance of the requested resource class.
  1. C#
Result<customer> customerResult = gateway.Customer.Create(new CustomerRequest()); Customer customer = customerResult.Target; Result<transaction> transactionResult = gateway.Transaction.Sale( new TransactionRequest() ); Transaction transaction = transactionResult.Target;

Error resultsAnchorIcon

If the API call was not successful, the success on the result will return {false}. An error may be due to:
  1. C#
if (!result.IsSuccess()) {
    ValidationErrors errors = result.Errors;
}
The Errors result object will only be populated if the error is due to a failed validation. In this case, the object will contain one or more validation errors indicating which parameters were invalid:
  1. C#
var request = new CustomerRequest {
    Email = "invalid_email",
    CreditCard = new CreditCardRequest {
        Number = "not_numeric"
    }
};

Result<customer> result = gateway.Customer.Create(request);

foreach (ValidationError error in result.Errors.DeepAll()) {
    Console.WriteLine(error.Attribute);
    Console.WriteLine(error.Code);
    Console.WriteLine(error.Message);
}

var customerErrors = result.Errors.ForObject("Customer");

foreach (ValidationError error in customerErrors.All()) {
    Console.WriteLine(error.Attribute);
    Console.WriteLine(error.Code);
    Console.WriteLine(error.Message);
}

var creditCardErrors = customerErrors.ForObject("CreditCard");

foreach (ValidationError error in creditCardErrors.All()) {
    Console.WriteLine(error.Attribute);
    Console.WriteLine(error.Code);
    Console.WriteLine(error.Message);
}
For details on transaction error results, see the transaction response object .

MessageAnchorIcon

The message on the error result gives a human-readable description of what went wrong, regardless of the cause and nature of the error.
  1. C#
Console.WriteLine(result.Message); // "Amount is required.\nCredit card number is invalid."
The message can contain multiple error messages.
Note
This was added in version {{sdkVersionForDate "Jul09_2010"}}

ParamsAnchorIcon

Error results include the parameters that were submitted. This can be useful during Transparent Redirects to repopulate your form if validations fail.
  1. C#
foreach (var param in result.Parameters) {
    Console.WriteLine(param.Key + "=" + param.Value);
} // transaction[type]=sale // transaction[amount]=1000 // transaction[credit_card][expiration_date]=05/2012
For PCI compliance reasons, credit card number and cvv parameters are not included.

See AlsoAnchorIcon