SBX - Search With Button

SBX - Forum Post Title

Javascript issue with Plugin

Microsoft Dynamics CRM Forum

rocky asked a question on 13 Jul 2018 3:15 AM
My Badges

Question Status

Suggested Answer

HI,

I tried with this https://nishantrana.me/2015/07/04/using-action-to-trigger-plugin-in-crm-20132015/  

I created a button and added javascript code to trigger plugin with process.But its getting error 

<s:Envelope
 
xmlns:s
="
http://schemas.xmlsoap.org/soap/envelope/
"
>
<s:Body
>
<s:Fault
>
<faultcode
>
s:Client
</faultcode
>
<faultstring
 
xml:lang
="
en-US
"
>
fz_bulkoptionset With Id = 57dbb357-e785-e811-a951-000d3ab10c56 Does Not Exist
</faultstring
>
<detail
>
<OrganizationServiceFault
 
xmlns
="
http://schemas.microsoft.com/xrm/2011/Contracts
"
 
xmlns:i
="
http://www.w3.org/2001/XMLSchema-instance
"
>
<ActivityId
>
3049fd27-14f3-4e6a-babd-195258424b0b
</ActivityId
>
<ErrorCode
>
-2147220969
</ErrorCode
>
<ErrorDetails
 
xmlns:a
="
http://schemas.datacontract.org/2004/07/System.Collections.Generic
"
>
…
</ErrorDetails
>
<Message
>
fz_bulkoptionset With Id = 57dbb357-e785-e811-a951-000d3ab10c56 Does Not Exist
</Message
>
<Timestamp
>
2018-07-13T08:06:36.9119581Z
</Timestamp
>
<ExceptionRetriable
>
false
</ExceptionRetriable
>
<ExceptionSource
 
i:nil
="
true
"
/
>
<InnerFault
 
i:nil
="
true
"
/
>
<OriginalException
 
i:nil
="
true
"
/
>
<TraceText
 
i:nil
="
true
"
/
>
</OrganizationServiceFault>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>

fz_bulkoptionset With Id = 57dbb357-e785-e811-a951-000d3ab10c56 Does Not Exist but for fz_bulkoptionset  entity its the id i checked in url when we click on entity i saw id in link.

Here the code for That

Process  Name:fz_BulkImportGlobalOptionset

Javascript Code 

