You need to sign in to do that
Don't have an account?
Sai Theja
Help me in code coverage of webservice class
Hello,
Below is my Class:
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())); } //} } }
Test.setMock(HttpCalloutMock.class, new ClientMakeMockHttpResponse());