Untitled

 avatar
unknown
plain_text
a year ago
7.7 kB
7
Indexable
var AccountRibbon = window.AccountRibbon || {};
(function () {


    // #region bottone che crea un contact a partire dall'email

    this.CreaContactEnable = function(formContext) {
       // debugger
      
        var email = formContext.getAttribute("emailaddress1").getValue();
        if (email != null ) {
            return true; 
        } 
        return false;
    }  
    this.CreaContactCommand = function(formContext) {
        debugger

        var alertStrings = { confirmButtonLabel: "Si", text: "Sei sicuro di voler procedere con l'operazione?", title: "Attenzione" };
        var alertOptions = { height: 120, width: 260 };
        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
            function (success) {
                console.log("Alert dialog closed");

                Xrm.Utility.showProgressIndicator("Elaborazione in corso...");

                var accountId = formContext.data.entity.getId();
                var execute_yod_CreaContact_Request = {
                    // Parameters
                    Target: { "@odata.type": "Microsoft.Dynamics.CRM.account", accountid: accountId }, // mscrm.crmbaseentity

                    getMetadata: function () {
                        return {
                            boundParameter: null,
                            parameterTypes: {
                                Target: { typeName: "mscrm.crmbaseentity", structuralProperty: 5 }
                            },
                            operationType: 0, operationName: "yod_CreaContact"
                        };
                    }
                };

                Xrm.WebApi.execute(execute_yod_CreaContact_Request).then(
                    function success(response) {
                        if (response.ok) {
                            formContext.getAttribute("yod_creacontatto").setValue(false);    // L'ho messo qui perche essendo la webapi.request asincrona se mettevo questa riga di codice su accountform creacontatto l'avrebbe settata a false senza aspettare che venga eseguito creacontatto ma mettendola qui nel success , sono sicuro che funzionera
                            alert("Creazione del contatto avvenuta con successo!");
                            Xrm.Utility.closeProgressIndicator();t

                            console.log("Success");

                        }
                    }
                ).catch(function (error) {
                    Xrm.Utility.closeProgressIndicator();
                    console.log(error.message);
                });
                 
            },
            function (error) {
                console.log(error.message);
            }
        );
    }
    // #endregion
}).call(AccountRibbon)





using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Principal;

namespace TRIAL1.ACTION
{
    public class CreaContact : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            try
            {
                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
                {
                    tracingService.Trace($"inizio ");

                    EntityReference accountRef = (EntityReference)context.InputParameters["Target"];
                    tracingService.Trace($"inizio retrieve");

                    Entity account = service.Retrieve(accountRef.LogicalName, accountRef.Id, new ColumnSet("name", "telephone1", "fax", "address1_line1","address1_line2","address1_line3","address1_city","address1_stateorprovince","address1_postalcode","address1_country","address1_shippingmethodcode","address1_freighttermscode"));
                    tracingService.Trace($"fine retreive");

                    string nomeAccount = account.GetAttributeValue<string>("name");
                    string[] nameSurname = Utility.DivideInTwoWords(nomeAccount);
                    string nome = nameSurname[0];
                    string cognome = nameSurname[1];
                    string email = account.GetAttributeValue<string>("emailaddress1");
                    string telefono = account.GetAttributeValue<string>("telephone1");
                    string fax = account.GetAttributeValue<string>("fax");
                    string indirizzo_ln1 = account.GetAttributeValue<string>("address1_line1");
                    string indirizzo_ln2 = account.GetAttributeValue<string>("address1_line2");
                    string indirizzo_ln3 = account.GetAttributeValue<string>("address1_line3");
                    string citta = account.GetAttributeValue<string>("address1_city");
                    string provincia = account.GetAttributeValue<string>("address1_stateorprovince");
                    string cap = account.GetAttributeValue<string>("address1_postalcode");
                    string paese = account.GetAttributeValue<string>("address1_country");
                    OptionSetValue shipMethod = account.GetAttributeValue<OptionSetValue>("address1_shippingmethodcode");
                    OptionSetValue freighttrmcode = account.GetAttributeValue<OptionSetValue>("address1_freighttermscode");
                    tracingService.Trace($"fine getattribute");

                    Entity contact = new Entity("contact");
                    contact.Attributes.Add("parentcustomerid", new EntityReference("account", account.Id));

                    contact.Attributes.Add("firstname", nome);
                    contact.Attributes.Add("lastname", cognome);
                    contact.Attributes.Add("telephone1", telefono);
                    contact.Attributes.Add("emailaddress1", email);
                    contact.Attributes.Add("fax", fax);
                    contact.Attributes.Add("address1_line1", indirizzo_ln1);
                    contact.Attributes.Add("address1_line2", indirizzo_ln2);
                    contact.Attributes.Add("address1_line3", indirizzo_ln3);
                    contact.Attributes.Add("address1_city", citta);
                    contact.Attributes.Add("address1_stateorprovince", provincia);
                    contact.Attributes.Add("address1_postalcode", cap);
                    contact.Attributes.Add("address1_country", paese);
                    contact.Attributes.Add("address1_shippingmethodcode", shipMethod);
                    contact.Attributes.Add("address1_freighttermscode", freighttrmcode);
                    Guid contactid = service.Create(contact);
                    tracingService.Trace($"inizio postimage");

                    account.Attributes.Add("primarycontactid", new EntityReference("contact", contactid));
                    service.Update(account);
                }
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException($"Errore CreaContact : {ex.Message}");
            }
        }

    }
}
Editor is loading...
Leave a Comment