-1

I have a class "object" like that :

public with sharing class CalloutWS{
public Date dateEffet {get; set;}
public cls_post[] posts {get; set;}

public class cls_post {
    public Integer num;
    public Date date;   
    public cls_activite activite;
}

public class cls_activite {
    public Integer statut;  
}
}

I need to call this class and set the value in another class, in a method :

    CalloutWS calloutWS = new CalloutWS();

    calloutWS.dateEffet = Date.today();
    List<CalloutWS.cls_post> postulants = new List<CalloutWS.cls_post>();
    CalloutWS.cls_activite activitePost1 = new CalloutWS.cls_activite();
    CalloutWS.cls_post post1 = new CalloutWS.cls_post();
    post1.num= 1;
    post1.date = Date.valueOf(dateNaissance);
    post1.activite = activitePost1;
    activitePostulant1.statut = 62;
    postulants.add(post1);

The problem here is that only dateEffet ispart of the instance of calloutWS, calloutWS. It seems simple, but how can I make postulants and activitePost1 part of the instance ? I tried to make something like that :

calloutWS.cls_activite = new CalloutWS.cls_activite();

But that doesn't work either (does not compile).

2 Answers 2

5

You have an instance variable of type List<CalloutWS.cls_post>; it's posts. Note that List<CalloutWS.cls_post> and cls_post[] (within CalloutWS) are equivalent types, because cls_post is an inner class. You can assign

calloutWS.posts = postulants

Note that your variable name calloutWS shadows your class name CalloutWS. Apex is not case-sensitive. To avoid very confusing errors, change your variable name.

You do not have an instance variable of type cls_activite. You would need to create one to assign it. The compiler is reading

calloutWS.cls_activite = new CalloutWS.cls_activite();

as an attempt to assign to an inner class, which is not legal or what you intend to do.

However, you have an instance variable in the inner class cls_post that already holds the cls_activite value activitePost1.

1
  • thanks for the answer and the tip for the name of the class ! I will change it then
    – dou
    Commented Jun 12, 2018 at 14:03
4

calloutWS has a variable called as posts. You have to initialize it with 'postulants'. Thus your code will be

calloutWS.posts = postulants;

That being said, you are doing so much work in the service class.A bettwer work would be to define constructor and pass values through them.

public with sharing class CalloutWS{
    public Date dateEffet {get; set;}
    public cls_post[] posts {get; set;}


    CalloutWS(Date dateEffet ,cls_post [] posts){
        this. dateEffet = dateEffet;
        this.posts = posts;
    }
    public class cls_post {
        public Integer num;
        public Date date;   
        public cls_activite activite;

        cls_post(Integer num ,Date date,cls_activite activite){
            this.num = num;
            this.date = date;
            this.activite = activite;
        }
    }

    public class cls_activite {
        public Integer statut;  

        cls_activite (Integer statut){
            this.statut = statut;
        }

    }
}

Thus your implementation code will be of 3 lines.

CalloutWS.cls_activite activite =new CalloutWS.cls_activite(62);
CalloutWS.cls_post postulant = new CalloutWS.cls_post(1, Date.valueOf(dateNaissance),1);
CalloutWS calloutWS = new CalloutWS(Date.today(),new List<CalloutWS.cls_post>{postulant});
4
  • Dear downvoters would love to understand reasons for your downvotes Commented Jun 12, 2018 at 14:00
  • 1
    Have my upvote to balance it out.
    – David Reed
    Commented Jun 12, 2018 at 14:00
  • Ill bite. I downvoted since when you first posted it, it was just a code dump. Looks like you've polished it out since. Commented Jun 12, 2018 at 14:01
  • I might have posted as answer instead of saving as a draft. My bad :/ Commented Jun 12, 2018 at 14:02

You must log in to answer this question.

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