locked
can I do calculation using dialog ?!! RRS feed

  • Question

  • Hi .. 

             I'm using CRM 2011 online, need a help regarding a requirement.. I  have a dialog which is triggering on custom button. Dialog will be updating the lead fields [ 4 fields] once it is finished. All these been done now.. the problem is once the dialog process is finished I need to take sum of 4 fields and  save it in a  new filed.  I'm not finding this sum operation using  dialog, there is only option to "multiply, increment etc.. 

    Since dialog updates the value asynchronously  ,  javascript calculation on save is out of the option  !!! I can't put this on-load as well coz these updates are the last operation user will be doing on lead. they might not open that record again...

    I tried with asynchronous plugin to  take sum of 4 fields. but this is not happening !!!


    Monday, August 12, 2013 11:21 AM

All replies

  • Hi,

    By far the easiest way to do this would be to use N52 formula manager - it's a free (community edition) small light weight solution that you can create Excel like formulas to calculate fields - see http://www.north52.com/formulamanager.html

    If you were going to do it using a plugin, it would have to be a synchronous one to ensure the values are available immediately.

    hth


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Monday, August 12, 2013 2:58 PM
    Answerer
  • Hi,

    By far the easiest way to do this would be to use N52 formula manager - it's a free (community edition) small light weight solution that you can create Excel like formulas to calculate fields - see http://www.north52.com/formulamanager.html

    If you were going to do it using a plugin, it would have to be a synchronous one to ensure the values are available immediately.

    hth


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thanks. ya that looks good.. 

    I was able to achieve this using asynchronous plugin, all I needed to do is to register the plugin in postimage. !! Still got problem though, i can only update once.. after that if i try to calculate again it is not working.. luckily for me client strictly want this to be an one time activity. there will not  be further update once the calculation is done  :) !!! 

    But out of curiosity, I would like to find out why the update is not happen for 2nd time ?!!!  


    Tuesday, August 13, 2013 4:47 AM
  • Hi There.

    Would think you may be able to use custom workflow as the last step before the dialog finishes to calculate the 4 input values and the custom workflow returns the result back to another field as [sum value] in the finish page. Then your dialog step could use this value and create the new record.

    If your plugin is only registered on create, postImage....it won't be fired the second time as it will be update event.

    Hope this helps.

    Jaimie

    Tuesday, August 13, 2013 3:59 PM
  • Hi There.

    Would think you may be able to use custom workflow as the last step before the dialog finishes to calculate the 4 input values and the custom workflow returns the result back to another field as [sum value] in the finish page. Then your dialog step could use this value and create the new record.

    If your plugin is only registered on create, postImage....it won't be fired the second time as it will be update event.

    Hope this helps.

    Jaimie

    Yes.. This is exactly what I was looking for... It would be great if I could do this with CRM 2011 online.  I haven't' created any custom workflows for CRM Online since it was not supporting before.

    And jaimie, I register this plugin on "Update" message , ya it's strange, it behaves like I've registered it on "create"

    And this is the simple code, which is doin the trick..

                Entity entity;

                if (context.PostEntityImages.Contains("PostImage") && context.PostEntityImages["PostImage"] is Entity)
                {

                    entity = (Entity)context.PostEntityImages["PostImage"];
                    try
                    {
                        if (entity.LogicalName == "lead")
                        {

                                int field1 = Convert.ToInt32(entity.Attributes["new_rh_rating_a"]);
                                int field2 = Convert.ToInt32(entity.Attributes["new_rh_rating_b"]);
                                int field3 = Convert.ToInt32(entity.Attributes["new_rh_rating_c"]);
                                int field4 = Convert.ToInt32(entity.Attributes["new_rh_rating_d"]);
                            
                               int sum = field1 + field2 + field3 + field4;                         
                               entity.Attributes.Add("new_rh_rating_total", sum);
                               service.Update(entity);
                            
                        }
                    }

     


    • Edited by Pradeep47 Wednesday, August 14, 2013 5:33 AM
    Wednesday, August 14, 2013 5:20 AM
  • Hi,

    Creating a custom workflow for CRM Online is very easy - you just need to remember to register the activity in the sandbox isolation mode.

    Your plugin code probably is only working on once because it is failing on the second Update due to the entity.Attributes.Add line.

    Because there is already an attribute value there - you'll get an exception.

    Use entity.Attributes["new_rh_rating_total"]= sum; 

    If you do an Advanced find for 'System Jobs' and filter where ErrorCode has data - you'll probably find that there are the Async Plugin messages in an error state.

    Hth,


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Wednesday, August 14, 2013 8:05 AM
    Answerer
  • Hi,

    Creating a custom workflow for CRM Online is very easy - you just need to remember to register the activity in the sandbox isolation mode.

    Your plugin code probably is only working on once because it is failing on the second Update due to the entity.Attributes.Add line.

    Because there is already an attribute value there - you'll get an exception.

    Use entity.Attributes["new_rh_rating_total"]= sum; 

    If you do an Advanced find for 'System Jobs' and filter where ErrorCode has data - you'll probably find that there are the Async Plugin messages in an error state.

    Hth,


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    yes, you are right, I can see failed messages in system jobs. I made change to code as you suggested but still issue persist.. 

    One thing i noticed though, it fails only when the values in those 4 fields are same for the 2nd update. meaning not updated while saving for second time..!! if i change the values of 4 fields manually and then update the record it calculates and gives the sum of it !!!! it rings a bell ,  the usual error "Given key is not present in the dictionary " occurs when the value is not found or value not changed  while udpate plugin is running !! 

    well, dat should happen while using "InputParameters" !! hmm... I'm using "PostEntityImages".

    can you help me to figure out where i'm going wrong ?!!!

    I'll give a try on custom workflow. let' c how it goes.. 

    Thanks for the help

    And on a completely different note,  :: how to debugg an asynchronous plugin ONLINE, if  you can help me with dat i can solve a lot of issues my self...now i don't have any clue what's going on with that code.. 





    • Edited by Pradeep47 Wednesday, August 14, 2013 8:55 AM
    Wednesday, August 14, 2013 8:51 AM
  • Hi,

    Debugging Dynamics CRM Online plugins is a case of looking at the exception and trace message (added using the ITracingService http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.itracingservice.aspx

    You will see the full error and trace message in the 'Details' tab of the System Job message (you need to expand it since it is collapsed by default).

    You can also use the Plugin Logger and playback mechanism - http://support.microsoft.com/kb/2778280

    hth


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Wednesday, August 14, 2013 8:57 AM
    Answerer
  • i got it. but it gives only a error log, i can't really debug the code :( 

    and this what it says.. 

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred in the plug-in.Detail: 
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147220891</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
        <KeyValuePairOfstringanyType>
          <d2p1:key>OperationStatus</d2p1:key>
          <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:int">0</d2p1:value>
        </KeyValuePairOfstringanyType>
      </ErrorDetails>
      <Message>An error occurred in the plug-in.</Message>
      <Timestamp>2013-08-14T08:28:52.4751823Z</Timestamp>
      <InnerFault>
        <ErrorCode>-2147204734</ErrorCode>
        <ErrorDetails xmlns:d3p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
        <Message>This workflow job was canceled because the workflow that started it included an infinite loop. Correct the workflow logic and try again. For information about workflow logic, see Help.</Message>
        <Timestamp>2013-08-14T08:28:52.708542Z</Timestamp>
        <InnerFault i:nil="true" />
        <TraceText i:nil="true" />
      </InnerFault>
      <TraceText>
    [AlertWhileClosingOpportunity: AlertWhileClosingOpportunity.Class1]
    [9a7f14ab-3503-e311-94fb-984be1731321: AlertWhileClosingOpportunity.Class1: Update of lead]

    </TraceText>
    </OrganizationServiceFault>

    can you see what is the issue here  ?!!


    • Edited by Pradeep47 Wednesday, August 14, 2013 9:36 AM
    Wednesday, August 14, 2013 9:31 AM
  • You've got an infinite loop issue.

    When you update - the update plugin fires again, which then causes the update to fire again for ever...

    You'll need to stop the race condition by checking the context.Depth, checking if the target contains any of the dependant fields, or putting a set of filtering attributes on your plugin step.

    hth


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Wednesday, August 14, 2013 9:43 AM
    Answerer
  • You've got an infinite loop issue.

    When you update - the update plugin fires again, which then causes the update to fire again for ever...

    You'll need to stop the race condition by checking the context.Depth, checking if the target contains any of the dependant fields, or putting a set of filtering attributes on your plugin step.

    hth


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thanks scott, I took care of infinite loop by adding  this line :  if (context.Depth > 1) { return; }

    Now, as I was guessing.. this is what error message says..

    Error Message:

    Unhandled Exception: Microsoft.Crm.CrmException: Unexpected exception from plug-in (Execute): XXXXXXXXXXX.Class1: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
       at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context)
       at Microsoft.Crm.Asynchronous.EventOperation.InvokePlugin(AsyncExecutionContext context, IPlugin pluginInstance)

    this is because, I dint' change those 4 field values before updating, i change something else in the record.  How can i fix this..?!!

    How can i put a check that enter only if the filed value changed ?!!  can i use..  

    entity.attributes.contains("xxxx") will this fix ma issue ?!!


    • Edited by Pradeep47 Wednesday, August 14, 2013 10:02 AM
    Wednesday, August 14, 2013 9:57 AM