Corret way to use the CRMscriptAgent in Online

Hello, 

I need to create some attachments in Online (from a customApp) and have figured i need to create a CRMscript that creates this attachment for me since i cant  use the standard API to do this. 

I have tried using the CRMscriptAgent to execute the script, and pass inn a string representation of the byteArray as a parameter: 

try
     {
     var byteArray = httpClient.GetByteArrayAsync("document/4098?id=71&docid=2");
     string docString = System.Text.Encoding.UTF8.GetString(byteArray.Result);
     var docStringDictionary = new SuperOffice.CRM.Services.StringDictionary();
     docStringDictionary.Add("documentString", docString);
     using (var scAgent = new SuperOffice.CRM.Services.CRMScriptAgent())
           {
             var scEntity = scAgent.GetCRMScriptEntity(42);
             var temp = scEntity.Description;
             scAgent.ExecuteScript(42, docStringDictionary);
           }

     }
catch (AggregateException ex)
      {
        //log error
        ex.ToString();
      }

This is just an initial test if i can use this agent, and in the example above im able to get the crmscript.Description (temp) and other values. The script itself also validates OK if i use the ValidateScript-method. 

If i add any 'normal ways to fetch values in a script' it fails with this exception: 
Does anyone have an example on how i use the executeScript-method and how i fetch the parameters in the script? 
It looks like it fails if i use any of the global functions like getCgiContent(), getCgiVariableArray() etc. 

Edit: I also notice that if i enable tracing on a script it does not notice if my code is executed through the CrmScriptagent. If i edit my script to only continain this: 
 
It does not fail when executing it through the agent,and should in theory be 'ok and run just fine'. 

Am I doing this all wrong? 
I could try to do a POST from my customApp to the script instead, and get the byte[] with getCgiContent() instead (?), but it would be better to use the scriptAgent since i already have a valid session in my application. 


//Eivind

RE: Corret way to use the CRMscriptAgent in Online

Hello Eivind,

You need to encode the parameters you pass to the CRMScript you're executing.

Example:

                var byteArray = httpClient.GetByteArrayAsync("document/4098?id=71&docid=2");

                int CrmScriptId = 42;

                using (var CRMScriptAgent = new SuperOffice.CRM.Services.CRMScriptAgent())
                {
                    CRMScriptAgent.ExecuteScript(CrmScriptId, new SuperOffice.CRM.Services.StringDictionary 
                                                              { 
                                                                  { 
                                                                        "documentString", SuperOffice.CRM.Globalization.CultureDataFormatter.Encode(Encoding.UTF8.GetString(byteArray.Result)) 
                                                                  }
                                                              });
                }
Av: David Hollegien 29. nov 2019

RE: Corret way to use the CRMscriptAgent in Online

Hello, 

Thank you for your reply! 

And how to i fetch this in the script i execute? Most of the get-functions returns an error similar to this: 
"GeneralException:\nejScript function getCgiVariableArray() called without an EJWebApp instance!"" 


What i want is to ble able to do this: 
String base64 = getWhateverIsPassedInAsParameter();
byte[] bytes = decodeBase64(base64); 


//Eivind

Av: Eivind Johan Fasting 29. nov 2019

RE: Corret way to use the CRMscriptAgent in Online

Hello Eivind,

You can use the getVariable method to retrieve values your passed to the CRMScript:

getVariable("documentString")
Av: David Hollegien 29. nov 2019

RE: Corret way to use the CRMscriptAgent in Online

Figured it out: 

String base64 = getVariable("documentString");
byte[] bytes = decodeBase64(base64); 

Still a little puzzled why the trace dont work but its no biggie :)

Thanks!

//Eivind

Av: Eivind Johan Fasting 29. nov 2019

RE: Corret way to use the CRMscriptAgent in Online

Hello,

The trace propably isn't created since the CRMScript is called from NetServer, which in turns calls the CS installation. Maybe register this as a bug/wish?

 

Av: David Hollegien 29. nov 2019