function generatebulkimport()
{
try {
   var entityId = "57DBB357-E785-E811-A951-000D3AB10C56";
var entityName = "fz_bulkoptionset";
var requestName = "fz_BulkImportGlobalOptionset";
var xml =
"<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\"><s:Body><Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\"><request xmlns:a=\"schemas.microsoft.com/.../Contracts\"><a:Parameters xmlns:b=\"schemas.datacontract.org/.../System.Collections.Generic\"><a:KeyValuePairOfstringanyType><b:key>Target</b:key><b:value i:type=\"a:EntityReference\"><a:Id>"+entityId+"</a:Id><a:LogicalName>"+entityName+"</a:LogicalName><a:Name i:nil=\"true\"/></b:value></a:KeyValuePairOfstringanyType></a:Parameters><a:RequestId i:nil=\"true\"/><a:RequestName>"+requestName+"</a:RequestName></request></Execute></s:Body></s:Envelope>";

 
var xmlHttpRequest = new XMLHttpRequest();
 
xmlHttpRequest.open("POST", Xrm.Page.context.getClientUrl() +"/XRMServices/2011/Organization.svc/web", false);
xmlHttpRequest.setRequestHeader("SOAPAction","schemas.microsoft.com/.../Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
 
var resultXml = xmlHttpRequest.responseText;
alert(resultXml);
}
catch(err) {
alert(err.message);
}
}


My Plugin Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;


namespace Bulkoptionsetimport
{
    public class Class1 : IPlugin
    {

        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context =
                (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Get a reference to the Organization service.
            IOrganizationServiceFactory factory =
                (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);


            try
            {
                
                //OrganizationRequest req = new OrganizationRequest("fz_BulkImportGlobalOptionset");
                //req["Target"] = new EntityReference("fz_bulkoptionset", new Guid ("57DBB357-E785-E811-A951-000D3AB10C56"));
                //////Read configuration

                //OrganizationResponse response = service.Execute(req);


                //Read configuration
                QueryExpression optionsetconfig = new QueryExpression()
                {
                    EntityName = "fz_bulkoptionset",
                    ColumnSet = new ColumnSet(true),
                    Criteria =
                {
                    Conditions =
                            {
                                //new ConditionExpression("fz_name", ConditionOperator.Equal, "Configurations"),
                                //new ConditionExpression("statecode", ConditionOperator.Equal, 0)
                            }
                }
                };

                EntityCollection _Records = service.RetrieveMultiple(optionsetconfig);





                foreach (Entity _Entity in _Records.Entities)

                {

                    InsertOptionValueRequest insertOptionValueRequest =
                    new InsertOptionValueRequest
                    {
                        Value = Convert.ToInt16(_Entity.Attributes["fz_optionset"]),
                        OptionSetName = _Entity.Attributes["fz_name"].ToString(),
                        Label = new Label(_Entity.Attributes["fz_optionsetlabel"].ToString(), 1033)

                    };
                    //execute the request
                    InsertOptionValueResponse optionResponse =
                        (InsertOptionValueResponse)service.Execute(insertOptionValueRequest);
                }












            }
            catch (Exception ex)
            {
                tracingService.Trace("accountPlugin: {0}", ex.ToString());
                throw;
            }
        }


    }
}
Reply
Sandeep Mishra responded on 13 Jul 2018 8:32 AM
My Badges
Suggested Answer

Hi,

I will suggest to create a dummy entity and attached plugin on created or update event of custom entity. Then call create method  using js. It will fire plugin.

Hope it helps.

Reply
Andrew Butenko responded on 13 Jul 2018 2:48 PM
Suggested Answer

Hello,

Are you sure that entity with "57DBB357-E785-E811-A951-000D3AB10C56" id exists in CRM? It seems that no...

Reply
Ravi Kashyap responded on 13 Jul 2018 6:29 PM
Suggested Answer

Hi,

Based on your screenshot, "57dbb357-e785-e811-a951-000d3ab10c56" is the id of your solution which you opened and not the id of the record for your fz_bulkoptionset .

To get the id of the particular record, you need to open that record in a new window. You can also do this by clieking on the tilted arrow on the top right corner of the record.

Or, if you are working on Chrome, you can download this chrome extension, which has an option to get the record id.

chrome.google.com/.../bjnkkhimoaclnddigpphpgkfgeggokam

Hope this helps.

Reply
Shahbaaz Ansari responded on 14 Jul 2018 12:45 AM
My Badges
Suggested Answer

Check below link for calling Plugin from javascript,

dynamics365learning.wordpress.com/.../clone-entity-record

Best Regards,

Shahbaaz

Reply
Sandeep Mishra responded on 13 Jul 2018 8:32 AM
My Badges
Suggested Answer

Hi,

I will suggest to create a dummy entity and attached plugin on created or update event of custom entity. Then call create method  using js. It will fire plugin.

Hope it helps.

Reply
Andrew Butenko responded on 13 Jul 2018 2:48 PM
Suggested Answer

Hello,

Are you sure that entity with "57DBB357-E785-E811-A951-000D3AB10C56" id exists in CRM? It seems that no...

Reply
Ravi Kashyap responded on 13 Jul 2018 6:29 PM
Suggested Answer

Hi,

Based on your screenshot, "57dbb357-e785-e811-a951-000d3ab10c56" is the id of your solution which you opened and not the id of the record for your fz_bulkoptionset .

To get the id of the particular record, you need to open that record in a new window. You can also do this by clieking on the tilted arrow on the top right corner of the record.

Or, if you are working on Chrome, you can download this chrome extension, which has an option to get the record id.

chrome.google.com/.../bjnkkhimoaclnddigpphpgkfgeggokam

Hope this helps.

Reply
Shahbaaz Ansari responded on 14 Jul 2018 12:45 AM
My Badges
Suggested Answer

Check below link for calling Plugin from javascript,

dynamics365learning.wordpress.com/.../clone-entity-record

Best Regards,

Shahbaaz

Reply

SBX - Two Col Forum

SBX - Migrated JS