Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

CustomWorkFlow calling the account name through plugins

Posted on by Microsoft Employee

Hi as i new to Crm i  dont have clear idea of custom Workflow. so can anyone help me out. i need to get the outputparameter Account name through  plugins. and then need to put in workflow as(Action )then the Account name should call shall any one help me out here is my plugin code.

namespace Workflowcustom1
{
public class Class1 : CodeActivity

{

[RequiredArgument]

[Input("Decimal input")]
public InArgument<string> AccountName { get; set; }

[Output("Retrieve")]
public OutArgument<string> s1 { get; set; }

protected override void Execute(CodeActivityContext context)
{
var workflowContext = context.GetExtension<IWorkflowContext>();
var serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
var service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
var account = this.AccountId.Get(context);
QueryExpression Retrieve = new QueryExpression("AccountName");
QueryExpression query = new QueryExpression("accounts");
query.ColumnSet = new ColumnSet(true);
EntityCollection retrieved = service.RetrieveMultiple(query);
string s1 = retrieved.Entities[0].GetAttributeValue<int>("AccountID").ToString();

}
}
}

i need to  retrieve the accountname through plugins.  can any one help

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: CustomWorkFlow calling the account name through plugins

    ? sign after FirstOrDefault() removes the need to check if value was returned or not. Check this as a reference - docs.microsoft.com/.../member-access-operators

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: CustomWorkFlow calling the account name through plugins

    and FirstOrDefault() will save you from an exception but you still have to test that s1 is not null, right?

    ps- this is not a plugin but a workflow assembly

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: CustomWorkFlow calling the account name through plugins

    Hello,

    Here are few corrections:

    Replace

    QueryExpression query = new QueryExpression("accounts");

    with

    QueryExpression query = new QueryExpression("account");

    Always use lowercase in entities/fields schema names so replace

    string s1 = retrieved.Entities[0].GetAttributeValue<int>("AccountID").ToString();

    with

    string s1 = retrieved.Entities[0].GetAttributeValue<int>("accountid").ToString();

    Also accountid will never be an int so your cast to int will fail. So replace

    string s1 = retrieved.Entities[0].GetAttributeValue<int>("accountid").ToString();

    with

    string s1 = retrieved.Entities[0].GetAttributeValue<Guid>("accountid").ToString();

    And one more - there is a chance that your query will not return the result so referencing first element of collection will fail with null reference exception so it's safer to use something like

    string s1 = retrieved.Entities.FirstOrDefault()?.GetAttributeValue<Guid>("accountid").ToString();

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans