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)

Custom Action Not working

(0) ShareShare
ReportReport
Posted on by 40

Dear All,

I have written a sample action which is not working somehow;
Please check the below references and help me out to solve this.

Entity;
pastedimage1568369703051v1.png

Custom Action Code:

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using Newtonsoft.Json;
using System;
using System.Activities;
using System.Collections.Generic;
using System.IO;
using System.Net;

namespace DataClone
{
public class CarRentalAction : CodeActivity
{
[Input("Currency Code")]
public InArgument<string> CurrencyCode { get; set; }
[Input("Total Price")]
public InArgument<decimal> TotalPrice { get; set; }

[Output("Converted Price")]
public OutArgument<decimal> TotalPriceinEURO { get; set; }

public class Currency
{
public Dictionary<string, decimal> rates;
[JsonProperty("base")]
public string Base { get; set; }
public string date;
}

protected override void Execute(CodeActivityContext context)
{
var workflowContext = context.GetExtension<IWorkflowContext>();
var serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
var orgService = serviceFactory.CreateOrganizationService(workflowContext.UserId);


TotalPriceinEURO.Set(context, Convert.ToDecimal(TotalPrice.ToString()) * 100);

}
}
}

Action:

pastedimage1568369896927v2.png

JavaScript Which Trigger the action on Save of entity to set TotalPriceinEURO field:

function CallmyAction(executionContext) {

debugger;
var formContext = executionContext.getFormContext();

var Id = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');

var CurrencyCode = formContext.getAttribute("my_name").getValue();
var TotalPrice= formContext.getAttribute("my_totalprice").getValue();

var parameters = {};
var entity = {};
entity.id = Id;
entity.entityType = "my_customentity1";
parameters.entity = entity;
parameters.CurrencyCode = CurrencyCode;
parameters.TotalPrice = TotalPrice;

var my_myActionRequest = {
entity: parameters.entity,
CurrencyCode: parameters.CurrencyCode,
TotalPrice: parameters.TotalPrice,

getMetadata: function() {
return {
boundParameter: "entity",
parameterTypes: {
"entity": {
"typeName": "mscrm.my_customentity1",
"structuralProperty": 5
},
"CurrencyCode": {
"typeName": "Edm.String",
"structuralProperty": 1
},
"TotalPrice": {
"typeName": "Edm.Decimal",
"structuralProperty": 1
}
},
operationType: 0,
operationName: "my_myAction"
};
}
};

Xrm.WebApi.online.execute(my_myActionRequest).then(
function success(result) {
if (result.ok) {
var results = JSON.parse(result.responseText);
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
}

*This post is locked for comments

I have the same question (0)
  • Praveen Pesaramilli Profile Picture
    40 on at

    If I use other JS (XMLHTTP)

    JS-Pblm.JPG

  • Suggested answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at

    I think you mixed up the things. https://community.dynamics.com/crm/b/magnetismsolutionscrmblog/posts/how-to-trigger-plugins-on-custom-messages-using-actions-in-dynamics-365

    1. You can have a custom action to take input parameters, register a plugin on that Action name as message in PRT, copy that custom workflow activity logic into plugin Execute method, which results the output parameter to JS method itself

    2. You can have custom workflow to invoke that custom WF activity by taking input parameters & outputting the result needed as such your code logic

    Verify the code execution flow by throwing some exception or tracing.

  • Praveen Pesaramilli Profile Picture
    40 on at

    I didnt get you actually;

    My requirement is that

    Write an action which takes 2 input parameters and set an output parameter upon Save or onchange of field any of input field.

    Can you please let me know, How to achieve this? by taking ref. on what steps I have done.

    What Actually I am doing is; 

    OnSave of form through JS, I am calling system Action; Which intern calling CWF registered taking inputs either in Step itself of from JS; And Assigning Output to intented field;
    Is this process is wrong?

    I am thinking below JS is calling Action:: but throwing an error like this

    "An unexpected error occurred from ISV code. (ErrorType = ClientError) Unexpected exception from plug-in (Execute):  DataClone.CarRentalAction: System.FormatException: Input string was not in a correct format."

    function CallmyAction(executionContext) {

    debugger;
    var formContext = executionContext.getFormContext();

    var Id = Xrm.Page.data.entity.getId().replace('{', '').replace('}', '');

    var CurrencyCode = formContext.getAttribute("my_name").getValue();
    var TotalPrice= formContext.getAttribute("my_totalprice").getValue();

    var parameters = {};
    var entity = {};
    entity.id = Id;
    entity.entityType = "my_customentity1";
    parameters.entity = entity;
    parameters.CurrencyCode = CurrencyCode;
    parameters.TotalPrice = TotalPrice;

    var my_myActionRequest = {
    entity: parameters.entity,
    getMetadata: function() {
    return {
    boundParameter: "entity",
    parameterTypes: {
    "entity": {
    "typeName": "mscrm.my_customentity1",
    "structuralProperty": 5
    }
    },
    operationType: 0,
    operationName: "my_myAction"
    };
    }
    };

    Xrm.WebApi.online.execute(my_myActionRequest).then(
    function success(result) {
    if (result.ok) {
    var results = JSON.parse(result.responseText);
    }
    },
    function(error) {
    Xrm.Utility.alertDialog(error.message);
    }
    );
    }

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