Skip to main content

Notifications

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

  • Community Member Profile Picture
    on at
    RE: 'Incorrect attribute value type System.String' while creating invoice through code

    make sure your account is valid it is also possible that you are getting erorr from other GUID attributes ,you can put entity reference us the same method of Customer ID .

  • Suggested answer
    MAJBlackburn Profile Picture
    80 on at
    RE: 'Incorrect attribute value type System.String' while creating invoice through code

    I had this same issue where I was not getting the attribute that was failing. I was using a workflow action, so I could see the error, but no info about the specific attribute that was causing the problem. I was having issues getting the trace files (no Sandbox trace), so I changed the registration of the dll to be out of the sandbox. Lo and behold, the error trace printed into the workflow dialog box included the offending field (of literally hundreds, so thank goodness). Obviously, this isn't an option for everyone, but it does help a lot if it is possible.

  • Community Member Profile Picture
    on at
    RE: 'Incorrect attribute value type System.String' while creating invoice through code

    oh, sorry. well my idea was trying to hard-type the id.

    I'd run all the attributes though a tracert just to see what kind of values they return (string, in, etc), especially when running .GetValue()

  • Community Member Profile Picture
    on at
    RE: 'Incorrect attribute value type System.String' while creating invoice through code

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

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: 'Incorrect attribute value type System.String' while creating invoice through code

    try

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

  • Community Member Profile Picture
    on at
    RE: 'Incorrect attribute value type System.String' while creating invoice through code

    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?

  • Aileen Gusni Profile Picture
    44,524 on at
    RE: 'Incorrect attribute value type System.String' while creating invoice through code

    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
    RE: 'Incorrect attribute value type System.String' while creating invoice through code

    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

  • Community Member Profile Picture
    on at
    RE: 'Incorrect attribute value type System.String' while creating invoice through code

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

  • Community Member Profile Picture
    on at
    RE: 'Incorrect attribute value type System.String' while creating invoice through code

    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.

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,120 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans