when i update my code with your suggestion i get the error i mentioned above
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SET_FSM_Final
{
public class Set_FSM : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//************************************stuff needed for plugin to work**********************************
var context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
if (context.Stage != 20)
throw new InvalidPluginExecutionException("must run as pre-operation stage 20");
if (context.MessageName != "Create")
throw new InvalidPluginExecutionException("must run on create");
if (context.PrimaryEntityName != "account")
throw new InvalidPluginExecutionException("must run agianst account entity");
var tracingservice = serviceProvider.GetService(typeof(ITracingService)) as ITracingService;
IOrganizationServiceFactory serviceFactory = serviceProvider.GetService(typeof(IOrganizationServiceFactory)) as IOrganizationServiceFactory;
IOrganizationService service = serviceFactory.CreateOrganizationService(null);
//************************************Start the Main Section**********************************
tracingservice.Trace("Hello from the plugin dude");
tracingservice.Trace("lets get our guid, shall we?");
Entity account = context.InputParameters["Target"] as Entity;
string NewAccType = Get_Account_Type(tracingservice, account, service);
tracingservice.Trace("select the account entity");
if (NewAccType == "55cdb627-9703-e211-8be2-78e3b510fddb")
{
Set_Supplier_FSM(tracingservice, service, account);
}
else if (NewAccType == "add5b921-9703-e211-8be2-78e3b510fddb")
{
Set_Repairer_FSM(tracingservice, service, account);
}
else
return;
}
private static string Get_Account_Type(ITracingService tracingservice, Entity account, IOrganizationService service)
{
string NewAccType = string.Empty; //creat + set zipcode var
if (account.Contains("ptus_companytypeid")) //check if populated
{
tracingservice.Trace(account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id.ToString());
NewAccType = account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id.ToString();
Entity entitydetails = service.Retrieve("account", account.GetAttributeValue<EntityReference>("ptus_companytypeid").Id, new ColumnSet(true));
string NewAccTypeName = entitydetails.GetAttributeValue<String>("ptus_companytypeid");
tracingservice.Trace(NewAccTypeName);
//tracingservice.Trace(account.GetAttributeValue<EntityReference>("ptus_companytypeid").Name.ToString());
//String NewAccTypeName = account.GetAttributeValue<EntityReference>("ptus_companytypeid").Name.ToString();
}
return NewAccType;
}
private static void Set_Repairer_FSM(ITracingService tracingservice, IOrganizationService service, Entity account)
{
string ZipCode = string.Empty; //creat + set zipcode var
if (account.Contains("address1_postalcode")) //check if populated
ZipCode = account.GetAttributeValue<string>("address1_postalcode");
tracingservice.Trace("query FSMUpdate entity "); //**trace ask for the stuff you want from the place you need**
tracingservice.Trace("start of query expression"); //**start of query**
var query2 = new QueryExpression("ptus_fsmipdate"); //set the entity to query
tracingservice.Trace("select entity ptus_fsmipdate");
query2.ColumnSet.AddColumn("ptus_fsm"); //select the user field
tracingservice.Trace("select the ptus_fsm field");
query2.Criteria.AddCondition("ptus_name", ConditionOperator.Equal, ZipCode); //only select when zipcode in account = zipcode in update entity
tracingservice.Trace("select when zipcodes match");
var result2 = service.RetrieveMultiple(query2); //set the result
tracingservice.Trace("set result");
if (result2.Entities.Count > 0) //check to see if results > 0
{
var r2 = result2.Entities[0]; //select the guid
account["ptus_fieldrelationshipmanagerid"] = ((EntityReference)r2.Attributes["ptus_fsm"]); //set the fsm field on account from fsm on update
}
}
private static void Set_Supplier_FSM(ITracingService tracingservice, IOrganizationService service, Entity account)
{
string ZipCode = string.Empty; //creat + set zipcode var
if (account.Contains("address1_postalcode")) //check if populated
ZipCode = account.GetAttributeValue<string>("address1_postalcode");
tracingservice.Trace("query FSMUpdate entity "); //**trace ask for the stuff you want from the place you need**
tracingservice.Trace("start of query expression"); //**start of query**
var query2 = new QueryExpression("new_supplierfsmupdate"); //set the entity to query
tracingservice.Trace("select entity ptus_fsmipdate");
query2.ColumnSet.AddColumn("new_fsm"); //select the user field
tracingservice.Trace("select the ptus_fsm field");
query2.Criteria.AddCondition("new_name", ConditionOperator.Equal, ZipCode); //only select when zipcode in account = zipcode in update entity
tracingservice.Trace("select when zipcodes match");
var result2 = service.RetrieveMultiple(query2); //set the result
tracingservice.Trace("set result");
if (result2.Entities.Count > 0) //check to see if results > 0
{
var r2 = result2.Entities[0]; //select the guid
account["ptus_fieldrelationshipmanagerid"] = ((EntityReference)r2.Attributes["new_fsm"]); //set the fsm field on account from fsm on update
};
}
}
}