+ Start a Discussion
Charni WigginsCharni Wiggins 

Map on one object name with another object id

I am trying to construct a map of one object (c2g_codaCompany__C) name with another object (Journal_Stagings__c) id. But I am receiving one error, please help?

Invalid bind expression type of Journal_Staging__c for Id field of SObject c2g__codaCompany__c

Code: 

List<Journal_Staging__c> journals = new List<Journal_Staging__c>();
        // map to get all companies
        Map<Id, c2g__codaCompany__c> companies = New Map<Id, c2g__codaCompany__c>([Select Id, Name FROM c2g__codaCompany__c]);
        // for each company, create a journal, setting that company id on the journal - add this to list
        for(c2g__codaCompany__c c : companies.keySet()) {
			journals.add(new Journal_Staging__c(
            // set company lookup - only put id not name
			Company__c = c.Id));
		}
        // insert journals to get id for jlis
		insert journals;
        // map of company name to journal id
        List<Journal_Staging__c> getNewJournals = [Select Id, Name, Company__c FROM Journal_Staging__c];
        Map<String, Id> companyJournal = New Map<String, Id>();
        for(Journal_Staging__c j : [SELECT Name FROM c2g__codaCompany__c WHERE Id=: getNewJournals]) {
			companyJournal.put(j.Name, j.Id);
		}
GauravendraGauravendra
Hi Charni,

Everything seems fine with your code, except when you are lopping in c2g__codaCompany__c object and try to store in Journal_Staging__c.

Find the updated code, you may need modification accourding to you. 
What I got from your question is you are trying to create a map:
Map<String,Id> --> Map<c2g__codaCompany__c.Name,Journal_Staging__c.Id>.
 
List<Journal_Staging__c> journals = new List<Journal_Staging__c>();
        // map to get all companies
        Map<Id, c2g__codaCompany__c> companies = New Map<Id, c2g__codaCompany__c>([Select Id, Name FROM c2g__codaCompany__c]);
        // for each company, create a journal, setting that company id on the journal - add this to list
        for(c2g__codaCompany__c c : companies.keySet()) {
			journals.add(new Journal_Staging__c(
            // set company lookup - only put id not name
			Company__c = c.Id));
		}
        // insert journals to get id for jlis
		insert journals;
        // map of company name to journal id
        List<Journal_Staging__c> getNewJournals = [Select Id, Name, Company__c FROM Journal_Staging__c];
		// Create list of c2g__codaCompany__c record
		List<c2g__codaCompany__c> c2gList = [Select Id,Name from c2g__codaCompany__c];
        Map<String, Id> companyJournal = New Map<String, Id>();
		String c2gName;
        for(Journal_Staging__c j : [SELECT Id,Name,Company__c FROM Journal_Staging__c WHERE Id =: getNewJournals]) {
			for(c2g__codaCompany__c cc : c2gList) {
				if(j.Company__c == cc.Id) {
					c2gName = cc.Name;
				}
			}
			
			companyJournal.put(c2gName, j.Id);
			c2gName='';
		}