1

I have to pass a child record's field value while iterating by Parent records. I am getting invalid Foreign key relationship error. Can someone please help me here?

Code snippet:

global class Account_chk {
  WebService static void method1(Id i) {
  List<Call2_vod__c> a= new List<Call2_vod__c>();

  List<Attachment> p=new List<Attachment> ();
    List<Attachment> exist=new List<Attachment> ();
exist=[select ParentId,id from Attachment];

Map<Id, Attachment> existMap = new Map<Id, Attachment>();
Map<Id,Call2_vod__c> signMap = new Map<Id, Call2_vod__c>();

for (Attachment att : exist) {
    existMap.put(att.ParentId, att);
}

 List<Account> acc = new List<Account>([select Id,(select Signature_vod__c,Name from Call2_vod__r where Signature_Date_vod__c !=Null and Account_vod__c not in :existMap.keySet() limit 1) from Account ]);   
    for (Account call: acc)
    {

      Attachment record = new Attachment(Name = Call.Name ,ParentId = call.Id, Body = EncodingUtil.base64Decode(get(call).Signature_vod__c), ContentType = 'image/jpeg');
       if (record.parentId !=Null ) {
        p.add(record);
}


    }
    try { insert(p);
    } catch(System.DMLException e){
      System.debug('The following exception has occurred: ' + e.getMessage());
      }
  }
}

Getting error: Method does not exist or incorrect signature: get(SOBJECT:Account) at line 20 column 113

6
  • Check your WSDL for the correct relationship name
    – highfive
    Sep 3, 2014 at 7:09
  • Hi highfive! Can you please help me here? I don't know how to do that. I am new to salesforce. Sep 3, 2014 at 7:13
  • It is a NOT good idea to change the example code while other already answering the question... Sep 3, 2014 at 7:21
  • Ok mast0r! But it is too long, I can't give it in comments.Also the code snippet was a part of this code. Sep 3, 2014 at 7:22
  • Now the topic and your error not matching. btw, as the error also is saying, where is your get() method?
    – highfive
    Sep 3, 2014 at 7:26

1 Answer 1

4

If you want to access a child relationship list in the loop, you need to keep in mind that it is a list, again. So you will have "loop in the loop".

Example:

// First loop: iterating through accounts
for(Account acc : [Select Id, Name, (Select Field__c From Child__r) From Account]){

    // Second loop: iterating through the child relationship list
    for(Child c : acc.Child__r){
        if(acc.Name == c.Field__c){
            ....
        }
    }
}




Your SOQL-for-loop syntax is wrong. You need to define a type of the loop variable (in your case it is an Account):

for(Account call: [select Id from Account])
{
    ....
}
6
  • Hi mast0r! I have edited the class.Actually I need to pass a child object's field iterating through the master to store as attachment. Can you please have look to the whole code? Sep 3, 2014 at 7:19
  • Hi mast0r! I have followed your way.Now I am facing error in for declaration Call2_vod__r. Please find the code snippet here. for (Account call: acc ) { for(Call2_vod__c c : Call2_vod__r){ if(call.Name== c.Name){ Attachment record = new Attachment(Name = Call.Name ,ParentId = call.Id, Body = EncodingUtil.base64Decode(call.Signature_vod__c), ContentType = 'image/jpeg'); if (record.parentId !=Null ) { p.add(record); } } Sep 3, 2014 at 7:51
  • Error: Compile Error: Variable does not exist: Call2_vod__r Sep 3, 2014 at 7:53
  • @saikat My fault. I've forgott to add some syntax to the second loop (updated my answer): for(Child c : acc.Child__r) - here we need to use acc.Child__r (the child relationship is a part of the current account). Sep 3, 2014 at 7:58
  • Thanks mast0r!! It is working but I am getting now a limit exception for too many records here at the first loop start. Can you put some light here? I can create a separate question for it. Sep 3, 2014 at 8:12

You must log in to answer this question.

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