Home

Apr 15, 2010

Programmatically Create Service & Associate Resources to it in CRM 4.0

We are working on requirement which needs a extension to service entity however service is a system entity so we can’t customize it. We come up with an extension entity to service entity which will be related to N : 1 relationship with service. We have create service entity and associated resources to it oncreate of extension entity. We have used below code snippets to achieve this.

Blogger Labels: Service,snippets

            // Create the plumber resource group.
          
constraintbasedgroup group = newconstraintbasedgroup();
            group.businessunitid = newLookup();
            group.businessunitid.type = EntityName.businessunit.ToString();
            group.businessunitid.Value = user.BusinessUnitId;
            group.name = serviceName + " Group";
            System.Text.StringBuilder builder = newSystem.Text.StringBuilder("<Constraints>");
            builder.Append("<Constraint>");
            builder.Append("<Expression>");
            builder.Append("<Body>");
            builder.Append("resource[\"Id\"] == {BAF6E7A4-0E76-43de-8462-E951CD9BFEDF} ");
            builder.Append("</Body>");
            builder.Append("<Parameters>");
            builder.Append("<Parameter name=\"resource\" />");
            builder.Append("</Parameters>");
            builder.Append("</Expression>");
            builder.Append("</Constraint>");
            builder.Append("</Constraints>");
            group.constraints = builder.ToString();
            group.grouptypecode = new Picklist();
            group.grouptypecode.Value = 0;

            Guid groupId = crmService.Create(group);

            // Create the resource specification.
          
resourcespec spec = new resourcespec();
            spec.businessunitid = newLookup();
            spec.businessunitid.type = EntityName.businessunit.ToString();
            spec.businessunitid.Value = user.BusinessUnitId;
            spec.objectiveexpression = @"
               <Expression>
                  <Body>udf ""Random""(factory,resource,appointment,request,leftoffset,rightoffset)</Body>
                  <Parameters>
                     <Parameter name=""factory"" />
                     <Parameter name=""resource"" />
                     <Parameter name=""appointment"" />
                     <Parameter name=""request"" />
                     <Parameter name=""leftoffset"" />
                     <Parameter name=""rightoffset"" />
                  </Parameters>
                  <Properties EvaluationInterval=""P0D"" evaluationcost=""0"" />
               </Expression>"
;
            spec.requiredcount = new CrmNumber();
            spec.requiredcount.Value = 1;
            spec.name = "Rooms Specification";
            spec.groupobjectid = new UniqueIdentifier();
            spec.groupobjectid.Value = groupId;
            Guid specId = crmService.Create(spec);

            // Create the service.
          
service serviceProcedure = new service();
            serviceProcedure.name = serviceName;
            serviceProcedure.description = serviceName;
            serviceProcedure.duration = newCrmNumber();
            serviceProcedure.duration.Value = 60;
            serviceProcedure.initialstatuscode = newStatus();
            serviceProcedure.initialstatuscode.Value = 1;
            serviceProcedure.granularity = Constants.Qranularity;
            serviceProcedure.resourcespecid = newLookup();
            serviceProcedure.resourcespecid.type = EntityName.resourcespec.ToString();
            serviceProcedure.resourcespecid.Value = specId;
            serviceProcedure.strategyid = newLookup();
            // This is a known GUID for the default strategy.
          
serviceProcedure.strategyid.Value = newGuid(Constants.StrategyId);
            returncrmService.Create(serviceProcedure);

Jai Ho CRM,

Amol
 

 

No comments: