Skip to main content

In the current Shopify connector, it doesn’t support the custom mapping in the Entity mapping screen for the Refund entity, but if you want to have some custom mappings in the Refund order, you can do it in the customization project. 

In this topic, I want to share how to create a custom mapping in Shopify refund entity through the customization package. You can make a mapping to the original field or custom field. I tested it in 2023R2 and it works well. 

  1. Create a new customization project
  2. Create a new code file, and extend the Graph from SPRefundsProcessor.
     

     

  3. Override the method CreateRefundOrders
     


     

    	public class SPRefundsProcessor_Extension : PXGraphExtension<PX.Commerce.Shopify.SPRefundsProcessor>
    {
    public static bool IsActive() => CommerceFeaturesHelper.ShopifyConnector;
    #region Event Handlers
    public delegate Task CreateRefundOrdersDelegate(SPRefundsBucket bucket, MappedRefunds existing, CancellationToken cancellationToken);

    /PXOverride]
    public Task CreateRefundOrders(SPRefundsBucket bucket, MappedRefunds existing, CancellationToken cancellationToken, CreateRefundOrdersDelegate baseMethod)
    {
    var baseResult = baseMethod(bucket, existing, cancellationToken);

    SalesOrder origOrder = bucket.Refunds.Local;
    OrderData shopifyOrder = bucket.Refunds?.Extern;
    List<SalesOrder> refundOrders = new List<SalesOrder>();
    if(origOrder.RefundOrders?.Count > 0)
    {
    foreach(SalesOrder oneRefund in origOrder.RefundOrders)
    {
    //shopifyOrder is the Order data, if you want to map the value from Refund data, you can get it from ShopifyOrder.Refunds
    oneRefund.Note = shopifyOrder.Note;

    //Setup custom field mapping.
    SetCustomField(oneRefund);
    refundOrders.Add(oneRefund);
    }
    origOrder.RefundOrders = refundOrders;
    }

    return baseResult;
    }

    //Do the mapping to a custom field.
    private void SetCustomField(ILocalEntity impl)
    {
    string viewName = "Document";
    string fieldName = "SalesPersonID";
    string fieldValue = "WEB";
    CustomField field = impl.Custom?.Where(x => x.ViewName == viewName && x.FieldName == fieldName).FirstOrDefault();
    if(field == null)
    {
    SchemaInfo schema = ConnectorHelper.GetConnectorSchema(Base.Operation.ConnectorType, Base.Operation.Binding, BCEntitiesAttribute.Order);
    {
    CustomFieldInfo template = schema.CustomFields?.Where(x => x.Container == impl.GetType().Name && x.Field.ViewName == viewName && x.Field.FieldName == fieldName).FirstOrDefault();
    if (template?.Field != null)
    {
    field = template.Field.CreateCustomFieldCopy();
    if (impl.Custom == null) impl.Custom = new List<CustomField>();
    impl.Custom.Add(field);
    }
    }
    }
    if (field != null)
    {
    var targetType = field.GetType();

    //If the custom field is not StringValue type, you need to change it.
    StringValue newValue = new StringValue() { Value = fieldValue };
    var propertyVal = targetType.GetProperty("Value");
    if (propertyVal != null)
    {
    propertyVal.SetValue(field, newValue);
    }
    else
    {
    FieldInfo fieldVal = targetType.GetField("Value");
    if (fieldVal != null)
    {
    fieldVal.SetValue(field, newValue);
    }
    }
    }
    }

    #endregion
    }

     

  4. Publish the customization and import the refund. 

I attached the customization package for your reference.

Hope it helps!

@simonliang91 we will give this a try, thank you!


Thank you for sharing this tip with the community @simonliang91!


Hey,

We have a similar issue and tried making the customization work with our configuration but had no success.

Is there an easier way to do this if we want to map Shopify Order Tags to Sales Person ID for our Refunds/Returns?

Right now we map Sales Order → Details : Sales Person ID from Order Data : =IsNull(Nullif(lOrderData.Tags],''),'095')

This maps any Shopify Order Tag to the Sales Person ID, if empty it’ll set it to 095 which is our default ‘WEB’ tag.


@simonliang91 - I was testing this on 2023R1 and I didn’t seem to get the “Import Mapping” to open with the customization package published for the entity - refund.  Is there anything that I’m missing?

 

 


@StevenRatner , the customization package doesn’t add the Refund entity to the Entity Mapping screen, you need to do the mapping in the code level.


Reply