mattkol / SugarRestSharp

SugarRestSharp is a .NET C# SugarCRM/SuiteCRM Rest API Client. It is .NET C# Wrapper for the SugarCRM/SuiteCRM REST API Client.
MIT License
30 stars 12 forks source link

CastException Error #6

Closed Manthan2611 closed 5 years ago

Manthan2611 commented 5 years ago

Hi,

I am getting the following error

System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'SugarRestSharp.Models.Case'.'

While the code behind is ` string moduleName = "Cases";

        var client = new SugarRestClient(url, username, password);
        var request = new SugarRestRequest(moduleName, RequestType.Create);

        Case c = new Case();
        c.AccountId = "75262eb1-2382-1d61-b3a4-5901ef67e373";
        c.Name = "API Testing";
        c.Description = "This is where i am going to write my tally problem";
        c.MobileNumber = "9999999999";
        c.Status = "New";
        c.SupportMode = "call";
        c.Priority = "P2";

        request.Parameter = c;

        // Select fields.
        List<string> selectFields = new List<string>();
        selectFields.Add(nameof(Case.AccountId));
        selectFields.Add(nameof(Case.Name));
        selectFields.Add(nameof(Case.Description));
        selectFields.Add(nameof(Case.MobileNumber));
        selectFields.Add(nameof(Case.Status));
        selectFields.Add(nameof(Case.TicketSource));
        selectFields.Add(nameof(Case.MobileNumber));
        selectFields.Add(nameof(Case.SupportMode));
        selectFields.Add(nameof(Case.Priority));

        request.Options.SelectFields = selectFields;
        SugarRestResponse response = client.Execute(request);          

        Case cres = (Case)response.Data;`

few weeks back it was working fine but from now i am getting the cast exception.

mattkol commented 5 years ago

There is no change to the code base since Feb 2018. No recent changes.

The only casting I see is the last line.

Case cres = (Case)response.Data;

May be you remove the cast and check for an object to see what is returned. Been a while I worked on this, and I may not be able to figure it out that quickly.

Try this and see why the cast is failing:

object cres = response.Data;

Also ensure you are using relevant SugarCrm version.

Manthan2611 commented 5 years ago

Hello,

Regrets for late reply. The following is the response i am getting in the object

3d708bde-a071-1fca-b8d3-5bf8e3a33bc0

I'm using the sugarcrm community edition 6.5

mattkol commented 5 years ago

Does not look right to me.

Please try see what the raw request and response is.

response.JsonRawRequest response.JsonRawResponse

Manthan2611 commented 5 years ago

Again Regrets for the delayed response

Below are the JsonRawRequest:

{"resource":"","parameters":[{"name":"method","value":"set_entry","type":"GetOrPost"},{"name":"input_type","value":"json","type":"GetOrPost"},{"name":"response_type","value":"json","type":"GetOrPost"},{"name":"rest_data","value":"{\"session\":\"8j8erj7tvr2qlj2bu0knuftni7\",\"module_name\":\"Cases\",\"name_value_list\":{\"name\":{\"name\":\"name\",\"value\":\"API Testing\"},\"description\":{\"name\":\"description\",\"value\":\"This is where i am going to write my tally problem\"},\"status\":{\"name\":\"status\",\"value\":\"New\"},\"priority\":{\"name\":\"priority\",\"value\":\"P2\"},\"account_id\":{\"name\":\"account_id\",\"value\":\"b8301ff8-9157-3144-2385-5aa25420ac0f\"},\"enjay_mobile_c\":{\"name\":\"enjay_mobile_c\",\"value\":\"9029171999\"},\"enjay_emailid_c\":{\"name\":\"enjay_emailid_c\",\"value\":\"manthan@cbditsolutions.com\"},\"enjay_case_source_c\":{\"name\":\"enjay_case_source_c\",\"value\":\"Tally\"},\"enjay_support_mode_c\":{\"name\":\"enjay_support_mode_c\",\"value\":\"call\"},\"assigned_user_name\":{\"name\":\"assigned_user_name\",\"value\":\"8dac4928-7275-1442-0f18-5abe0774348b\"}}}","type":"GetOrPost"},{"name":"Accept","value":"application/json, application/xml, text/json, text/x-json, text/javascript, text/xml","type":"HttpHeader"}],"method":"POST","uri":"https://srk.njcrm.in/service/v4_1/rest.php"}

