|
| 1 | +--- |
| 2 | +group: graphql |
| 3 | +title: createCompanyUser mutation |
| 4 | +contributor_name: Atwix |
| 5 | +contributor_link: https://www.atwix.com/ |
| 6 | +b2b_only: true |
| 7 | +--- |
| 8 | + |
| 9 | +The `createCompanyUser` mutation allows an existing company user who is assigned a role that contains the `Magento_Company::users_edit` permission to create a new company user. The specified email address determines how Magento processes the request. |
| 10 | + |
| 11 | +- If the email address is unique for the website, Magento immediately creates the company user. |
| 12 | + |
| 13 | +- If the email address belongs to a customer who is not a company user, Magento sends an invitation to join the company organization to the customer. When the customer accepts the invitation, Magento adds the customer to the company organization. |
| 14 | + |
| 15 | +- If the email address belongs to a customer who is part of any company organization, Magento returns the error "A customer with the same email already assigned to company". |
| 16 | + |
| 17 | +The `target_id` input attribute allows you to specify which node in the company structure will be the parent node of the company user. If you do not specify a value, the user will be assigned to the top-level (root) node of the company structure. |
| 18 | + |
| 19 | +You can get the `target_id` and the `role_id` with the [`company`]({{page.baseurl}}/graphql/queries/company.html) query. |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +```graphql |
| 24 | +mutation { |
| 25 | + createCompanyUser( |
| 26 | + input: CompanyUserCreateInput! |
| 27 | + ) { |
| 28 | + CreateCompanyUserOutput |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Example usage |
| 34 | + |
| 35 | +### Create a company user (minimal payload) |
| 36 | + |
| 37 | +The following example shows the minimal payload to add a company user. Because a `target_id` is not specified, Magento places the new company user at the top node of the company structure. |
| 38 | + |
| 39 | +**Request:** |
| 40 | + |
| 41 | +```graphql |
| 42 | +mutation { |
| 43 | + createCompanyUser( |
| 44 | + input: { |
| 45 | + |
| 46 | + firstname: "John" |
| 47 | + lastname: "Doe" |
| 48 | + job_title: "User" |
| 49 | + role_id: "MQ==" |
| 50 | + status: ACTIVE |
| 51 | + telephone: "1234567890" |
| 52 | + } |
| 53 | + ) { |
| 54 | + user { |
| 55 | + created_at |
| 56 | + email |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +**Response:** |
| 63 | + |
| 64 | +```json |
| 65 | +{ |
| 66 | + "data": { |
| 67 | + "createCompanyUser": { |
| 68 | + "user": { |
| 69 | + "created_at": "2020-10-15 23:33:49", |
| 70 | + |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Create a company user in a specific location in the company structure |
| 78 | + |
| 79 | +This example creates a new company user of the parent company team specified in the `target_id` field. |
| 80 | + |
| 81 | +**Request:** |
| 82 | + |
| 83 | +```graphql |
| 84 | +mutation { |
| 85 | + createCompanyUser( |
| 86 | + input: { |
| 87 | + |
| 88 | + firstname: "Jane" |
| 89 | + lastname: "Doe3" |
| 90 | + job_title: "User" |
| 91 | + role_id: "NTc=" |
| 92 | + status: ACTIVE |
| 93 | + telephone: "1234567890" |
| 94 | + target_id: "OA==" |
| 95 | + } |
| 96 | + ) { |
| 97 | + user { |
| 98 | + created_at |
| 99 | + email |
| 100 | + firstname |
| 101 | + lastname |
| 102 | + job_title |
| 103 | + role { |
| 104 | + id |
| 105 | + name |
| 106 | + } |
| 107 | + team { |
| 108 | + id |
| 109 | + name |
| 110 | + structure_id |
| 111 | + } |
| 112 | + status |
| 113 | + telephone |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +**Response:** |
| 120 | + |
| 121 | +```json |
| 122 | +{ |
| 123 | + "data": { |
| 124 | + "createCompanyUser": { |
| 125 | + "user": { |
| 126 | + "created_at": "2020-10-15 23:39:11", |
| 127 | + |
| 128 | + "firstname": "Jane", |
| 129 | + "lastname": "Doe", |
| 130 | + "job_title": "User", |
| 131 | + "role": { |
| 132 | + "id": "NTc=", |
| 133 | + "name": "Default User" |
| 134 | + }, |
| 135 | + "team": { |
| 136 | + "id": "MQ==", |
| 137 | + "name": "Test Team", |
| 138 | + "structure_id": "Mg==" |
| 139 | + }, |
| 140 | + "status": "ACTIVE", |
| 141 | + "telephone": "1234567890" |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +## Input attributes |
| 149 | + |
| 150 | +The `CompanyUserCreateInput` input object defines the company user data. |
| 151 | + |
| 152 | +### CompanyUserCreateInput attributes {#CompanyUserCreateInput} |
| 153 | + |
| 154 | +The `CompanyUserCreateInput` object contains the following attributes: |
| 155 | + |
| 156 | +Attribute | Data Type | Description |
| 157 | +--- | --- | --- |
| 158 | +`email` | String! | The company user's email address |
| 159 | +`firstname` | String! | The company user's first name |
| 160 | +`job_title` | String! | The company user's job title or function |
| 161 | +`lastname` | String! | The company user's last name |
| 162 | +`role_id` | ID! | The role ID to assign to the company user |
| 163 | +`status` | CompanyUserStatusEnum! | Indicates whether the company user is ACTIVE or INACTIVE |
| 164 | +`target_id` | ID | The ID of a node within a company's structure. This ID will be the parent of the created company user |
| 165 | +`telephone` | String! | The company user's phone number |
| 166 | + |
| 167 | +## Output attributes |
| 168 | + |
| 169 | +The `CreateCompanyUserOutput` output object contains the following attribute: |
| 170 | + |
| 171 | +Attribute | Data Type | Description |
| 172 | +--- | --- | --- |
| 173 | +`user` | Customer! | Contains company user data |
| 174 | + |
| 175 | +### Customer attributes {#Customer} |
| 176 | + |
| 177 | +{% include graphql/customer-output-24.md %} |
| 178 | + |
| 179 | +## Errors |
| 180 | + |
| 181 | +Error | Description |
| 182 | +--- | --- |
| 183 | +`Invitation was sent to an existing customer, they will be added to your organization once they accept the invitation.` | The email provided in the `input`.`email` argument belongs to an existing customer. Magento will send an invitation to this customer. When the customer accepts the invitation, the customer will be assigned to the company. |
| 184 | +`A customer with the same email already assigned to company.` | The email provided in the `input`.`email` argument belongs to an existing customer, and the customer has already been assigned to the company. |
| 185 | +`"Email" is not a valid email address.` | The value provided in the `input`.`email` argument has an invalid format. |
| 186 | +`Field "createCompanyUser" argument "input" requires type String!, found xxx.` | The value specified in the one of the `input` arguments has an invalid type. |
| 187 | +`Field "xxx" is not defined by type CompanyUserCreateInput.` | The `input`.`xxx` argument is undefined. |
| 188 | +`Required parameters are missing: xxx` | The `input`.`xxx` argument was omitted or contains an empty value. |
| 189 | +`No such entity with roleId = xxx` | The company role with ID `xxx` doesn't exist. |
0 commit comments