简体   繁体   中英

CRM Plugin not working C#

I am making a plugin that is supposed to take data from Quote Product entity. By default, there is no discount in percent field, just discount amount. I added Discount% filed to the form ( ad_discountpercent ). Now I should calculate new price like this:

sum = (price - discount)*quantity*(100 - discount%)/100

I made a code for that. When i build this code there are no errors, but there are no changes in the CRM. I have also registered my plugin.

There are 3 fields manualdiscountamount , priceperunit , extendedamount that are Currency type fields. quantity is decimal and ad_discountpercent is integer. I have found a few methods for getting value from Currency, some are commented, some are not, because I don't know which one is proper.

One notice i checked fetch results, and they are as expected, so fetch is working fine. I compressed it in one line, because of the code length.

Here is the part of the code that should do the job. I have no information is there anything wrong with conversion, am I using proper way to set a new value? I am new in this, so any comment would help me.

string fetch1 = @"<fetch count='50' ><entity name='quotedetail' ><attribute name='manualdiscountamount' /><attribute name='priceperunit' /><attribute name='ad_discountpercent' /><attribute name='quantity' /> <attribute name='extendedamount' /> <filter> <condition attribute='ad_discountpercent' operator='not-null'/>   </filter >  </ entity>            </fetch>";            

EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetch1));

foreach (var c in result.Entities)
{           
    // int discountPer = (int)c.Attributes["ad_discountpercent"];
    int discountPer = c.GetAttributeValue<int>("ad_discountpercent");

    Money m = (Money)c.Attributes["priceperunit"];
    decimal price = m.Value;
    // decimal price = c.GetAttributeValue<decimal>("priceperunit");

    Money m1 = (Money)c.Attributes["manualdiscountamount"];
    decimal discount = m1.Value;
    // decimal discount = c.GetAttributeValue<decimal>("manualdiscountamount");

    //decimal discountPer = Convert.ToDecimal( c.Attributes["ad_discountpercent"]);

    decimal quantity = Convert.ToDecimal(c.Attributes["quantity"]);
    //decimal quantity = c.GetAttributeValue<decimal>("quantity");

    decimal sum = (price - discount) * quantity * (100 - discountPer) / 100;

    Money summTotal = new Money();
    summTotal.Value = sum;
    entity["extendedamount"] = summTotal;
    service.Update(entity);

ExtendedAmount is an auto-calculated field based on priceperunit and quantity . So any updates to this field will be ignored by CRM.

Instead update the priceperunit and quantity to get the desired extendedamount .

entity["priceperunit"] = new Money((price - discount) * quantity * (100 - discountPer) / 100);
entity["quantity"] = 1;
service.Update(entity);

Is it possible that your query isn't returning any data, therefore your loop is never dropped into and your update never executes?

As previously suggested, take a look at how to debug a plugin via the plugin profiler.

Or, if you don't want to do that. Throw an temporary exception in your code after you do the RetrieveMultiple and in the message, add in the result.Entities.Count value to confirm how many records you're getting back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM