Forum Discussion

Wian_le_Roux's avatar
Wian_le_Roux
Ideator III
5 years ago

Transferring relationship rows with a C# method

Hi 

I have a Request for quotation item type with an relationship to Parts. As well as a Purchase Order Item Type also with a relationship to Parts  This allowing users to select the parts that the want on the RFQ and POR. I then created a method that transfers all the form fields from the RFQ to the POR. Now i would like it to include the relationship rows as well (meaning the part rows). So when you trigger the method from the RFQ it should fill in the POR form as well as the Parts relationship. 

Does anyone perhaps have sample code on transferring relationship items to a new forms/item types relationship?

Thanks! 

Wian 

5 Replies

  • Hi Wian le Roux

    You can add below code in your action C# method to copy the relationship from RFQ to POR

    This method will validate whether POR already has that relationship part or not and add if it does not exist.

    To Change in method :

    1. Source ID is hard coded but you need to pass the POR ID which you might have got while copying the RFQ to POR form information.

    2. Validate the relationship names (RFQ Parts and POR Parts - highlighted below)

    HashSet <string> existingPORRel = new HashSet <string> ();
    string sourceId = "DB2B0131FD70498BB456074471382EEA"; // POR ID to which the relationship to be copied
    Item PORRel = this.newItem("POR Parts", "get");
    PORRel.setProperty("source_id", sourceId);
    PORRel.setAttribute("select", "related_id");
    PORRel = PORRel.apply();
    if (!PORRel.isError())
    {
    for (int i = 0; i < PORRel.getItemCount(); i++)
    {
    existingPORRel.Add(PORRel.getItemByIndex(i).getProperty("related_id"));
    }
    }
    Item RFQfetchRel = this.fetchRelationships("RFQ Parts");
    Item RFQgetRel = RFQfetchRel.getRelationships("RFQ Parts");
    for (int j = 0; j < RFQgetRel.getItemCount(); j++)
    {
    Item RFQOneItem = RFQgetRel.getItemByIndex(j);
    string relatedId = RFQOneItem.getProperty("related_id");
    if (!existingPORRel.Contains(relatedId))
    {
    Item addPORItem = this.newItem("POR Parts", "add");
    addPORItem.setProperty("source_id", sourceId);
    addPORItem.setProperty("related_id", relatedId);
    addPORItem = addPORItem.apply();
    if (addPORItem.isError())
    {
    return addPORItem;
    }
    }
    }
    return this;

    Thank You

    Gopikrishnan R

    • Wian_le_Roux's avatar
      Wian_le_Roux
      Ideator III

      Hi Gopikrishnan

      Thanks so much for your reply, appreciate it :) 

      This is my main method code to take the RFQ details over to the POR, would it be better to run this as a separate method after the POR has been created or can i just add your code to after the POR is created in my method?

      Innovator myInn = this.getInnovator();

      string thisId = this.getID();
      string thisType = this.getType();

      Item thisItem = myInn.getItemById(thisType, thisId);

      string srcId = thisItem.getProperty("source_id", "");
      Item myRfq = myInn.getItemById("DV_RFQ", srcId);
      if (myRfq == null || myRfq.isError()) {
      return myInn.newError(string.Format("Unable to get RFQ with ID {0}", srcId));
      }

      if (myRfq.fetchLockStatus() != 0) {
      string rfqNum = myRfq.getProperty("item_number", "");
      string lockedBy = myRfq.getPropertyAttribute("locked_by_id", "keyed_name", "?");
      return myInn.newError(string.Format("RFQ {0} is locked by {1}, please unlock an try again", rfqNum, lockedBy));
      }

      // Check we have an attached supplier quotation...
      string suppQuoteFileId = thisItem.getProperty("_supplier_quote_file", "");
      if (suppQuoteFileId == ""){
      return myInn.newError("You must attach a supplier quotation to the RFQ");
      }

      //Creates the New POR

      Item newPoReq = this.newItem("DV_PO_Request", "add");
      newPoReq.setProperty("_sage_vendor_id", thisItem.getRelatedItemID());
      newPoReq.setProperty("_rfq_id", srcId);
      newPoReq.setProperty("_supplier_quote_file", suppQuoteFileId);
      newPoReq.setProperty("name", myRfq.getProperty("name", ""));
      newPoReq.setProperty("description", myRfq.getProperty("description", ""));
      newPoReq = newPoReq.apply();

      if (newPoReq.isError()) return newPoReq;
      string porId = newPoReq.getID();

      // User Aras PLM Identity to promote the RFQ...
      Aras.Server.Security.Identity plmIdentity = Aras.Server.Security.Identity.GetByName("Aras PLM");
      bool PermissionWasSet = Aras.Server.Security.Permissions.GrantIdentity(plmIdentity);

      try {
      string porNum = newPoReq.getProperty("item_number", porId);
      myRfq.promote("Accepted", string.Format("Accepted on PO Request {0}", porNum));
      }
      catch (Exception ex) {
      return myInn.newError(ex.ToString());
      }
      finally {
      // Revoke Aras PLM Identity...
      if (PermissionWasSet) Aras.Server.Security.Permissions.RevokeIdentity(plmIdentity);
      }

      string mySQL = "UPDATE innovator.[DV_RFQ_SUPPLIER] SET [_po_request_id] = '{0}' WHERE [id] = '{1}'";
      myInn.applySQL(string.Format(mySQL, porId, thisId));

      return newPoReq;

      Thanks 

      Wian

      • Gopikrishnan's avatar
        Gopikrishnan
        Accelerator II

        Hi Wian

        Try below method. I have added my code into your method. Validate the highlighted relationship name before running

        Innovator myInn = this.getInnovator();

        string thisId = this.getID();
        string thisType = this.getType();

        Item thisItem = myInn.getItemById(thisType, thisId);

        string srcId = thisItem.getProperty("source_id", "");
        Item myRfq = myInn.getItemById("DV_RFQ", srcId);
        if (myRfq == null || myRfq.isError()) {
        return myInn.newError(string.Format("Unable to get RFQ with ID {0}", srcId));
        }

        if (myRfq.fetchLockStatus() != 0) {
        string rfqNum = myRfq.getProperty("item_number", "");
        string lockedBy = myRfq.getPropertyAttribute("locked_by_id", "keyed_name", "?");
        return myInn.newError(string.Format("RFQ {0} is locked by {1}, please unlock an try again", rfqNum, lockedBy));
        }

        // Check we have an attached supplier quotation...
        string suppQuoteFileId = thisItem.getProperty("_supplier_quote_file", "");
        if (suppQuoteFileId == ""){
        return myInn.newError("You must attach a supplier quotation to the RFQ");
        }

        //Creates the New POR

        Item newPoReq = this.newItem("DV_PO_Request", "add");
        newPoReq.setProperty("_sage_vendor_id", thisItem.getRelatedItemID());
        newPoReq.setProperty("_rfq_id", srcId);
        newPoReq.setProperty("_supplier_quote_file", suppQuoteFileId);
        newPoReq.setProperty("name", myRfq.getProperty("name", ""));
        newPoReq.setProperty("description", myRfq.getProperty("description", ""));
        newPoReq = newPoReq.apply();

        if (newPoReq.isError()) return newPoReq;
        string porId = newPoReq.getID();

        // Add the relationship - Begin
        HashSet <string> existingPORRel = new HashSet <string> ();
        Item PORRel = this.newItem("POR Parts", "get");
        PORRel.setProperty("source_id", porId);
        PORRel.setAttribute("select", "related_id");
        PORRel = PORRel.apply();
        if (!PORRel.isError())
        {
        for (int i = 0; i < PORRel.getItemCount(); i++)
        {
        existingPORRel.Add(PORRel.getItemByIndex(i).getProperty("related_id"));
        }
        }
        Item RFQfetchRel = myRfq.fetchRelationships("RFQ Parts");
        Item RFQgetRel = RFQfetchRel.getRelationships("RFQ Parts");
        for (int j = 0; j < RFQgetRel.getItemCount(); j++)
        {
        Item RFQOneItem = RFQgetRel.getItemByIndex(j);
        string relatedId = RFQOneItem.getProperty("related_id");
        if (!existingPORRel.Contains(relatedId))
        {
        Item addPORItem = this.newItem("POR Parts", "add");
        addPORItem.setProperty("source_id", porId);
        addPORItem.setProperty("related_id", relatedId);
        addPORItem = addPORItem.apply();
        if (addPORItem.isError())
        {
        return addPORItem;
        }
        }
        }

        // Add the relationship - End

        // User Aras PLM Identity to promote the RFQ...
        Aras.Server.Security.Identity plmIdentity = Aras.Server.Security.Identity.GetByName("Aras PLM");
        bool PermissionWasSet = Aras.Server.Security.Permissions.GrantIdentity(plmIdentity);

        try {
        string porNum = newPoReq.getProperty("item_number", porId);
        myRfq.promote("Accepted", string.Format("Accepted on PO Request {0}", porNum));
        }
        catch (Exception ex) {
        return myInn.newError(ex.ToString());
        }
        finally {
        // Revoke Aras PLM Identity...
        if (PermissionWasSet) Aras.Server.Security.Permissions.RevokeIdentity(plmIdentity);
        }

        string mySQL = "UPDATE innovator.[DV_RFQ_SUPPLIER] SET [_po_request_id] = '{0}' WHERE [id] = '{1}'";
        myInn.applySQL(string.Format(mySQL, porId, thisId));

        return newPoReq;

        Thank You

        Gopikrishnan R