+ Start a Discussion
David HagenDavid Hagen 

code coverage in triggers can't increase

Hi, I've a trigger on insert and update task. I uses both API and Web to insert/update task. I would like to stop triggers when they are updated by API. So, added a filter to check if the user is API then do nothing. I can't increase the code coverage in the following code

trigger XmsTaskTrigger on Task (after insert, after update) {
    Task[] tsk = Trigger.new;
       String JSONString = JSON.serialize(tsk);
      if(UserInfo.getProfileId()!='API'){
       XmsTask.doCallout(JSONString);
    }
 }

If I remove   if(UserInfo.getProfileId()!='API') I get 94% code coverage otherwise i get 50%
Best Answer chosen by David Hagen
Mike.KatulkaMike.Katulka
First thing I see wrong is getProfileId will return an ID, not 'API'.  

Otherwise, you may need to run the unit test as certain users with certain profiles.

Help site looks down right now:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

What do the debug logs show?

All Answers

Mike.KatulkaMike.Katulka
First thing I see wrong is getProfileId will return an ID, not 'API'.  

Otherwise, you may need to run the unit test as certain users with certain profiles.

Help site looks down right now:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

What do the debug logs show?
This was selected as the best answer
David HagenDavid Hagen
Thank you for the reply. Actually I was using ID. If I use any other condition such as run as other user I still get the same issue.
Mike.KatulkaMike.Katulka
I see, then I'd be looking at the debug log next.  Track the log and trigger line number and see what happens.  Add system debugs to the trigger as well, debug that getProfileId variable to be sure, and put a debug inside the if statement.  This can rule out issues with "Code Coverage" being miscalculated, which is a known issue but with lots of cleanup you can fix. Doesn't look like you will get a quick answer on this without more investigation on your end via logging or something else.

Also try if(1=1) and see what happens... all good story building info.

If you uncover the issue please post back what you found.  Thanks!
David HagenDavid Hagen
Thanks Mike, I had to run the Unit test under different users which works fine.