Happy Business Starts Here

How do I write Test class in Salesforce for class which updates Bill to and Sold to Address at Zuora

Student

How do I write Test class in Salesforce for class which updates Bill to and Sold to Address at Zuora

 
7 REPLIES 7
Support SME

Re: How do I write Test class in Salesforce for class which updates Bill to and Sold to Address at Z

Hi @prasad02393 do you mean test class for OOB ZuoraContactUpdateController class?

We already have a test class for this. 

Sorry can you pls let me know your use case why a custome test class is needed for this?

 

Thank you






If you found my answer helpful, please give me a kudo ↑
Help others find answers faster by accepting my post as a solution √

Student

Re: How do I write Test class in Salesforce for class which updates Bill to and Sold to Address at Z

Hi Doyeli,

 

Thanks for your Reply,

 

The use case here is like in Salesforce Billing Account Object there are Fields Related to 'Bill To' and 'Sold to' Contact and if user updates any of these fields the same data should be updated at Zuora End, for Ex if user updated Bill to First name then same should be updated at Zuora Bill to Contact First name Field. This is the short desription of the use case. For this I have implemented a Trigger on Biling Account Object ad have wrote a class which uses zupdate method of Zuora tho update those fields.But we are facing issues while covering this in Unit Test class of Salesforce as we are not able to cover the callout.

Let me know if you can help here.

Guru

Re: How do I write Test class in Salesforce for class which updates Bill to and Sold to Address at Z

In order to get test coverage you need to structure your code so that it is testable.

What I do for these cases is build the result object instead of calling the update method the code is called from a test.

if(!Test.isRunningTest()){
    results = zApiInstance.zupdate(objs);
    logCurrentCallout('UpdateZObjects');
 }
 else {
     results = new List<Zuora.zApi.saveResult>();
    Zuora.zApi.saveResult testSaveResultSuccess = ZuoraZApiInstanceFactory.saveResult();
     testSaveResultSuccess.success=true;
    results.add(testSaveResultSuccess);
}

I also isolate the method that build the object list for update so I can write specific trigger tests that ensure the data I send for the update is accurate.

Maggie Longshore
Student

Re: How do I write Test class in Salesforce for class which updates Bill to and Sold to Address at Z

I tried to update my class with your method but I am getting error as "Method does not exist or incorrect signature: void logCurrentCallout(String) from the type Zuora_UpdateBillToAddress at line 35 column 22"

 

Below is my class

 

public class Zuora_UpdateBillToAddress
{
@future (callout=true)
public static void updateBillTo(String zBillAddress1, String zBilltoId,String zBillAddress2,String zBillCity,String zBillState,String zBillPostal,String zBillCountry,String zBillWPhone,String zBillFax)
{ List<Zuora__CustomerAccount__c> Accounts2 = new List<Zuora__CustomerAccount__c>();
String fName;
String lName;
List < Zuora__CustomerAccount__c > Accounts = new List < Zuora__CustomerAccount__c > ();
for (Zuora__CustomerAccount__c contact: [Select Zuora__BillToId__c,Zuora__BillToName__c,Zuora__BillToAddress1__c,Zuora__BillToAddress2__c,Zuora__BillToCity__c,Zuora__BillToState__c,Zuora__BillToPostalCode__c,Zuora__BillToCountry__c,Zuora__BillToWorkPhone__c,Zuora__BillToFax__c from Zuora__CustomerAccount__c where Zuora__BillToId__c =:zBilltoId AND Zuora__BillToAddress1__c =: zBillAddress1 AND Zuora__BillToAddress2__c =:zBillAddress2 AND Zuora__BillToCity__c =:zBillCity AND Zuora__BillToState__c =:zBillState AND Zuora__BillToPostalCode__c =:zBillPostal AND Zuora__BillToCountry__c =:zBillCountry AND Zuora__BillToWorkPhone__c =:zBillWPhone AND Zuora__BillToFax__c =:zBillFax ])
{
fName= (contact.Zuora__BillToName__c).SubStringBefore(' ');
lName = (contact.Zuora__BillToName__c).SubStringAfter(' ');
try {
Zuora.zApi zapiInstance = new Zuora.zApi();
zApiInstance.setEndpoint(System.label.Zuora_Endpoint_Url);
if(!Test.isRunningTest())
{
zApiInstance.zlogin(System.label.Zuora_Username,System.label.Zuora_Password); }
Zuora.zObject con = new Zuora.zObject('Contact');
con.setValue('Id',contact.Zuora__BillToId__c ); //an existing account
con.setValue('FirstName',fName);
con.setValue('LastName',lName);
con.setValue('Address1',contact.Zuora__BillToAddress1__c);
con.setValue('Address2',contact.Zuora__BillToAddress2__c);
con.setValue('City',contact.Zuora__BillToCity__c);
con.setValue('State',contact.Zuora__BillToState__c);
con.setValue('PostalCode',contact.Zuora__BillToPostalCode__c);
con.setValue('Country',contact.Zuora__BillToCountry__c);
con.setValue('WorkPhone',contact.Zuora__BillToWorkPhone__c);
con.setValue('Fax',contact.Zuora__BillToFax__c);
List<Zuora.zObject> objs = new List<Zuora.zObject> {con};
List<Zuora.zApi.SaveResult> results = zApiInstance.zupdate(objs);
if(!Test.isRunningTest()){
results = zApiInstance.zupdate(objs);
logCurrentCallout('UpdateZObjects');
}
else {
results = new List<Zuora.zApi.saveResult>();
Zuora.zApi.saveResult testSaveResultSuccess = ZuoraZApiInstanceFactory.saveResult();
testSaveResultSuccess.success=true;
results.add(testSaveResultSuccess);
}
}catch(Exception e){
system.debug('Exception at Zuora callout' +e.getMessage());
} }
}
}

Guru

Re: How do I write Test class in Salesforce for class which updates Bill to and Sold to Address at Z

My code was an example and includes references to how my code is strucured.

 

you need to restructure yours. If I get time toay I will show you

Maggie Longshore
Student

Re: How do I write Test class in Salesforce for class which updates Bill to and Sold to Address at Z

Thanks Maggie for your Reply,

 

Please show me how to restucture it.

Guru

Re: How do I write Test class in Salesforce for class which updates Bill to and Sold to Address at Z

It is not as simple as restructuring for the test. Your code has several issues - I do not believe it does what you want it to do.

 

The send to Zuora should not be in the for loop and I do not understand why the query was written like this to query based upon your input data.

 

Are your users updating the Customer Account in Salesforce to trigger this? The Salesforce Customer Account object is a copy of Zuora from the 360 syc and you should not allow edits of it.

 

I suggest you work with Zuora for a solution if you do not have a developer on staff that can rewrite your trigger..

 

 

Maggie Longshore