Search code examples
azure-ad-b2csendgridazure-ad-b2c-custom-policysendgrid-api-v3

SendGrid API from Custom Policy sends email with no subject


I am using custom policy that calls SendGrid API to send mails for OTP. The API is successful in sending however the subject part does not reflect the value from the request.

enter image description here

This is my custom policy code that generates the JSON.

<ClaimsTransformation Id="GenerateEmailRequestBody-Local" TransformationMethod="GenerateJson">
    <InputClaims>
        <InputClaim ClaimTypeReferenceId="email" TransformationClaimType="personalizations.0.to.0.email" />
        <InputClaim ClaimTypeReferenceId="otp" TransformationClaimType="personalizations.0.dynamic_template_data.otp" />
        <InputClaim ClaimTypeReferenceId="email" TransformationClaimType="personalizations.0.dynamic_template_data.email" />
        <InputClaim ClaimTypeReferenceId="subject" TransformationClaimType="subject" />
    </InputClaims>
    <InputParameters>
        <!-- Update the template_id value with the ID of your SendGrid template. -->
        <InputParameter Id="template_id" DataType="string" Value="d-xxxxxxxxxxxxxxxxxxxxxxxxxx" />
        <!-- Todo: update the sender -->
        <InputParameter Id="from.email" DataType="string" Value="sender@gmail.com" />
        <InputParameter Id="from.name" DataType="string" Value="Administrator" />
    </InputParameters>
    <OutputClaims>
        <OutputClaim ClaimTypeReferenceId="emailRequestBody" TransformationClaimType="outputClaim" />
    </OutputClaims>
</ClaimsTransformation>

Below is request generated from the custom policy and sent to SendGrid.

{
    "personalizations": [
        {
            "to": [
                {
                    "email": "someone@gmail.com"
                }
            ],
            "dynamic_template_data": {
                "email": "someone@gmail.com",
                "otp": "086924"
            }
        }
    ],
    "subject": "Verification code",
    "template_id": "d-xxxxxxxxxxxxxxxxxxxxxxxx",
    "from": {
        "email": "someone@gmail.com",
        "name": "Administrator"
    }
}

Is there a part that I've missed? Please help.


Solution

  • Few things to try

    1. Update the TransformationClaimType for subject.
    <InputClaim ClaimTypeReferenceId="subject" TransformationClaimType="personalizations.0.dynamic_template_data.subject" />
    
    1. Verify subject claim has a valid value. Try setting default value to inputclaim.

    2. Update the subject field in send grid portal.

    SendGrid Template Settings