Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crm tokens are returning null values #26

Open
zahrabatool opened this issue Nov 15, 2022 · 4 comments
Open

Crm tokens are returning null values #26

zahrabatool opened this issue Nov 15, 2022 · 4 comments

Comments

@zahrabatool
Copy link

Hi Jonas, 
I tried reusing XRM token with classic plugin code but it seems I am missing something because the token method is returning nulls against each tag. Appreciate if you can help me identify the issue. Thanks. Abbas

following is the code:

using System;
using System.Collections.Generic;
using System.IdentityModel.Metadata;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Dynamic;
using Rappen.XRM.Tokens;
using System.Runtime.CompilerServices;

namespace SamplePlugin
{
public class AccountUpdate : IPlugin
{

    IOrganizationService service; Entity currentEntity;
    IPluginExecutionContext context;
    ITracingService trace;

    

   private void GetOrganization(IServiceProvider serviceProvider)
    {
        try
        {

            context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(context.UserId);
            trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

        }
        catch(Exception ex)
        { 
        }

    }
    private void GetRecordContext()
    {
        try
        {
            currentEntity = (Entity)context.InputParameters["Target"];
            if (currentEntity.Attributes.Contains("new_groupcode") )
            {
                string groupCode = currentEntity.GetAttributeValue<String>("new_groupcode");
                GetAllChilRecords(groupCode);
            }
        }
        catch(Exception)
        {
            throw;
        }
    }


    private string GetTemplate()
    {
        currentEntity = (Entity)context.InputParameters["Target"];
        var template = currentEntity.Tokens(service, "Your Parent Company Name is {name}");
        return template;
    }   

    private void GetAllChilRecords(String groupCode)
    {

        try
        {
            string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> 
                <entity name = 'contact' >
                <attribute name = 'fullname' />
                <attribute name = 'telephone1' />
                <attribute name = 'contactid' /> 
                <order attribute = 'fullname' descending = 'false' />
                <filter type = 'and' >
                    <condition attribute = 'parentcustomerid' operator= 'eq' value='{" + currentEntity.Id.ToString() + @"}' /> 
                </filter>
                  </entity>
                </fetch>";

            EntityCollection retrievedContacts = service.RetrieveMultiple(new FetchExpression(fetchXML));
            if (retrievedContacts.Entities.Count > 0)
            {

                foreach (Entity e in retrievedContacts.Entities)
                {
                    Entity contactUpdate = new Entity("contact");
                    contactUpdate["new_groupcode"] = groupCode;
                    contactUpdate["new_text"] = GetTemplate();
                    contactUpdate.Id = e.Id;
                    service.Update(contactUpdate);
                }
            }


        }
        catch (Exception)
        {
            throw;
        }
    }

    public void Execute(IServiceProvider serviceProvider)
    {
        try
        {
            GetOrganization(serviceProvider);
            GetRecordContext();
        }
        catch (Exception)
        {


        }

        //throw new NotImplementedException();
    }



}

}

@rappen
Copy link
Owner

rappen commented Nov 15, 2022

Hi - step 1: include also Rappen.XRM.Helpers.
That is the core of my helpers.

You will also need some Microsoft.PowerFx.* assemblies from NuGet.

See details here: https://github.com/rappen/Rappen.XTB.Helper/wiki/XRM-Tokens#how-to-install-it

@zahrabatool
Copy link
Author

Thanks Jonas

I am planning to use in Plugins, do you have a sample plugin code which consumes your helper methods to generate the tokens. because currently there is only example on github to consume xrm Tokens.

Thanks

@rappen
Copy link
Owner

rappen commented Nov 17, 2022

Good idea, I will try to create a sample plugin.
Stay tuned!

@zahrabatool
Copy link
Author

Thanks Jonas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants