public void UpdateChildRecords(string relationshipName, string parentEntityType, string parentEntityId, string parentFieldNameToUpdate, string setValueToUpdate, string childFieldNameToUpdate) { //1) Get child lookup field name RetrieveRelationshipRequest req = new RetrieveRelationshipRequest() { Name = relationshipName }; RetrieveRelationshipResponse res = (RetrieveRelationshipResponse)service.Execute(req); OneToManyRelationshipMetadata rel = (OneToManyRelationshipMetadata)res.RelationshipMetadata; string childEntityType = rel.ReferencingEntity; string childEntityFieldName = rel.ReferencingAttribute; //2) retrieve all child records QueryByAttribute querybyattribute = new QueryByAttribute(childEntityType); querybyattribute.ColumnSet = new ColumnSet(childEntityFieldName); querybyattribute.Attributes.AddRange(childEntityFieldName); querybyattribute.Values.AddRange(new Guid(parentEntityId)); EntityCollection retrieved = service.RetrieveMultiple(querybyattribute); //2') retrieve parent fielv value var valueToUpdate = new object(); if (parentFieldNameToUpdate != null && parentFieldNameToUpdate != "") { Entity retrievedEntity = (Entity)service.Retrieve(parentEntityType, new Guid(parentEntityId), new ColumnSet(parentFieldNameToUpdate)); if (retrievedEntity.Attributes.Contains(parentFieldNameToUpdate)) { valueToUpdate = retrievedEntity.Attributes[parentFieldNameToUpdate]; } else { valueToUpdate = null; } } else { valueToUpdate = setValueToUpdate; } //3) update each child record foreach (Entity child in retrieved.Entities) { if (childEntityType.ToLower() == "dynamicpropertyinstance") { //pending... UpdateProductPropertiesRequest req2 = new UpdateProductPropertiesRequest(); // req2. break; } Entity entUpdate = new Entity(childEntityType); entUpdate.Id = child.Id; entUpdate.Attributes.Add(childFieldNameToUpdate, valueToUpdate); service.Update(entUpdate); } }
public void UpdateChildRecords(string relationshipName, string parentEntityType, string parentEntityId, string parentFieldNameToUpdate, string setValueToUpdate, string childFieldNameToUpdate, bool _UpdateonlyActive) { //1) Get child lookup field name RetrieveRelationshipRequest req = new RetrieveRelationshipRequest() { Name = relationshipName }; RetrieveRelationshipResponse res = (RetrieveRelationshipResponse)service.Execute(req); OneToManyRelationshipMetadata rel = (OneToManyRelationshipMetadata)res.RelationshipMetadata; string childEntityType = rel.ReferencingEntity; string childEntityFieldName = rel.ReferencingAttribute; //2) retrieve all child records QueryByAttribute querybyattribute = new QueryByAttribute(childEntityType); querybyattribute.ColumnSet = new ColumnSet(childEntityFieldName); if (!_UpdateonlyActive) { querybyattribute.Attributes.AddRange(childEntityFieldName); querybyattribute.Values.AddRange(new Guid(parentEntityId)); } else { querybyattribute.Attributes.AddRange(childEntityFieldName, "statecode"); querybyattribute.Values.AddRange(new Guid(parentEntityId), 0); } EntityCollection retrieved = service.RetrieveMultiple(querybyattribute); //2') retrieve parent fielv value var valueToUpdate = new object(); if (parentFieldNameToUpdate != null && parentFieldNameToUpdate != "") { Entity retrievedEntity = (Entity)service.Retrieve(parentEntityType, new Guid(parentEntityId), new ColumnSet(parentFieldNameToUpdate)); if (retrievedEntity.Attributes.Contains(parentFieldNameToUpdate)) { valueToUpdate = retrievedEntity.Attributes[parentFieldNameToUpdate]; } else { valueToUpdate = null; } } else { valueToUpdate = setValueToUpdate; } //3) update each child record foreach (Entity child in retrieved.Entities) { if (childEntityType.ToLower() == "dynamicpropertyinstance") { //pending... UpdateProductPropertiesRequest req2 = new UpdateProductPropertiesRequest(); // req2. break; } RetrieveAttributeRequest reqAtt = new RetrieveAttributeRequest(); reqAtt.EntityLogicalName = childEntityType; reqAtt.LogicalName = childFieldNameToUpdate; RetrieveAttributeResponse resAtt = (RetrieveAttributeResponse)service.Execute(reqAtt); bool valueToUpdateBool = false; AttributeMetadata meta = resAtt.AttributeMetadata; Entity entUpdate = new Entity(childEntityType); entUpdate.Id = child.Id; if (meta.AttributeType.Value.ToString() == "Boolean") { if (valueToUpdate is bool) { if ((bool)valueToUpdate == true) { valueToUpdate = "1"; } else { valueToUpdate = "0"; } } if (valueToUpdate == "1") { entUpdate.Attributes.Add(childFieldNameToUpdate, true); } else { entUpdate.Attributes.Add(childFieldNameToUpdate, false); } } else { if (meta.AttributeType.Value.ToString() == "Picklist" || meta.AttributeType.Value.ToString() == "Status") { if (valueToUpdate == null) { entUpdate.Attributes.Add(childFieldNameToUpdate, null); } else { if (valueToUpdate is OptionSetValue) { valueToUpdate = ((OptionSetValue)valueToUpdate).Value; } OptionSetValue opt = new OptionSetValue(Convert.ToInt32(valueToUpdate)); entUpdate.Attributes.Add(childFieldNameToUpdate, opt); } } else { entUpdate.Attributes.Add(childFieldNameToUpdate, valueToUpdate); } } service.Update(entUpdate); } }