private void createCustomObjectSample() { //Verify that we are already authenticated, if not //call the login function to do so if (!loggedIn) { if (!login()) return; } try { MetadataService mBinding = new apexMetadata.MetadataService(); mBinding.SessionHeaderValue = new basicSample_cs_p.apexMetadata.SessionHeader(); mBinding.SessionHeaderValue.sessionId = binding.SessionHeaderValue.sessionId; mBinding.Url = binding.Url.Replace(@"/u/", @"/m/"); CustomObject cObj = new CustomObject(); cObj.deploymentStatus = DeploymentStatus.Deployed; cObj.description = "This object was created using the metadata API sample."; Console.Write("Do you want to enable activities on this new object? (y/n)"); if (Console.ReadLine().ToLower().Equals("y")) { cObj.enableActivities = true; } Console.Write("Do you want to enable division support on this new object? (y/n)"); if (Console.ReadLine().ToLower().Equals("y")) { cObj.enableDivisions = true; } Console.Write("Do you want to enable history on this new object? (y/n)"); if (Console.ReadLine().ToLower().Equals("y")) { cObj.enableHistory = true; } Console.Write("Do you want to enable reporting on this new object? (y/n)"); if (Console.ReadLine().ToLower().Equals("y")) { cObj.enableReports = true; } Console.Write("Enter the name of your custom object: "); String fullName = Console.ReadLine(); if (fullName.Length == 0) { Console.Write("No name entered, quitings custom object creation..."); Console.ReadLine(); return; } cObj.fullName = fullName + "__c"; Console.Write("Enter the label for your custom object: "); String objectLabel = Console.ReadLine(); if (objectLabel.Length == 0) { Console.Write("No label entered, quiting custom object creation..."); Console.ReadLine(); return; } cObj.label = objectLabel; CustomField nameField = new CustomField(); Console.Write("Enter the name field name for the your object: "); nameField.fullName = Console.ReadLine(); if (nameField.fullName.Length == 0) { return; } Console.Write("What type of field is the name field? (text/autonumber) "); String nameFieldType = Console.ReadLine(); if (nameFieldType.Length == 0) { return; } else { if (nameFieldType.ToLower().Equals("text")) { nameField.type = FieldType.Text; nameField.length = 255; } else if (nameFieldType.ToLower().Equals("autonumber")) { nameField.startingNumber = 1; nameField.displayFormat = "SAMPLE {000000}"; } else { return; } cObj.nameField = nameField; } cObj.nameField.description = "This is the name field from the API Samples."; cObj.nameField.label = cObj.fullName + " name"; cObj.pluralLabel = cObj.label + "s"; cObj.sharingModel = SharingModel.ReadWrite; cObj.gender = null; cObj.startsWith = null; AsyncResult asyncResult = mBinding.create(new Metadata[] { cObj })[0]; while (asyncResult.state == AsyncRequestState.InProgress) { Thread.Sleep(asyncResult.secondsToWait * 1000); Console.WriteLine("Checking status...\n\tState:" + asyncResult.state + "\n\tStatus: " + asyncResult.statusCode + "\n\tSeconds to wait: " + asyncResult.secondsToWait); asyncResult = mBinding.checkStatus(new String[] { asyncResult.id })[0]; } if (asyncResult.state == AsyncRequestState.Completed) { Console.Write("Custom object has been created. \n\nUse the describe global sample to see your object listed."); Console.ReadLine(); } Console.Write("To delete the newly created custom object enter 'delete' now... "); if (Console.ReadLine().ToLower().Equals("delete")) { CustomObject deleteObj = new CustomObject(); deleteObj.fullName = cObj.fullName; asyncResult = mBinding.delete(new Metadata[] { deleteObj })[0]; while (asyncResult.state == AsyncRequestState.InProgress) { Thread.Sleep(asyncResult.secondsToWait * 1000); Console.WriteLine("Checking status...\n\tState:" + asyncResult.state + "\n\tStatus: " + asyncResult.statusCode + "\n\tSeconds to wait: " + asyncResult.secondsToWait); asyncResult = mBinding.checkStatus(new String[] { asyncResult.id })[0]; } if (asyncResult.state == AsyncRequestState.Completed) { Console.Write("Custom object has been deleted. \n\nUse the describe global sample to see your object is not listed."); Console.ReadLine(); } } } catch (Exception ex) { Console.WriteLine("\nFailed to create object: \n" + ex.Message); Console.WriteLine("\nHit return to continue..."); Console.ReadLine(); } }
private void createCustomFieldSample() { //Verify that we are already authenticated, if not //call the login function to do so if (!loggedIn) { if (!login()) return; } try { Console.WriteLine("\nThis sample will create a new Text field on the Lead object."); Console.ReadLine(); Console.Write("Enter the name that you would like for the field: "); String fieldName = Console.ReadLine(); if (fieldName.Length == 0) { Console.Write("No name was entered for the field, so we are exiting this sample."); Console.ReadLine(); return; } CustomField cf = new CustomField(); cf.description = "Simple text field from API"; cf.fullName = "Lead." + fieldName + "__c"; Console.Write("Enter a label for the field: "); String fieldLabel = Console.ReadLine(); if (fieldLabel.Length == 0) { fieldLabel = "Sample Field"; } cf.label = fieldLabel; cf.length = 50; cf.type = FieldType.Text; MetadataService mBinding = new MetadataService(); mBinding.SessionHeaderValue = new basicSample_cs_p.apexMetadata.SessionHeader(); mBinding.SessionHeaderValue.sessionId = binding.SessionHeaderValue.sessionId; mBinding.Url = binding.Url.Replace(@"/u/", @"/m/"); AsyncResult asyncResult = mBinding.create(new Metadata[] { cf })[0]; while (asyncResult.state == AsyncRequestState.InProgress) { Thread.Sleep(asyncResult.secondsToWait * 1000); Console.WriteLine("Checking status...\n\tState:" + asyncResult.state + "\n\tStatus: " + asyncResult.statusCode + "\n\tSeconds to wait: " + asyncResult.secondsToWait); asyncResult = mBinding.checkStatus(new String[] { asyncResult.id })[0]; } if (asyncResult.state == AsyncRequestState.Completed) { Console.Write("Custom object has been created. \n\nUse the describe sobject sample to see your object is not listed."); Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine("\nFailed to create object: \n" + ex.Message); Console.WriteLine("\nHit return to continue..."); Console.ReadLine(); } }