Below are then JsonRawResponse:

{"statusCode":200,"content":"{\"id\":\"242c099d-c736-279a-5f93-5c0265a489ab\",\"entry_list\":{\"assigned_user_name\":{\"name\":\"assigned_user_name\",\"value\":\"8dac4928-7275-1442-0f18-5abe0774348b\"},\"name\":{\"name\":\"name\",\"value\":\"API Testing\"},\"description\":{\"name\":\"description\",\"value\":\"This is where i am going to write my tally problem\"},\"status\":{\"name\":\"status\",\"value\":\"New\"},\"priority\":{\"name\":\"priority\",\"value\":\"P2\"},\"account_id\":{\"name\":\"account_id\",\"value\":\"b8301ff8-9157-3144-2385-5aa25420ac0f\"},\"enjay_mobile_c\":{\"name\":\"enjay_mobile_c\",\"value\":\"9029171999\"},\"enjay_emailid_c\":{\"name\":\"enjay_emailid_c\",\"value\":\"manthan@cbditsolutions.com\"},\"enjay_case_source_c\":{\"name\":\"enjay_case_source_c\",\"value\":\"Tally\"},\"enjay_support_mode_c\":{\"name\":\"enjay_support_mode_c\",\"value\":\"call\"}}}","headers":[{"Name":"Pragma","Value":"no-cache","Type":3,"ContentType":null},{"Name":"Content-Length","Value":"753","Type":3,"ContentType":null},{"Name":"Cache-Control","Value":"no-store, no-cache, must-revalidate, post-check=0, pre-check=0","Type":3,"ContentType":null},{"Name":"Content-Type","Value":"application/json; charset=UTF-8","Type":3,"ContentType":null},{"Name":"Date","Value":"Sat, 01 Dec 2018 10:44:09 GMT","Type":3,"ContentType":null},{"Name":"Expires","Value":"Thu, 19 Nov 1981 08:52:00 GMT","Type":3,"ContentType":null},{"Name":"Set-Cookie","Value":"PHPSESSID=8j8erj7tvr2qlj2bu0knuftni7; path=/","Type":3,"ContentType":null},{"Name":"Server","Value":"Apache/2.4.7 (Ubuntu)","Type":3,"ContentType":null},{"Name":"X-Powered-By","Value":"PHP/5.5.9-1ubuntu4.26","Type":3,"ContentType":null}],"responseUri":"https://srk.njcrm.in/service/v4_1/rest.php","errorMessage":null}

Front end: 1

I've also passed owner (assign to) value but it is not getting saved.

mattkol commented 5 years ago

So I can see you are not getting an error and also looks like reasonable data coming back

"statusCode":200,"content":"{\"id\":\"242c099d-c736-279a-5f93-5c0265a489ab\",\"entry_list\":

So I will suggest that as the usual cast does not work, that you find a way to parse the raw Json response.

Also looking at the original request, the response you get is actually correct. You are requesting to create a model (var request = new SugarRestRequest(moduleName, RequestType.Create);) and the response of the creation is the id of the created model object (3d708bde-a071-1fca-b8d3-5bf8e3a33bc0).

Sorry I misled you with my earlier statement as I missed the "Create" part.

mattkol commented 5 years ago

Just more info. Since you are creating you should only cast to string. Please see wiki https://github.com/mattkol/SugarRestSharp/wiki/Create-Module

Manthan2611 commented 5 years ago

Sure will i.

As you can see in my above image i am passing the assigned user value but i am not able to received that value in front end.

Previously before opening the ticket i used to get cast json response. i was using the same for around 6-8 months and it suddenly stop working.

Thank you for your help ;)