Wednesday, December 5, 2012

Sample function using SetStateRequest to deactivate record ms crm 2011

below is the code to deactive the records associated to a particular record.
also the 2nd function is used to deactivate a particular record...

//Diactivate all the Associated Records of the primary records
        public static void DiactivateAllAssociatedRecords(IOrganizationService service, Guid primaryEntityId,
            String relatedEntityName, String lookupName)
        {
            try
            {
                //first get all the GUIDs as per the primaryEntityId
                #region //build the query
                ConditionExpression condition1 = new ConditionExpression();
                condition1.AttributeName = lookupName;
                condition1.Operator = ConditionOperator.Equal;
                condition1.Values.Add(primaryEntityId);
 
                FilterExpression filter1 = new FilterExpression();
                filter1.Conditions.Add(condition1);
 
                QueryExpression query = new QueryExpression();
                query.EntityName = relatedEntityName;
                query.ColumnSet = new ColumnSet(true);
                query.Criteria.AddFilter(filter1);
                #endregion
 
                EntityCollection ecResult = service.RetrieveMultiple(query);
                if (ecResult != null && ecResult.Entities.Count > 0)
                {
                    foreach (Entity eResult in ecResult.Entities)
                    {
//this is the next function called.....
                          DiactivateRecord(service, eResult);
                    }
                }
            }
            catch (SoapException spex)
            {
                throw new Exception("Interface : " + spex.StackTrace);
            }
            catch (Exception ex)
            {
                throw new Exception("Interface : " + ex.StackTrace);
            }
        }

 

//diactivate the record now
        public static void DiactivateRecord(IOrganizationService service, Entity eResult)
        {
            try
            {
                SetStateRequest setState = new SetStateRequest();
                setState.EntityMoniker = new EntityReference(eResult.LogicalName, eResult.Id);
                setState.State = new OptionSetValue();
                setState.State.Value = 1;
                setState.Status = new OptionSetValue();
                setState.Status.Value = -1;
                SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);
            }
            catch (SoapException spex)
            {
                throw new Exception("Interface : " + spex.StackTrace);
            }
            catch (Exception ex)
            {
                throw new Exception("Interface : " + ex.StackTrace);
            }
        }

 NOTE: We Can not Deactivate the Address records as there is no option for that... if we do, it will throw the below exception....
"The 'SetStateDynamicEntity' method does not support entities of type 'customeraddress'."

Regards,
Sudhanshu

No comments:

Post a Comment