Using the ASP COM API to create more than 2 records at once.

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Using the ASP COM API to create more than 2 records at once.

  • Comments 4
  • Likes

In an earlier article I showed how you could create a complex screen that allowed the editing multiple records in ASP.  The actual example was the editing of a Company, Person and Address record all together.

You can find this discussed in the article "Example of a Complex Screen editing multiple records in ASP".

But how do you create a record of two or more different entities at once?  Can we build our own version of the Company Entry screen?

The answer is yes we most certainly can!  The code below is a partial rebuild only, as it doesn't cover the creation of the email and phone records.  Be careful with those as actuall denormalisation back into the company and person records is required.

<!-- #include file ="sagecrm.js"-->
<%
if(CRM.Mode==View)
{
CRM.Mode=Edit;
}
var recCompany = CRM.CreateRecord("company");
var recAddress = CRM.CreateRecord("address");
var recPerson = CRM.CreateRecord("person");

var companyBlock = CRM.GetBlock("companyboxlong");
companyBlock.ArgObj = recCompany;
companyBlock.Title = CRM.GetTrans("tabnames","company");

var addressBlock = CRM.GetBlock("addressboxshort");
addressBlock.ArgObj=recAddress;
addressBlock.Title = CRM.GetTrans("tabnames","address");

var personBlock = CRM.GetBlock("personboxshort");
personBlock.ArgObj=recPerson;
personBlock.Title = CRM.GetTrans("tabnames","person");

var myBlockContainer = CRM.GetBlock("Container");
with (myBlockContainer)
{
  AddBlock(companyBlock);
  AddBlock(addressBlock);
  AddBlock(personBlock);
}

CRM.AddContent(myBlockContainer.Execute());

if(CRM.Mode==Save)
{
recCompany.comp_primaryaddressid = recAddress.addr_addressid;
recCompany.comp_primarypersonid = recPerson.pers_personid;
recCompany.SaveChanges();

var recPersonLink = CRM.CreateRecord("person_link");
recPersonLink.peli_personid = recPerson.pers_personid;
recPersonLink.peli_companyid = recCompany.comp_companyid;
recPersonLink.SaveChanges();

var recAddressLink = CRM.CreateRecord("address_link");
recAddressLink.adli_addressid = recAddress.addr_addressid;
recAddressLink.adli_companyid = recCompany.comp_companyid;
recAddressLink.adli_personid = recPerson.pers_personid;
recAddressLink.SaveChanges();

Response.Redirect(CRM.Url("200")+"&key0=1&key1="+recCompany.comp_companyid);
}

Response.Write(CRM.GetPage("new"));
%>


An article that discusses the creation of default Address and Persons for a Company is How do default addresses and persons get created for a Company?

An article that discusses the phone and email records is Email and Phone Numbers in CRM.

Comments
  • Jeff...

    Hoping that you can help me with the Response.Redirect portion of a page that creates records for multiple entities.  One entity is a "parent" to the other entities ("child1" and "child2").  After the records are created, I want to be redirected to the parent record that was just created.  I thought I had the code correct.  However, I end up on a different record...not the one just created.  All three entities are custom entities.  

    This is what I have for the redirect...

    Response.Redirect(CRM.URL("MyParentSummary.asp")+"&key0=58&key58="+recParent.pare_ParentID);

    I did discover that it depends on where I start from when creating the records.  I am accessing an asp page to create the records from the "New" menu.  Therefore, if I am on a parent record and I hit the New Menu button (the green button under the Admin menu..not a new button on a record) to access my input form (asp page), I will end up back at the record that I was on when I accessed my form..not on my newly created record.

    If I am on my calendar and I hit the New Menu button and access my form.  After the records are created, I do end up on the summary page for my parent record.

    Therefore, I think I should end up in the correct place as long as I am not coming from another custom entity.  I think Key58 is my issue.  Since it is in my URL if I am coming from my custom entity, the redirect appears to be picking that up instead of the one I have put in my Response.Redirect line.  I am guessing I need to an array.  However, i am not sure how to go about it since it is only needed if I am coming from a custom entity.

    I couldn't find much on Response.Redirect and how to work with J, F, T and E in the URL.  Can you point me in the right direction?

    Any assistance you could provide would be greatly appreciated.  Thank you!

  • Jeff...

    After looking at this some more, I do think I am going to need to create an array to get the correct Key58.  However, I am also going to have to do an "if" statement to accommodate where the user is coming from.  Instead of doing all that, I was thinking I should just "control" the URL myself.  Can you point me to some information on how I might do that?  I have figured out some of the "pieces" of the URL.  However, I am wondering about the order of the data in the URL.  Is that important?  I am going to try to find some information on creating the URL from scratch...but if you could point me in the right direction, I would greatly appreciate it.  Would some of that have been covered in ASP 1 and ASP 2 in the developers program?

    Any assistance you could provide would be greatly appreciated!

  • Jeff...

    I think I got this!  Doing a little happy dance. :-)

    I did create the redirect URL using an example from this post...

    community.sagecrm.com/.../changing-context-when-jumping-between-pages-using-the-com-asp-api.aspx

    The page appears to function as expected.  Therefore, I think I am good to go!

    Sorry for all the posts!

  • Michele

    Happy Dance!  I am glad you were able to sort this out.  I have been working on the materials for Sage CRM 2017 so have not been able to think about this until now.  But you sorted yourself so that is brilliant.