2

I have an contact after trigger which collects data from Contact, add some personal, authentication pairs, and updates a 3rd party system.

While the insertion of account/contact in the test class is easy, I don't understand how do I test cover the creation of the json object.

Since the data it uses is hard coded.

These lines did (do?) not pass the test coverage:

enter image description here

The class:

trigger trgSF2NSContact on Contact (after update, after insert) {

String jsonData;

// Create a list of integers to write to the JSON string.
List<Id> idlist = new List<Id>();

for(Contact con : Trigger.new) {

    // Only update contacts for accounts that are in NS

    Account myAcct = [SELECT Id, Total_Closed_Opptys__c, NS_External_ID__c FROM Account WHERE Id = :con.AccountId ];
    if (myAcct.Total_Closed_Opptys__c == 0)
    {
        system.debug('Account not in NS');
        system.debug('                S H O U L D     R E T U R N !!!!');
        return;
    }

    system.debug('    Account is in Netsuite !!!!!!');

    JSONGenerator jsGen = JSON.createGenerator(true);

    system.debug('json debug' + json.serialize(con));
    jsGen.writeStartObject();

    jsGen.writeStringField('mappingtype', 'Contact_Mapping');
    jsGen.writeStringField('recordtype', 'Contact');
    jsGen.writeStringField('Name', con.LastName);
    if (con.FirstName != NULL) jsGen.writeStringField('FirstName', con.FirstName);
    jsGen.writeStringField('CurrencyIsoCode', con.CurrencyIsoCode);
    idlist.add(con.Id);
    jsGen.writeStringField('Id', con.Id);
    jsGen.writeStringField('SF_Account_id', myAcct.Id);

    if (con.Phone != NULL) jsGen.writeStringField('Phone', con.Phone);
    if (con.Email != NULL) jsGen.writeStringField('Email', con.Email);
    if (con.Title != NULL) jsGen.writeStringField('Title', con.Title);
    if (con.Fax != NULL) jsGen.writeStringField('Fax', con.Fax);

    jsGen.writeEndObject();

    jsonData = jsGen.getAsString();
    system.debug('jsonData DATA SENT TO NS !!!!! \n : ' + jsonData);
}

SF2NSHandleContact.handleContact(idlist, jsonData);
}

Test class :

> @isTest
public class trgSF2NSContactTest{
static testMethod void insertNewUser() {

    Account acctTest = new Account();
    accttest.Name = 'test account';
    acctTest.NS_External_ID__c =  '223344';
    accttest.BillingCountry = 'Israel';

    insert accttest;

    Contact contactToCreate = new Contact();
    contactToCreate.LastName = 'last';
    contactToCreate.FirstName = 'first';
    contactToCreate.CurrencyIsoCode= 'USD';
    contactToCreate.Phone = '0545558335';
    contactToCreate.Email = 'test@gmail.com';
    contactToCreate.Title = 'CIO';
    contactToCreate.Fax = '039888979'; 
    contactToCreate.MailingCountry = 'Israel'; 
    contacttocreate.AccountId = accttest.Id;


    insert ContactToCreate;
    }
}

So assuming the JSON is within a class, how do I test it?

  • 2
    Do you mean functional testing or in regards to code coverage ? – Samuel De Rycke Mar 13 '14 at 9:22
  • test coverage (adding test class) – Saariko Mar 13 '14 at 9:31
  • 1
    Off topic: you have a query into a loop, warn. On Topic: are you sure that is not returning (breaking the trigger) when evaluate if (myAcct.Total_Closed_Opptys__c == 0)? – Martin Borthiry Mar 13 '14 at 9:41
  • Yes, can you tell us a bit about Total_Closed_Opptys__c - is it a formula, rollup, integer? I believe your test class is probably not getting past the IF that Martin mentioned. – Simon Lawrence Mar 13 '14 at 9:48
  • 1
    Came on @Saariko you are a Triathlete! you could code anything ;) – Martin Borthiry Mar 13 '14 at 10:15
2

Off topic: you have a query into a loop, warn.

On Topic: are you sure that is not returning (breaking the trigger) when evaluates if (myAcct.Total_Closed_Opptys__c == 0)?

If Total_Closed_Opptys__c is a roll up, your json code will not be run.

In addition, your code in a bulk-safe way should look like this:

//.... same prev code here...
// prepare the list of account ids
Id[] accountIds = new Id[]{};
for(Contact con : Trigger.new) {
   accountIds.add(con.accountId);
}
// now you have to create an account map
map<id, Account> accounts = new Map<ID, Account>([SELECT Id, Total_Closed_Opptys__c, NS_External_ID__c FROM Account WHERE Id in :accountIds ]);



for(Contact con : Trigger.new) {

    // Only update contacts for accounts that are in NS
    // here you avoid the query by using the map.
    Account myAcct = accounts.get(con. AccountId);
  • I don't know why this answer was accepted, it might helped the OP with other issues but not with the mentioned issue - how to cover JsonGenerator code? – Json Oct 21 '18 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.