How to add a value to the search field?

I have an entitiy account in which she has some field_name in Microsoft Dynamics CRM. In addition to the search field, all other field values ​​can be inserted. How to select an existing value in a search?

I used the following code to add a value to the search box. However, I am not getting any errors.

Account acc = new Account();
acc.Attributes["name"] = "Ram"; // this values got inserted
acc.Attributes["age"] = "22"; // this values got inserted
acc.Attributes["lookupfieldid"] = "Sampletext";

service.Create(acc); // to create account

How can I change my code to select the "primary" value in the search field?

+4
source share
3 answers

Search fields in CRM 2011 EntityReference, this means that you need to know the LogicalNameobject that the search points to and the Idrecords.

so your code will be as follows:

Account acc = new Account();
acc.Attributes["name"] = "Ram"; // this values got inserted
acc.Attributes["age"] = "22"; // this values got inserted

acc.Attributes["lookupfieldid"] = new EntityReference("contact", contactId); // if lookupfieldid is pointing to contact entity

service.Create(acc); // to create account

One consideration: you wrote

Account acc = new Account();

, ( , crmsvcutil.exe) ( Entity acc = new Entity("account");)

, :

Account acc = new Account();
acc.Name = "Ram"; // this values got inserted
acc.Age = "22"; // this values got inserted
acc.LookupFieldId = new EntityReference("contact", contactId); // if lookupfieldid is pointing to contact entity
service.Create(acc); // to create account

, , .

+9

, , (, new_accounttype) (new_name), "", "" "". Lookupfieldid - , new_accounttype. , , "", , new_name = "Primary".

//Retrieve "Primary" account type
QueryExpression query = new QueryExpression("new_accounttype");
query.Criteria.AddCondition("new_name", ConditionOperator.Equal, "Primary");
Entity accountType = service.RetrieveMultiple(query).Entities.First();

//Set the lookup as Guido described above
Account acc = new Account();
acc.Attributes["name"] = "Ram";
acc.Attributes["age"] = "22";
acc.Attributes["lookupfieldid"] = new EntityReference("new_accounttype", accountType.Id);
service.Create(acc);
+4

EntityReference entref = (EntityReference)item.Attributes[attributeName];

var LookupId = entref.Id;

var logicalName = entref.LogicalName;

newAccount[attributeName] = new EntityReference(logicalName, LookupId);
0

Source: https://habr.com/ru/post/1535876/


All Articles