web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

'Incorrect attribute value type System.String' while creating invoice through code

(0) ShareShare
ReportReport
Posted on by

Hi All,

we have a c# application which has to create Invoice in MS CRM 2013. Below snap shot shows the attributes and their values filled for the invoice object.

CRM-invoice-creation-exception.png

i get 'Incorrect attribute value type System.String' exception on the Guid id = _orgService.Create(crmInvoice); line of code.

for setting customerid have tried multiple below options but all give same error.

1st Approach

crmInvoice.Attributes["customerid"] = accountId;
crmInvoice.Attributes["customeridtype"] = 1;


on above approach failure tried 2nd below approach.

Entity customer = new Entity("customer");
customer.Id = accountId;
customer.Attributes["type"] = "account"; //1;
crmInvoice.Attributes["customerid"] = customer; 

when this also failed tried 3rd approach.

crmInvoice.Attributes["customerid"] = new EntityReference("account", accountId); 

This too gives same error. Kindly let me know where am going wrong. how to resolve this issue. It is turning out to be a night mare now

*This post is locked for comments

I have the same question (0)
  • Aileen Gusni Profile Picture
    44,524 on at

    Shah,

    It should work using this code:

    crmInvoice.Attributes["customerid"] = new EntityReference("account", accountId);

    Like you did in the 3rd approach.

    So this should not be the root.

    But..

    Make sure that your accounted is valid GUID with 32 characters uniqueidentifier.

    How you get the accountId?

    It also can be possible you got error from other attributes, you might post it also.

    It seems your pricelevel also incorrect.

    You should put entityreference instead of just give direct guid.

    Please use the same method for customerid I gave you before

    crmInvoice.Attributes["pricelevelid"] = new EntityReference("pricelevel", pricelevelguid);

    And also it is about string error.

    It can be you give wrong type to an attribute that it is a text type but you give maybe datetime, or integer.

    Hope this helps.

    Thanks.

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hello,

    Using EntityReference is correct way of setting lookup that you did in 3rd approach. It seems there is some other issue, you don't have full code here, so I will suggest you to catch faultexception instead of exception, it will give you detail error message what is happening :

    catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)

    {

    }

    You can refer: msdn.microsoft.com/.../gg327884.aspx

  • Pramod M Profile Picture
    1,445 on at

    Shah,

    Can you please let me know what is the type of new_qblistid, Is it a Entity reference or just a string?

  • Community Member Profile Picture
    on at

    Hello Aileen,

    Thanks for the reply. below is my code to fill invoice entity object values

    Entity crmInvoice = new Entity();
                    crmInvoice.Attributes["name"] = qbInvoice.CustomerRef.FullName.GetValue() + "-INV-" + qbInvoice.RefNumber.GetValue();
                    crmInvoice.Attributes["name"] = qbInvoice.CustomerRef.FullName.GetValue();
                    crmInvoice.Attributes["invoicenumber"] = qbInvoice.RefNumber.GetValue();
                    crmInvoice.Attributes["new_invoicehash"] = qbInvoice.RefNumber.GetValue();
                    RetrieveMultipleResponse retrievedResponse = CommonEntityOperation.getRecordIdByName("pricelevel", "name", "Kyra-base-price-list");//getPriceListID();
                    EntityCollection beCollection = retrievedResponse.EntityCollection;
                    //pricelevel priceList = (pricelevel)beCollection[0];
                    Entity priceList = beCollection[0];
                    Guid priceListID = (Guid)priceList.Attributes["pricelevelid"];
                    //crmInvoice.pricelevelid = new Lookup();
                    //crmInvoice.pricelevelid.Value = priceListID;
    
                    crmInvoice.Attributes["pricelevelid"] = new EntityReference();
                    crmInvoice.Attributes["pricelevelid"] = priceListID;
                    string customerListId = qbInvoice.CustomerRef.ListID.GetValue();
                    retrievedResponse = CommonEntityOperation.getRecordIdByName("account", "new_qblistid", customerListId);//getPriceListID();
                    beCollection = retrievedResponse.EntityCollection;
                    //account customerAccount = (account)beCollection[0];
                    Entity customerAccount = (Entity)beCollection[0];
                    Guid accountId = (Guid)customerAccount.Attributes["accountid"];
                    crmInvoice.Attributes["customerid"] = new EntityReference("account", accountId);
                    crmInvoice.Attributes["paymenttermscode"] = 200000 + paymentTermValue;
                    date = qbInvoice.TxnDate.GetValue();
                    crmInvoice.Attributes["datedelivered"] = date.ToString();
                    crmInvoice.Attributes["new_qbcreatedon"] = date.ToString();
                    crmInvoice.Attributes["new_ponumber"] = qbInvoice.PONumber.GetValue();
                    date = qbInvoice.DueDate.GetValue();
                    //crmInvoice.duedate = new COMMON.CrmSdk.CrmDateTime();
                    //crmInvoice.duedate.Value = date.ToString();
                    crmInvoice.Attributes["duedate"] = date.ToString();
                    crmInvoice.Attributes["new_class"] = qbInvoice.ClassRef.FullName.GetValue();
                    crmInvoice.Attributes["new_paidamount"] = new Money();
                    crmInvoice.Attributes["new_paidamount"] = Math.Abs(Convert.ToDecimal(qbInvoice.AppliedAmount.GetValue()));
                    crmInvoice.Attributes["statecode"] = 2; //Value of InvoiceState.Paid;
                    crmInvoice.Attributes["new_qblistid"] = qbInvoice.TxnID.GetValue();
                    DateTime syncDate = System.DateTime.Now;
                    syncDate = syncDate.AddMinutes(2);
                    //crmInvoice.new_syncupdate = new COMMON.CrmSdk.CrmDateTime();
                    crmInvoice.Attributes["new_syncupdate"] = syncDate.ToString();


    Note : GetValue() function returns a string.

    Kindly help me identifiying where am going wrong.

  • Community Member Profile Picture
    on at

    Hi Himbap, Thanks for your reply. I have added my full code for entity object property setting.

  • Community Member Profile Picture
    on at

    Hi Pramod,

    Thanks for your reply. It is a string. Have posted full code for filling invoice object properties in one of  the replies. Could please see wht could be worng in it

  • Aileen Gusni Profile Picture
    44,524 on at

    Shoab,

    There are lines you need to notice and amend it.

    I don't think you can just give the invoicenumber like this:

    For the entity reference, do not split like that, instead using the same concept with customerid and also for the new_paidamount, please use new Money(Math.Abs(Convert.ToDecimal(qbInvoice.AppliedAmount.GetValue())));

    And also crmInvoice.Attributes["new_qblistid"] =

    You sure this one is not an entityreference???

    crmInvoice.Attributes["statecode"] = 2;

    You cannot use this one..

    You can either later update it using the SetStateRequest or you need to use option set one

    crmInvoice.Attributes["statecode"] = new OptionSetValue(2);

    then also give its pair, the status code, make sure they are in pair one.

    And this one is a date or string

    crmInvoice.Attributes["new_syncupdate"] = syncDate.ToString();

    Even though you fix the string one, there are other things to fix.

    Thanks.

  • Community Member Profile Picture
    on at

    I am getting the same error with a field of type EntityReference. but I am very annoid that it doesn't specify which field, and I have several. Should I check every field? is this my only option?

  • Suggested answer
    Community Member Profile Picture
    on at

    try

    Guid id = (Guid)_orgService.Create((Entity)crmInvoice);

  • Community Member Profile Picture
    on at

    my question was: Why does it not tell me what field has the "Incorrect attribute value type"

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans