function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
algot algotalgot algot 

Set RecordType on Contact ceation via Enterprise API

I have created a C# project, connect Salesforce Enterprise WSDL as WebReference and trying to create the Contact
 
var recordTypes = sforceService.query("select Id from RecordType where sObjectType = 'Contact'"); 
var contact = new Contact { 
  LastName = textTemplate, 
  RecordType = (RecordType)recordTypes.records[0], 
  Email = textTemplate + "@test.com", 
  Phone = "1234567890", 
}; 
res = sforceService.create(new sObject[] { contact });

And get an error: 
"Field name provided, Id is not an External ID or indexed field for RecordType"

How should I set RecordType in this case?
Best Answer chosen by algot algot
Deepali KulshresthaDeepali Kulshrestha
Hi algot,
Greetings to you!

- I implemented in my Org. Please use the below code[Solved] : -
 
var recType = sforce.connection.query("SELECT name,Id from RecordType where developername = 'Record_Type_Name'");
    alert(recType.records.Id);
    
    var taskToCreate  = new sforce.SObject("Contact");
    taskToCreate.LastName = "Test Js";
    taskToCreate.RecordTypeId = recType.records.Id;
    
    var result = sforce.connection.create([taskToCreate]);
    alert(result);
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi algot,
Greetings to you!

- Please use the below code : -
    
Id recordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName()
                        .get('Contact').getRecordTypeId();
var contact = new Contact { 
  LastName = textTemplate, 
  RecordType = recordTypeId, 
  Email = textTemplate + "@test.com", 
  Phone = "1234567890", 
}; 
res = sforceService.create(new sObject[] { contact });

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
algot algotalgot algot
Hi Deepall
'The name Schema doesn't exist in the current context'
Deepali KulshresthaDeepali Kulshrestha
Hi algot,
Greetings to you!

- I implemented in my Org. Please use the below code[Solved] : -
 
var recType = sforce.connection.query("SELECT name,Id from RecordType where developername = 'Record_Type_Name'");
    alert(recType.records.Id);
    
    var taskToCreate  = new sforce.SObject("Contact");
    taskToCreate.LastName = "Test Js";
    taskToCreate.RecordTypeId = recType.records.Id;
    
    var result = sforce.connection.create([taskToCreate]);
    alert(result);
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
This was selected as the best answer