1

I've generated JSON2APEX class for complicated JSON, but when I created an instance of this class I can't deploy class where this instance is created, compilator doesn't see inner classes and throws "Non-static field cannot be referenced from a static context" error. But when I checked class instance values in logs - json was fully parsed and inner classes are presented with all received fields populated.

Some fields in the received json are empty, maybe this is the cause of the problem?

My JSON2APEX class:


public class SlackTeamJoinEventDTO {

    public class Authorizations {
        public Boolean is_enterprise_install;
        public Boolean is_bot;
        public String user_id;
        public String team_id;
        public Object enterprise_id;
    }

    public class User {
        public String presence;
        public String who_can_share_contact_card;
        public Boolean is_email_confirmed;
        public Integer updated;
        public Boolean is_app_user;
        public Boolean is_bot;
        public Boolean is_ultra_restricted;
        public Boolean is_restricted;
        public Boolean is_primary_owner;
        public Boolean is_owner;
        public Boolean is_admin;
        public Profile profile;
        public Integer tz_offset;
        public String tz_label;
        public String tz;
        public String real_name;
        public String color;
        public Boolean deleted;
        public String name;
        public String team_id;
        public String id;
    }

    public Boolean is_ext_shared_channel;
    public List<Authorizations> authorizations;
    public Integer event_time;
    public String event_id;
    public String type;
    public Event event;
    public String api_app_id;
    public String team_id;
    public String token;

    public class Event {
        public String event_ts;
        public Integer cache_ts;
        public User user;
        public String type;
    }

    public class Status_emoji_display_info {
    }

    public class Profile {
        public String team;
        public String status_text_canonical;
        public String image_512;
        public String image_192;
        public String image_72;
        public String image_48;
        public String image_32;
        public String image_24;
        public String last_name;
        public String first_name;
        public String email;
        public String avatar_hash;
        public Integer status_expiration;
        public List<Status_emoji_display_info> status_emoji_display_info;
        public String status_emoji;
        public String status_text;
        public Status_emoji_display_info fields;
        public String display_name_normalized;
        public String display_name;
        public String real_name_normalized;
        public String real_name;
        public String skype;
        public String phone;
        public String title;
    }


    public static SlackTeamJoinEventDTO parse(String json) {
        return (SlackTeamJoinEventDTO) System.JSON.deserialize(json, SlackTeamJoinEventDTO.class);
    }
}

Class where json Is parsed:

public with sharing class SlackService {
    public static void createMemberAndRequestLinkedinOAuth(String body) {
        SlackTeamJoinEventDTO parsedObj = SlackTeamJoinEventDTO.parse(body);
        parsedObj.is_ext_shared_channel;
        System.debug('service' + parsedObj);
        insert new Slack_request__c(Type__c = 'Service');
       insert new Slack_Member__c(
                First_name__c = parsedObj.Profile.first_name,
                Last_name__c = parsedObj.Profile.last_name,
                Email__c = parsedObj.Profile.email,
                Profile_Picture__c = parsedObj.Profile.image512,
                Slack_Id__c = parsedObj.User.id
        );
    }

}

Errors:

enter image description here

Errors

1 Answer 1

1

You're trying to access the classes, not the instance. Consider:

public class A {
  public class B {
    public String C;
  }
  public B d;
}

If you try to write:

A val = (A)JSON.deserialize('{"d":{"C":"message"}}');
System.debug(val.b.c);

You'll get this error. Instead, you need to use the variable (here, d) in order to access C:

System.debug(val.d.c);

In other words, you need to access the data through the variables:

First_Name__c = parsedObj.event.user.profile.first_name,
Last_Name__c = parsedObj.event.user.profile.last_name,
1
  • Thank you very much, I didn't notice that :) Mar 23, 2023 at 1:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .