+ Start a Discussion
Christos KolonisChristos Kolonis 

Update field with values from map (OpportunityContactRole)

Hello, 
I need to pass the OpportunityContactRole (contact id) to a new record's field, which is a contact one. I created a map for the OpportunityContactRole and i am trying to pass that id, when i debug at the MAP i get the values but when i pass the contact id to the field i get the error of attempitng to de-reference a null object. Above is a snippet of my code:
 
map<Id, OpportunityContactRole> oppRoleMap = new map<Id, OpportunityContactRole>([select id, OpportunityId, ContactId, isPrimary from OpportunityContactRole where OpportunityId IN: oppId AND isPrimary = true limit 1]);
            	system.debug('The ROLE MAP IS' + oppRoleMap);//i get the values
                system.debug('RoleMap Values ' +oppRoleMap.values());//i get also the values
                //csconta__Contract__c contr = [select id, csconta__Account__r.id from csconta__Contract__c where csconta__Account__r.id =: opp1.accountId];
                
                for(opportunity opp: oppList){
                
                if(oppMap.containsKey(opp.id)){
                    system.debug('inside first if ' + oppMap.containsKey(opp.id));
                    
                if(opp.StageName == 'Closed Won'){
                    system.debug('IS CLOSED WON? ' +opp.StageName);
                    list<csconta__Contract__c> contrList = [select id, csconta__Account__r.id from csconta__Contract__c where csconta__Account__r.id =: opp.accountId];
                    
                    if(contrList.isEmpty()){
                   csconta__Contract__c contract = new csconta__Contract__c();
                  // contract.csconta__Contract_Name__c = 'Hello' +' Contract';
                   contract.csconta__Contract_Name__c = oppMap.get(opp.id).account.name + ' Contract';
                   contract.csconta__Account__c = opp.AccountId;
                   contract.csconta__Valid_From__c = system.today();
                   contract.csconta__Status__c = 'Open';
                   contract.csconta__Contact__c = oppRoleMap.get(opp.id).ContactId;// seems like its null
                        system.debug('The contact is '+ contract.csconta__Contact__c);
                   createcontrList.add(contract); 
                    }

What can i do to pass the contact id?
Best Answer chosen by Christos Kolonis
sachinarorasfsachinarorasf
Hi <Christos>,

In the below map, you are using the Id of the OpportunityContactRole as a key.
map<Id, OpportunityContactRole> oppRoleMap = new map<Id, OpportunityContactRole>([select id, OpportunityId, ContactId, isPrimary from OpportunityContactRole where OpportunityId IN: oppId AND isPrimary = true limit 1]);

Here, you are passing the Id of the Opportunity to the same map which is inappropriate.
contract.csconta__Contact__c = oppRoleMap.get(opp.id).ContactId;

Since, the oppRoleMap contains only single record. You can directly assign the contact Id by using the below line of code:
contract.csconta__Contact__c = oppRoleMap.Values().ContactId;

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com