$pk_related_accounts = Hash\New(); $pk_related_to_portal_user = record\fetchRelatedManyHash('User', $user_id, 'accounts', 'id', 'id', 'asc'); $pk_m = array\length($pk_related_to_portal_user); $pk_j = 0; while($pk_j < $pk_m, $pk_row = array\at($pk_related_to_portal_user, $pk_j); $pk_account_id = hash\get($pk_row, 'id'); $pk_related_accounts = Hash\Set($pk_related_accounts, $pk_account_id, 'del'); $pk_j = $pk_j + 1; ); $pk_related_to_contact = record\fetchRelatedManyHash('Contact', $contact_id, 'accounts', 'id', 'id', 'asc'); $pk_m = array\length($pk_related_to_contact); $pk_j = 0; while($pk_j < $pk_m, $pk_row = array\at($pk_related_to_contact, $pk_j); $pk_account_id = hash\get($pk_row, 'id'); ifThenElse(Hash\IsSet($pk_related_accounts, $pk_account_id), $pk_related_accounts = Hash\Set($pk_related_accounts, $pk_account_id, 'exist'); , $pk_related_accounts = Hash\Set($pk_related_accounts, $pk_account_id, 'new'); ); $pk_j = $pk_j + 1; ); $pk_keys = Hash\Keys($pk_related_accounts); $pk_m = array\length($pk_keys); $pk_j = 0; while($pk_j < $pk_m, $pk_key = array\at($pk_keys, $pk_j); $pk_val = Hash\Get($pk_related_accounts, $pk_key); logAdd(log, 'info', $i, ': ', $pk_key, '=', Hash\Get($pk_related_accounts, $pk_key)); ifThenElse($pk_val == 'del', record\unrelate('User', $user_id, 'accounts', $pk_key); logAdd(log, 'info', 'Unrelated ', $pk_key, ' from ', $username); , ifThen($pk_val == 'new', record\relate('User', $user_id, 'accounts', $pk_key); logAdd(log, 'info', 'related ', $pk_key, ' to ', $username); ); ); $pk_j = $pk_j + 1; );