function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vishnu7700Vishnu7700 

Need to assign contacts of one account to another account in VF

Hi,

Need to assign contacts of one account to another account in  VF.

In above requriment i can able to display list contacts for assicoated account in VF page but unable to add selected contact of one account ot another account.

Any help is highly appricated.

 

Regards,

Vishnu.

DipakDipak

means say there is

two accounts

1- Account A
2- AccountB

and there is 4 contact

1- Contact A--AccpountA
2- ContactB- AccountA
3- ContactC- AccountB
4- ContactD- AccountB

Now you want assign COntactD with AccountA

Some thing like that

?

 

Vishnu7700Vishnu7700

Yes 

asish1989asish1989

Try this 

Account accpountA = [SELECT id,Name 
					 FROM Account
					 WHERE Name='AccountA'];
Account accpountB = [SELECT id,Name 
					 FROM Account
					 WHERE Name='AccountB'];

List<Contact> listofContact = [SELECT id, AccountId ,LastName 
							   FROM Contact 
							   WHERE accountId =:accpountA.id];
							   
List<Contact> ContactList = [SELECT id, accountId ,LastName 
							 FROM Contact 
							 WHERE AccountId =:accpountB.id];
							   
If(listofContact !=null && listofContact.Size()>0)							 
	List<Contact> listOfUpdatedContact;
	for(Contact con :listofContact){
		con.AccountId = AccpountB;
		listOfUpdatedContact.add(AccpountB);
	}
	upsert listOfUpdatedContact;
}

 

DipakDipak

I could not see any problem on this requirement , It seems easy.
please share your code, if there is any issue , I will suggest

Vishnu7700Vishnu7700

Hi,

VF code:

<apex:page controller="AssignContfromOneAcctToOthAcct">
<apex:form >
<apex:pageblock >
<!-- Display Account related Contacts -->
<apex:pageBlockTable value="{!relatedContacts}" var="val">
<apex:column value="{!val.name}"/>
<apex:column value="{!val.email}"/>
</apex:pageBlockTable>
</apex:pageblock>
</apex:form>
</apex:page>

Controller code:

public with sharing class AssignContfromOneAcctToOthAcct {

 

public List<Contact> relatedContacts { get; set; }{
List<Contact> conlist = new List<Contact>();
for(Account acc : [SELECT Name, (SELECT FirstName, LastName, Phone, Email FROM Contacts) FROM Account]){
for(Contact con : acc.contacts)
conlist.add(con);

}
return conList; //Line 14
}

}

i'm getting error as Compile Error: Return must be called from a method or trigger body at line 14 column 7

DipakDipak

Chnage the class code to this

public with sharing class AssignContfromOneAcctToOthAcct {

 

public List<Contact> getRelatedContacts (){
       List<Contact> conlist = new List<Contact>();
       for(Account acc : [SELECT Name, (SELECT FirstName, LastName, Phone, Email FROM Contacts) FROM Account]){
             for(Contact con : acc.contacts)
                   conlist.add(con);

             }
      return conList; //Line 14
    }

}

DipakDipak
Also another way is

public with sharing class AssignContfromOneAcctToOthAcct {



public List<Contact> relatedContacts{
get{
List<Contact> conlist = new List<Contact>();
for(Account acc : [SELECT Name, (SELECT FirstName, LastName, Phone, Email FROM Contacts) FROM Account]){
for(Contact con : acc.contacts)
conlist.add(con);

}
return conList; //Line 14
}
set;
}

}

Vishnu7700Vishnu7700

Hi Dipak,

Tried as u said it is working fine.

For existing logic i had added some additional logic and stuck with error as Error: Invalid field contact for SObject Contact

Please have alook

VF

<apex:page controller="AssignContfromOneAcctToOthAcct">
<apex:form >
<apex:pageblock >
<apex:repeat value="{!acc}" var="ac">
<apex:pageBlockSection title="{!ac.name}" columns="1">
<apex:pageBlockTable value="{!ac.contact}" var="val">
<apex:column value="{!val.Firstname}"/>
<apex:column value="{!val.Lastname}"/>
<apex:column value="{!val.Phone}"/>
<apex:column value="{!val.Email}"/>
<apex:column headerValue="Select">
<apex:inputCheckbox value="{!val.Id}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:repeat>
</apex:pageblock>
</apex:form>
</apex:page>

controller

public with sharing class AssignContfromOneAcctToOthAcct {

public List<Contact> getAcc() {
List<Contact> conlist = new List<Contact>();
for(Account acc1 : [SELECT Name, (SELECT FirstName, LastName, Phone, Email FROM Contacts) FROM Account]){
for(Contact con : acc1.contacts)
conlist.add(con);

}
return conList;
}
}

DipakDipak

From apex method

public List<Contact> getAcc() {
List<Contact> conlist = new List<Contact>();
for(Account acc1 : [SELECT Name, (SELECT FirstName, LastName, Phone, Email FROM Contacts) FROM Account]){
for(Contact con : acc1.contacts)
conlist.add(con);

}
return conList;
}

 

you are returning list of CONTACT,

but at VF, it seems , you need to access ACCOUNT list

 

<apex:repeat value="{!acc}" var="ac">
<apex:pageBlockSection title="{!ac.name}" columns="1">
<apex:pageBlockTable value="{!ac.contact}" var="val">
<apex:column value="{!val.Firstname}"/>
<apex:column value="{!val.Lastname}"/>
<apex:column value="{!val.Phone}"/>
<apex:column value="{!val.Email}"/>
<apex:column headerValue="Select">
<apex:inputCheckbox value="{!val.Id}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:repeat>

 

 

So At line <apex:pageBlockTable value="{!ac.contact}" var="val">....System tries to find contactField From returned CONTACT List from apex method.....


Please return the ACCOUNT list from apex method