locked
How to create simple plugin using c# RRS feed

  • Question

  • Hi,

    Can anyone explain the code which we write inside the plugin.

    if i want to make one field compulsory then what code i have to write.

    I am using .net 2010 and crm 2013.

    For me plugin is new concept so anyone who have better solution for this please help me out.

    Thanks,

    Ketan

    Monday, June 30, 2014 9:46 AM

All replies

  • Hi,
    normally to make a filed compulsory you set it as business required inside the form. However I understand you want to start with a simple plugin, my suggestion is to follow this youtube tutorial:

    https://www.youtube.com/watch?v=v3vie4hSE80


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Monday, June 30, 2014 10:11 AM
  • Hello Ketan,

    You don't need to write plugin to make your field compulsory, you can modify field property to set it from there or can set it using javascript check this.

    And for plugin development you can refer below posts

    http://happycrm.blogspot.in/2012/02/how-to-create-simple-plugin-in-ms-crm.html

    http://mscrmshop.blogspot.in/2010/10/crm-2011-plugin-tutorial.html

    https://community.dynamics.com/crm/b/mscrmshop/archive/2012/03/22/step-by-step-plugin-tutorial-using-developer-s-toolkit-part-1.aspx

    Also if you will download MS CRM SDK you will get sample code there as well


    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, June 30, 2014 10:12 AM
    Moderator
  • Hi,

    I know that i can make field compulsory using javascript code or through bussiness rule.

    but now i am working on Qualify and DisQualify buttons in Lead form.

    and when ever user select archived or rejected form gets read only.

    so i have to make one field compulsory which will ask user reason behind rejection or archived.

    and for that reason i need to create plugin.

    So please help me out how to create this

    Thanks,

    Ketan

    Monday, June 30, 2014 10:35 AM
  • Hi Guido Preite , Mahender Pal,

    I hope now you get what i mean to say .

    Thanks,

    Ketan

    Monday, June 30, 2014 10:41 AM
  • You can't do this throught plugin, and you can do changes to that dialog windows as well, atleast no supported way.

    you can write code on save of lead and check for Event mode valud if it is equal to 16 and then can check if user has selected any of above field or not, otherwise cancel this process.

    You could also develop your own custom window to have required fields and validations and can replace oob dialog with that.


    Our Website| Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Monday, June 30, 2014 11:18 AM
    Moderator
  • Mahender Pal,

    i write below code but its not working forms gets read only automatically

    function OnSave()

     {

    function preventAutoSave(econtext)
     {
        var eventArgs = econtext.getEventArgs();
        if (eventArgs.getSaveMode() == 15 || eventArgs.getSaveMode() == 2) 
        {
          var x = Xrm.Page.getAttribute("ava_rejectreason").getValue();
          if(!x)
          {
           eventArgs.preventDefault();
          }
        }
     }

    }



    Tuesday, July 1, 2014 11:50 AM
  • Hi,

    Can any one have solution why given code is not working ?

    thanks,

    Ketan

    Wednesday, July 2, 2014 4:02 AM
  • Hi Ketan,

    It is a known bug in CRM 2013 RTM, UR1, and UR2 that .preventDefault() will not prevent a save event when a record is being deactivated.

    This has been fixed in CRM 2013 SP1 and UR3.

    If upgrading to one of these is not an option, you could write a plugin which would check if the field contains a value. If not, you could throw an error alerting the user to the fact that the field needs to be filled out. The error message would stop the record from being deactivated.

    ~ Atomic Coder

    Wednesday, July 2, 2014 4:33 AM
  • Hi Ketan,

    I believe you want to restrict the user from disqualifying if no Rejection Reason is specified? Try with this code:

    function preventAutoSave(econtext)
    {
    	var eventArgs = econtext.getEventArgs();
    	
    	if (eventArgs.getSaveMode() == 15 || eventArgs.getSaveMode() == 2 || eventArgs.getSaveMode() == 70) 
    	{
    		var x = Xrm.Page.getAttribute("ava_rejectreason").getValue();
    		if(x==null)
    		{
    			eventArgs.preventDefault();
    		}
    	}
    }
    
    

    Make sure to link the function OnSave of the Form and check the "Pass context as the first parameter" checkbox.


    Admin QuikView Solution for CRM 2013

    • Marked as answer by Ketan Pimple Wednesday, July 2, 2014 11:46 AM
    • Unmarked as answer by Ketan Pimple Thursday, July 3, 2014 3:09 AM
    Wednesday, July 2, 2014 4:38 AM
  • Dynamotion,

    Its not working,

    Wednesday, July 2, 2014 11:34 AM
  • Its working :) :)

    thank you

    Wednesday, July 2, 2014 11:46 AM
  • Dynamotion ,

    Can you please tell me how to prevent only for rejected and archived.

    if user clicks on rejected and archived then only i have to ask for compulsory field reason.

    can you help.

    Thanks,

    Ketan.

    Thursday, July 3, 2014 3:11 AM
  • Hi,

    Which entity are you referring to? Lead doesn't have any options like Rejecting or Archiving.


    Admin QuikView Solution for CRM 2013

    Thursday, July 3, 2014 4:23 AM
  • In Disqualify,

    I have 4 options,

    Thursday, July 3, 2014 4:51 AM
  • Dynamotion,

    See above screen shot.

    Thanks,

    Ketan

    Thursday, July 3, 2014 5:23 AM
  • Hi Ketan,

    I am afraid it's not possible using JavaScript to read the "Status Code" values and restrict disqualification. You'll need Plugin to do this.


    Admin QuikView Solution for CRM 2013

    Thursday, July 3, 2014 5:56 AM
  • Dynamotion ,

    Can you please share the plugin code for given issue.

    so i can try that.

    Many thanks,

    Ketan

    Thursday, July 3, 2014 6:25 AM
  • Hi,

    Here's a sample code : http://ashwaniashwin.wordpress.com/2013/09/18/crm-2011-plugin-registration-on-lead-disqualify-using-setstate-and-setstatedynamicentity-message/

    Make sure to read the StatusCode value in the Plugin from the EntityMoniker and check if it's Archived or Rejected and take the necessary action.


    Admin QuikView Solution for CRM 2013

    Thursday, July 3, 2014 6:34 AM
  • Dynamotion,

    Thanku for help i will do as per you said.

    If i get any issues will let you know.

    Thanks,

    Ketan

    • Proposed as answer by Elvis Gabriel Sunday, July 6, 2014 9:08 PM
    • Unproposed as answer by Elvis Gabriel Sunday, July 6, 2014 9:08 PM
    Thursday, July 3, 2014 6:37 AM
  • Hi Ketan,

    If you didn't build your C# code yet, here is what I suggest:

    var HasStatusCode = entity.Contains("statuscode");
    var HasStatus = entity.Contains("statecode");
    
    if (HasStatusCode && HasStatus)
    {
        var statusCode = (OptionSetValue)entity["statuscode"];
        var status = (OptionSetValue)entity["statecode"];
    
        if (status.Value == 2 // Disqualify
            && (statusCode.Value == 100000000 // Archived
            || statusCode.Value == 100000001)) // Rejected
        {
            var lead = service.Retrieve(entity.LogicalName, entity.Id, new ColumnSet("new_disqreason"));
    
            var HasDisqReason = lead.Contains("new_discreason");
    
            var IncludeDisqReason = entity.Contains("new_disqreason");
    
            if (!HasDisqReason && !IncludeDisqReason)
            {
                throw new InvalidPluginExecutionException("A reason for disqualification must be specified.");
            }
        }
      }
    }
    I was unable to use the "SetState" and "SetStateDynamicEntity". For some reason could not make that work, so I had to use the "Update" message.

    Holpe this helps.

    Sunday, July 6, 2014 9:17 PM
  • Hi Elvis Gabriel ,

    Can i use code through web resourses, using javascript.

    and thanks for new solution.

    Regards,

    ketan

    Tuesday, July 8, 2014 6:35 AM
  • Hey Ketan,

    I've tried to use JScript, but every time the code checked for the status and statuscode fields as conditions, they had the old values (Open/New) instead of the new ones (Disqualified/Archived or Rejected).

    Regards,

    Elvis 

    Tuesday, July 8, 2014 1:49 PM
  • Hi Ketan,

    Achieving this using JavaScript is not possible, since the updated status codes are available once the status have already been set and the page reloads. Like Elvis pointed out, using JavaScript will give you the older status code.


    Admin QuikView Solution for CRM 2013

    Tuesday, July 8, 2014 7:23 PM