The verse says “when”. Does this mean that there are times when there is an obligation to fight and times when there isn’t?
There is a well-known distinction in Jewish law between milchemet reshut, a “voluntary (i.e. expansionary) war” and milchemet mitzvah, “a compulsory (literally, commanded) war” which has become necessary in order for the nation to defend itself.
In the first case an exemption from military service is possible for psychological or certain other reasons (Deut. 20).
The second case is more difficult since everyone has a duty to protect their nation.
What then would happen if a person had real qualms of conscience and were highly troubled by government policy?
Refusing to fight would bring instant, severe punishment. But a fighter does not have to enjoy bringing harm to others even if they are the enemy. One can “work to rule” and not derive pleasure from carrying out superior orders.
What about asking to be transferred away from direct fighting?
In theory it is a possibility and reduces the element of direct responsibility for killing, but it does not remove the overall moral guilt and implies that a person is more moral than his colleagues, who might now be more determined to show what good soldiers they are.
May a person sabotage the national effort even when fighting or if he is assigned work behind the scenes?
This and all the other aspects of the question are highly complex moral as well as legal problems and none of the answers are easy.