Create Opportunity Relationship record

Sometime we need to create “Opportunity Relationship” record through code. We can use below code to create Opportunity relationship record. Basically we need to set three values while creating opportunity relationship record customerid, opportunityid and opportunityroleid.

//I have used Dynamic entity to create opportunity relationship record

DynamicEntity  _OpportunityRelationship = new DynamicEntity();

//Set entity name as “customeropportunityrole”         

 _OpportunityRelationship.Name = “customeropportunityrole”;

//Create property to set customer 

CustomerProperty _OpportunityCustomer=new CustomerProperty();

  _ OpportunityCustomer.Name=”customerid”;

 _ OpportunityCustomer.Value=new Customer();

 _ OpportunityCustomer.Value.type = “account”; //Set customer type (I am using account here)

 _ OpportunityCustomer.Value.Value=CustomerGUID;  //replace CustomerGUID with customer GUID

_OpportunityRelationship.Properties.Add(_OpportunityCustomer);

//set Opportunity ID 

LookupProperty _OpportunityID=new LookupProperty();

 _ OpportunityID.Name=”opportunityid”;

 _ OpportunityID.Value=new Lookup();

 _ OpportunityID.Value.Value=OpportunityID; //replace OpportunityID with opportunity GUID

 _OpportunityRelationship.Properties.Add(_OpportunityID);

 //Set Opportunity Role

//you can create Relationship Roles from Setting->Business Management->Relationship Roles        

 LookupProperty _OpportunityRole = new LookupProperty();

 _OpportunityRole.Name = “opportunityroleid”;

 _OpportunityRole.Value = new Lookup();

   _OpportunityRole.Value.Value =GUIDofRelationshipRole; //Replace GUIDofRelationshipRole with Relationship role Guid.

   _OpportunityRelationship.Properties.Add(_OpportunityRole);

  TargetCreateDynamic targetCreate = new TargetCreateDynamic();

  targetCreate.Entity = _OpportunityRelationship;

// Create the request object.

 CreateRequest create = new CreateRequest();

  // Set the properties of the request object.

  create.Target = targetCreate;

 // Execute the request.

 CreateResponse created = (CreateResponse)_Service.Execute(create);

 Hope it will help somebody.

Cheer 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *