function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sai ThejaSai Theja 

Help me in code coverage of webservice class

Hello,
Below is my Class:
 
Global Class AgileClient{ 
public AgileClient(ApexPages.StandardController controller) {
}
webservice static void SyncClient()
{   
String currentRecordId;
currentRecordId  = ApexPages.CurrentPage().getparameters().get('id');
Account a = [SELECT Id,Name,BillingAddress,BillingCity,BillingCountry,BillingState,BillingStreet,BillingPostalCode,Client_City__c,Payroll_Contact__c,client_Id__c, Client_Address__C,Client_Zip__c ,Primary_Contact__c,Primary_Contact_Text__c,Primary_Contact__r.Phone,Primary_Contact__r.Email,Payroll_Contact_Text__c,Phone__c,Client_State__c , Contact_Name__c, Phone,(select Name, Email from contacts),(select Name from location__r)  from Account where id =: currentRecordId] ;  
//if(a.client_Id__c != null){
//ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'Already Synced with Agile'));
//}
//else{
JSONGenerator gen = JSON.createGenerator(true);    
gen.writeStartObject(); 
createJsonRequest.writeStringField(gen, 'name', a.name);
createJsonRequest.writeStringField(gen, 'city',a.BillingCity);
createJsonRequest.writeStringField(gen, 'address_line_1',a.BillingStreet);
createJsonRequest.writeStringField(gen, 'state_code',a.BillingState);
createJsonRequest.writeStringField(gen, 'zip_code',a.BillingPostalCode);
createJsonRequest.writeStringField(gen, 'sf_id',a.Id);
gen.writeFieldName('primary_contact_attributes');
gen.writeStartObject();        
createJsonRequest.writeStringField(gen, 'name',  a.Primary_Contact_Text__c);
createJsonRequest.writeStringField(gen, 'email', a.Primary_Contact__r.Email);
createJsonRequest.writeStringField(gen, 'phone', a.Primary_Contact__r.Phone);
gen.writeEndObject();    
gen.writeFieldName('payroll_contact_attributes');
gen.writeStartObject();        
createJsonRequest.writeStringField(gen, 'name', a.Payroll_Contact_Text__c);
createJsonRequest.writeStringField(gen, 'phone', a.Phone__c);  
gen.writeEndObject();    
    String jsonS = gen.getAsString();
System.debug('Client Data'+jsonS);

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api-qa.retrotax-aci.com/clients');
request.setMethod('POST');
request.setHeader('X-AUTH-TOKEN','1e3ed72468207930357c85c4717580d727f944936156ab40274049db7c6b77a84ce087f1de161725b196927d38be95b03a299f470a0f6c4c6ce19a254a0d42f4');
request.setHeader('X-API-KEY', 'yqvNrVR5Cs6vhLq1ZRPq38GM5OrXJ7C97n4BZCJa');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody(jsonS );
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() == 200) {
try {
    Account result = (Account)JSON.deserialize(response.getBody(), Account.class);
    a.client_Id__c = String.valueOf(result.id);
Update a;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Client Created Successfully'));

} catch(DmlException e) {
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, e.getMessage()));
}

//System.debug('Account:'+result.id);
// System.debug('Client Created Successfully: ' +
//   response.getStatusCode() + ' ' + response.getBody());
} else {
    System.debug('Unable to create Client: ' +
        response.getStatusCode() + ' ' + response.getBody());
     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, response.getBody()));
}
//}
}
}

 
Raj VakatiRaj Vakati
Use this code and add other fields 
 
@isTest
public class AgileClientTest {
    
    static testMethod void MyTest()
    {        
         Account acc1 = new Account();
        acc1.Name='acc1';
        acc1.BillingCity ='Chennai' ;
        acc1.BillingCountry='india';
        acc1.BillingLatitude=5657.577;
        acc1.BillingLongitude=7845.46;
        acc1.BillingPostalCode='600075';
        acc1.BillingState='tamil nadu';
        acc1.BillingStreet='water well street';  
        acc1.ShippingCity=null;
        acc1.ShippingCountry=null;
        acc1.ShippingLatitude=null;
        acc1.ShippingLongitude=null;
        acc1.ShippingPostalCode=null;
        acc1.ShippingState=null;
        acc1.ShippingStreet=null;
		// Add other fields required 
        insert acc1;

		
		Contact cont = new Contact();
		cont.FirstName='Test';
		cont.LastName='Test';
		cont.Email='test@gmail.com';
		// add other fields 
		cont.Accountid= acc1.id;
		insert cont;
		
		
		location__c loc = new location__c() ;
		loc.Account__c = acc1.Id ; 
		// add other fields 
		insert loc ; 
		
		
        PageReference pageRef = Page.YORPAGENAME;
        pageRef.getparameters().put('id', acc1.id);  
        Test.setCurrentPage(pageRef);
       
        Apexpages.StandardController sc = new Apexpages.StandardController(total);
        AgileClient ext = new AgileClient(sc);         
        AgileClient.SyncClient();      
    }
}

 
Raj VakatiRaj Vakati
Use this link in my code 


        Test.setMock(HttpCalloutMock.class, new ClientMakeMockHttpResponse());