Set the value of a lookup field

Set the value of a lookup field

I have a form with a dropdown question and below that a lookup field. I wrote a code as below to set the value of the lookup field according to what is selected in the dropdown question above:

if (input.Use  ==  "R&D")
\\ if R&D is selected in the dropdown question
{
   input.Type_of_Sale = Types_of_Sale[Type_of_Sale == "R&D"].ID;
\\set the value of the lookup field (Type of Sale) to the ID # of the R&D type of sale 
}

Because it is a lookup field for the form: Types of Sale, I had to set it to the ID# associated with R&D in that form. My problem is that it has worked a few times sporadically, but most of the time it does not work. I cannot figure out why sometimes it does set the value and other times it does not. Any ideas would be appreciated!