Set value of a webform field programatically
I don't know if your problem could be solved in an easier way. A lot of our solutions at my workplace seem equally convoluted. In fact, we use WebformSubmissions to hold a lot of submitted data. 

Here is how you set a value in a WebformSubmission:


Load the submission.
Get the submission's data as an associative array.
Change the submission's data however you want.
Set the submission's data to this modified array.
Save the submission.


Here is the code:

use Drupal\webform\Entity\WebformSubmission;

// $sid contains the submission id
$submission = WebformSubmission::load($sid);

$data = $submission-&gt;getData();

// you can find the machine name in the devel tab
// or just do what I do and var_dump($data);
$data["field_machine_name"] = "new value";

$submission-&gt;setData();
$submission-&gt;save();