Skip to main content

How To: Create a custom mapping in Shopify Refund entity

  • 14 February 2024
  • 5 replies
  • 204 views

simonliang91
Acumatica Employee
Forum|alt.badge.img+1

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!

nathantrauscht
Semi-Pro II
Forum|alt.badge.img

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


Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • February 14, 2024

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


malaniz
Freshman I
  • Freshman I
  • February 16, 2024

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([OrderData.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.


StevenRatner
Varsity I
Forum|alt.badge.img

@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?

 

 


simonliang91
Acumatica Employee
Forum|alt.badge.img+1

@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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings