Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 55831

Adding record to a entity using Query by Expression in MS crm Plugin(MS crm 2011)

$
0
0

Hello all,

Please pardon me if the question seems vauge.

I have a entity A and I am creating a Plugin (Create-Pre operation) on it,what I need is to check if a Record is present in Entity B,If not present in Entity B i want to create/update a new record for Entity B in my plugin.

I have been working on Query by expression. 

I could get the entityB from my Entoty A plugin but the problem is how to add a record.

Below is the sample code.

-----------------------------------------------------

// TODO: Implement your custom Plug-in business logic.
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;

if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "new_testingcrm")
{
try
{

String x = entity.FormattedValues["new_gender"].ToString();

QueryExpression qe = new QueryExpression("abaxis_prefixcounter2");
qe.ColumnSet = new ColumnSet();
EntityCollection SearchContactResult = service.RetrieveMultiple(qe);


int y = SearchContactResult.TotalRecordCount;

entity.Attributes["new_medicid"] = SearchContactResult.TotalRecordCount; // I am getting - 1 as count

if (SearchContactResult.EntityName.Contains("prefixcounter2"))

{
entity.Attributes["abaxis_medicalid"] = "ava";
}
else
{
entity.Attributes["abaxis_medicalid"] = "not ava";
}
var counter = "";
foreach (var result in SearchContactResult.Entities)
{
counter = result.Id.ToString();
entity.Attributes["abaxis_medicalid"] =counter;
}

string yy = counter.ToString();
if (String.IsNullOrWhiteSpace(yy)) 
{
entity.Attributes["new_callid"] = "correct guid is not available";
}

else
{
entity.Attributes["new_callid"] = yy;
}

}

catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}

----------------------------------------

when I try to use below code with [0] as index it gives me the error. I am stuck here how should i approch or any other easy way to update record

----------------------------

SearchContactResult[0].Attributes.Add("abaxis_name", "Prefix counter for testingcrm");

service.Update(SearchContactResult[0]);


Viewing all articles
Browse latest Browse all 55831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>