# CREATE and ASSIGN a STRING VARIABLE IN URCAP

**URL:** <https://forum.universal-robots.com/t/create-and-assign-a-string-variable-in-urcap/17856>\
**Category:** URCap Development\
**Created:** [November 26, 2021, 12:48pm UTC](https://forum.universal-robots.com/t/create-and-assign-a-string-variable-in-urcap/17856 "2021-11-26T12:48:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eyas](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eyas](https://forum.universal-robots.com/u/eyas)\
**Post date:** [November 26, 2021, 12:48pm UTC](https://forum.universal-robots.com/t/create-and-assign-a-string-variable-in-urcap/17856/1 "2021-11-26T12:48:23Z")

</div>

Hello,  
I am developing a urcap and I have the following error:  
I would like to create a variable in the urcap and assign this variable to a String value that is going to read it from another device,  
I could create the variable, set the variable and get selected variable using the following functions:

public Variable getSelectedVariable(){  
return model.get(KEY\_REPLY\_FROM\_SERVER, (Variable)null);  
}

```
public void setVariable(final Variable variable){
    undoRedoManager.recordChanges(new UndoableChanges() {

        @Override
        public void executeChanges() {
           model.set(KEY_REPLY_FROM_SERVER, variable);    
        }
    });
}

public GlobalVariable createGlobalVariable(String variableName){
    GlobalVariable variable = null;
    try {
        variable = variableFactory.createGlobalVariable(variableName, programAPI.getValueFactoryProvider().createExpressionBuilder().append("0").build());
    } catch (VariableException variableException) {
        System.out.println("Could not crerate the variable. ERROR " + variableException);
        variableException.printStackTrace();
    } catch (InvalidExpressionException e) {
        e.printStackTrace();
    }
    return variable;
}

```

public void generateScript(ScriptWriter writer) {  
Variable errorReset = getSelectedVariable();  
if (errorReset != null) {  
String errorVariableStringValue = writer.getResolvedVariableName(errorReset);  
[//errorVariableStringValue.toString](https://errorVariableStringValue.toString)();  
writer.appendLine(errorVariableStringValue + “=” +“"” + “SHOULD BE STRING NOT INT” + “"”);  
}

and everything is working well if I assign the variable to an integer value, but I need to assign this variable to a string value…

 ![error](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/0/04d69664ba50f74b474b5ebb3c5864c5f93147c4.png)

do you have any proposal.?

Best Regards.

---

<div class="post-metadata">

**Author:** ![Ebbe](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/ebbe/32/1413_2.png) [@Ebbe](https://forum.universal-robots.com/u/Ebbe)\
**Post date:** [November 29, 2021, 1:43pm UTC](https://forum.universal-robots.com/t/create-and-assign-a-string-variable-in-urcap/17856/2 "2021-11-29T13:43:08Z")

</div>

Hi @eyas,

Welcome to the UR community!

Your issue is that URScript variables cannot change type. It looks like you initiated it with an integer.

So I would suggest you make the [createGlobalVariable(String)](https://plus.universal-robots.com/apidoc/115998/com/ur/urcap/api/domain/variable/VariableFactory.html#createGlobalVariable-java.lang.String-) method instead of [createGlobalVariable(String, Expression)](https://plus.universal-robots.com/apidoc/115998/com/ur/urcap/api/domain/variable/VariableFactory.html#createGlobalVariable-java.lang.String-com.ur.urcap.api.domain.value.expression.Expression-)

Hope that solves your issue.

Best regards  
Ebbe

---

<div class="post-metadata">

**Author:** ![eyas](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eyas](https://forum.universal-robots.com/u/eyas)\
**Post date:** [December 2, 2021, 6:36am UTC](https://forum.universal-robots.com/t/create-and-assign-a-string-variable-in-urcap/17856/3 "2021-12-02T06:36:44Z")

</div>

Hi @Ebbe  
Thanks for your reply, and welcoming me.  
there is no error any more but something is still missing, if you check the image attached,  
I could get an output as a variable(String) in the program(teach pendant) but I could not have the same value in the terminal, that I need to use the result in the URCAP or clearly in the background,

even if I use : String errorVariableStringValue = writer.getResolvedVariableName(errorReset); and assign it to a String, the output in the terminal is the same.  
For further information or explanation, please let me know!!

 ![output](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/f/f758cd69aef57d44e22250da525f3d7de1c00668.jpeg)

---

<div class="post-metadata">

**Author:** ![anon32693620](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@anon32693620](https://forum.universal-robots.com/u/anon32693620)\
**Post date:** [December 2, 2021, 10:00pm UTC](https://forum.universal-robots.com/t/create-and-assign-a-string-variable-in-urcap/17856/4 "2021-12-02T22:00:18Z")

</div>

On the java side errorReset is a variable that is equal to the String “errorVariable”; while on the script side you are creating a variable called errorVariable that is equal to the String “testingOutput” so the System.out call is displaying the correct String on the defined variable.

Are you trying to use the value of the script variable on the Java side as well? Or are you just trying to see it in this case?

---

<div class="post-metadata">

**Author:** ![eyas](https://avatars.discourse-cdn.com/v4/letter/e/c77e96/32.png) [@eyas](https://forum.universal-robots.com/u/eyas)\
**Post date:** [December 3, 2021, 5:31am UTC](https://forum.universal-robots.com/t/create-and-assign-a-string-variable-in-urcap/17856/5 "2021-12-03T05:31:20Z")

</div>

I need to use the variable in the urcap(java side) !

---

<div class="post-metadata">

**Author:** ![Ebbe](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/ebbe/32/1413_2.png) [@Ebbe](https://forum.universal-robots.com/u/Ebbe)\
**Post date:** [December 17, 2021, 9:39am UTC](https://forum.universal-robots.com/t/create-and-assign-a-string-variable-in-urcap/17856/6 "2021-12-17T09:39:22Z")

</div>

A convenient way to serialize data from the controller to the java side is XML-RPC. You can find one sample via a Daemon process here:

> **[GitHub - UniversalRobots/MyDaemonSwing: URCap sample that mainly demonstrates...](https://github.com/UniversalRobots/MyDaemonSwing)**
>
> URCap sample that mainly demonstrates the principle of DaemonContribution and DaemonService - GitHub - UniversalRobots/MyDaemonSwing: URCap sample that mainly demonstrates the principle of DaemonCo...
