2

In reference to a previous question located here, I have a new problem.

Our marketing department had an issue with assets being tied to a particular contact record as it was causing issues with opportunities and leads. While I am unsure of the exact nature of the problem, I was asked to remove all contact names from our Customer Assets.

As a result, my code no longer functions properly. It still runs, but the customer signed by field is no longer being filled in.

I wanted to attempt creating a custom field named "Primary Contact" as a checkbox, then when that was checked off the account would automatically show the contact as being the primary.

I could then have the CreateContract and FirmwareUpgrade classes I created reference that field from the account section. After being foiled several times in trying to get that to work, I found that there already is a contact role I could use out of the box. Problem is that I can not find that as a reference anywhere in Salesforce.

Has anyone else run into this and can point me in the right direction? I just need to populate my code with a contact from the account that has ownership of the customer asset so we can send out notices to customers that a new product update is out, or that their maintenance contract is about to expire.

Contract Code:

public with sharing class CreateContract{

public void CreateContract(List<Customer_Asset__c> assets){
  List<Contract> ContractsToCreate = new List<Contract>();
  Map<String, String> signedcontact = new Map<String, String>();
  signedcontact = [Select Id, contactid from AccountContactRole where isprimary=true];


  for(Customer_Asset__c acc:assets){
      if (acc.Support_Contract__c == true){
      Contract ContractToAdd = new Contract();
      ContractToAdd.Accountid = acc.Account__c;
      ContractToAdd.StartDate = acc.DateShipped__c;
      ContractToAdd.ContractTerm = 15;
      ContractToAdd.Customer_Asset__c = acc.id;
      ContractToAdd.CompanySignedId = acc.OwnerId;
      ContractToAdd.CustomerSignedId = signedcontact;
      ContractsToCreate.add(ContractToAdd);
           }       
  } 
  if (ContractsToCreate.size() > 0)
  insert ContractsToCreate;
  }

 }
1
  • updated my code with this
    – Sai
    Commented Jul 12, 2013 at 15:32

1 Answer 1

1

listofAccountidsYou should be querying AccountContactRole Object (refer). And your query will look something like this:

Select Id, contactid, Isprimary, role from AccountContactRole where isprimary=true AND accountid == acc.Account__c;

UPDATE:

I put together this from your previous question, and on a notepad so please excuse any syntax errors. Hopefully this will be enough for you get it to work.

public with sharing class CreateContract{

public void CreateContract(List<Customer_Asset__c> assets){
   List<Contract> ContractsToCreate = new List<Contract>();
   List<Id> listofAccountIds = new List<Id>();
   Map<Id, Id> mapaccountidtocontactid = new Map<Id, Id>();

   for(Customer_Asset__c acc:assets){
      if(acc.Support_Contract__c == true){
         listofAccountids.add(acc.Account__c);
      }
   }      

   List <AccountContactRole> listPimaryContacts = [Select Id, contactid, Isprimary, accountid from AccountContactRole where isprimary=true AND accountid in :listofAccountids]; 
   for(AccountContactRole cr: listPimaryContacts){
      mapaccountidtocontactid.put(cr.accountid, cr.contactid);
   }

   for(Customer_Asset__c acc:assets){
      if(acc.Support_Contract__c == true){
         Contract ContractToAdd = new Contract();
         ContractToAdd.Accountid = acc.Account__c;
         ContractToAdd.StartDate = acc.DateShipped__c;
         ContractToAdd.ContractTerm = 15;
         ContractToAdd.Customer_Asset__c = acc.id;
         ContractToAdd.CompanySignedId = acc.OwnerId;
         ContractToAdd.CustomerSignedId = mapaccountidtocontactid.get(acc.Account__c);
         ContractsToCreate.add(ContractToAdd);
      }       
   } 

   if (ContractsToCreate.size() > 0)
      insert ContractsToCreate;
   }
}
10
  • Hi Sai - Thanks for the reply. I see Account Contact Role in Setup, but how do I activate it on the contacts screen? I've looked everywhere and the object is not referenceable in Fields or in Setup. Commented Jul 12, 2013 at 14:22
  • You can add it to the page layout of "Accounts" as a related list. Just go to the layout and drag-drop it.
    – Sai
    Commented Jul 12, 2013 at 14:28
  • Ok got the role in there, but I'm getting an illegal assignment from list error. ContractToAdd.CustomerSignedId = [Select Id, contactid, Isprimary, role from AccountContactRole where isprimary=true]; Commented Jul 12, 2013 at 14:53
  • You have use the query to the the primary contacts corresponding to the accounts on the assets. Put then in a map and then assign them in the above statement. Let me see this makes sense else I will try to put together something more detailed.
    – Sai
    Commented Jul 12, 2013 at 15:04
  • try my updated code and let me know...
    – Sai
    Commented Jul 12, 2013 at 15:20

You must log in to answer this question.

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