Hi Madiri,
It depends on whether the field's value is being modified while creating/updating the record from CRM platform.
If the value is being modified while creating/updating the record, you can get the current record field's value from Plugin Context.
But, If the value is not being modified from CRM Platform, then you have two options to retrieve the value:
Option 1 - Perform query and retrieve the value from the CRM database. Like:
Entity contactObj = service.Retrieve('contact','new Guid('30dd879c-ee2f-11db-8314-0800200c9a66'), new ColumnSet('firstname')');
Option 2 - Use Pre-Image. Instead of perform a retrieve query on database, the best practice is to push the required data in an image.
Let say: You are writing a plugin on Create of Contact Entity and want to copy emailaddress1 field value to emailaddress2 field. and User is putting the value in emailaddress1 field then Below is the pseudo code of the same.
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity contactEntity= (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an contact.
if (entity.LogicalName == "contact")
{
if (contactEntity.Attributes.Contains("emailaddress1"))
{
// Get the value from Emailaddress1 field, you can only get this value from context, if user has put some value in it.
String PrimaryEmailAddress = contactEntity["emailaddress1"].ToString();
// Set the Emailaddress1 value in EmailAddress2 field
contactEntity["emailaddress2"] = PrimaryEmailAddress;
//You no need to write service.Update(contact entity); if registering the plugin on Pre-Operation Stage
}
}
}
And as I said, If the value has not been modified from CRM Platform then you will have to retrieve the value using Pre-Image.
Check my below article to see the difference between Context and Pre-Images and to use Pre-Images in Plugins:
http://arpitmscrmhunt.blogspot.in/2018/01/pre-image-and-post-image-in-dynamics-crm.html
Hope it helps.
If my answer helped to resolve your issue, kindly verify it by clicking 'Yes'. It would be helpful to the other community members seeking to resolve a similar issue.
Cheers
Arpit
https://arpitmscrmhunt.blogspot.in